2011-12-19 47 views
9

Tôi mới dùng MATLAB và không biết cách sử dụng libsvm. Có bất kỳ mã mẫu nào để phân loại một số dữ liệu (với 2 tính năng) bằng SVM và sau đó trực quan hóa kết quả không? Làm thế nào về với hạt nhân (RBF, đa thức và Sigmoid)? tôi thấy rằng tập tin readme trong gói libsvm, nhưng tôi không thể làm cho một người đứng đầu hoặc đuôi của nó, bạn sẽ vui lòng đưa ra một ví dụ về phân loại 2 lớp sử dụng Support Vector Machines (SVM) trong matlab cái gì đó như:Làm thế nào để sử dụng libsvm trong Matlab?

Attribute_1 Attribute_2 Class 
170   66   -1 
160   50   -1 
170   63   -1 
173   61   -1 
168   58   -1 
184   88   +1 
189   94   +1 
185   88   +1 

Bất kỳ trợ giúp nào cũng sẽ được đánh giá cao.

+0

Bạn đang sử dụng libsvm từ đây: http://www.csie.ntu.edu.tw/~ cjlin/libsvm /? –

+0

có, tôi cũng đã thấy hướng dẫn ở đó nhưng không thể sử dụng nó – Sina

Trả lời

12

Trong gói libsvm, trong file matlab/README, bạn có thể tìm thấy các ví dụ sau:

Examples 
======== 

Train and test on the provided data heart_scale: 

matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale'); 
matlab> model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07'); 
matlab> [predict_label, accuracy, dec_values] = svmpredict(heart_scale_label, heart_scale_inst, model); % test the training data 

For probability estimates, you need '-b 1' for training and testing: 

matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale'); 
matlab> model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07 -b 1'); 
matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale'); 
matlab> [predict_label, accuracy, prob_estimates] = svmpredict(heart_scale_label, heart_scale_inst, model, '-b 1'); 

To use precomputed kernel, you must include sample serial number as 
the first column of the training and testing data (assume your kernel 
matrix is K, # of instances is n): 

matlab> K1 = [(1:n)', K]; % include sample serial number as first column 
matlab> model = svmtrain(label_vector, K1, '-t 4'); 
matlab> [predict_label, accuracy, dec_values] = svmpredict(label_vector, K1, model); % test the training data 

We give the following detailed example by splitting heart_scale into 
150 training and 120 testing data. Constructing a linear kernel 
matrix and then using the precomputed kernel gives exactly the same 
testing error as using the LIBSVM built-in linear kernel. 

matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale'); 
matlab> 
matlab> % Split Data 
matlab> train_data = heart_scale_inst(1:150,:); 
matlab> train_label = heart_scale_label(1:150,:); 
matlab> test_data = heart_scale_inst(151:270,:); 
matlab> test_label = heart_scale_label(151:270,:); 
matlab> 
matlab> % Linear Kernel 
matlab> model_linear = svmtrain(train_label, train_data, '-t 0'); 
matlab> [predict_label_L, accuracy_L, dec_values_L] = svmpredict(test_label, test_data, model_linear); 
matlab> 
matlab> % Precomputed Kernel 
matlab> model_precomputed = svmtrain(train_label, [(1:150)', train_data*train_data'], '-t 4'); 
matlab> [predict_label_P, accuracy_P, dec_values_P] = svmpredict(test_label, [(1:120)', test_data*train_data'], model_precomputed); 
matlab> 
matlab> accuracy_L % Display the accuracy using linear kernel 
matlab> accuracy_P % Display the accuracy using precomputed kernel 

Note that for testing, you can put anything in the 
testing_label_vector. For more details of precomputed kernels, please 
read the section ``Precomputed Kernels'' in the README of the LIBSVM 
package. 
+0

Tôi biết chủ đề này là cũ, nhưng những gì có nghĩa là "mẫu số serial" liên quan đến hạt nhân precomputed? – basti

+1

Chúng đang đề cập đến '(1: n) ''. Về cơ bản, bạn có thể cung cấp hạt nhân theo thứ tự khác với mẫu của bạn. Nếu không, chỉ cần sử dụng '(1: n) '' – Oli

Các vấn đề liên quan