LIBSVM数据准备 [英] LIBSVM data preparation
问题描述
我正在做一个有关Matlab中图像处理的项目,并希望实现LIBSVM进行监督学习.
I am doing a project on Image processing in Matlab and wish to implement LIBSVM for supervised learning.
我在数据准备中遇到了问题. 我有CSV格式的数据,当我尝试使用LIBSVM常见问题解答中提供的信息将其转换为libsvm格式时:-
I am encountering a problem in data preparation. I have the data in CSV format and when i try to convert it into libsvm format by using the information provided in LIBSVM faq:-
matlab> SPECTF = csvread('SPECTF.train'); % read a csv file
matlab> labels = SPECTF(:, 1); % labels from the 1st column
matlab> features = SPECTF(:, 2:end);
matlab> features_sparse = sparse(features); % features must be in a sparse matrix
matlab> libsvmwrite('SPECTFlibsvm.train', labels, features_sparse);
我以以下形式获取数据:
I get the data in the following form:
3.0012 1:2.1122 2:0.9088 ...... [值1] [索引1]:[值2] [索引2]:[值3]
3.0012 1:2.1122 2:0.9088 ...... [value 1] [index 1]:[value 2] [index 2]:[value 3]
这是第一个值不包含索引,索引1之后的值是值2.
That is the first value takes no index and the value following the index 1 is value 2.
从我阅读的内容来看,数据应采用以下格式:
From what i had read, the data should be in the following format:
[标签] [索引1]:[值1] [索引2]:[值2] ...
[label] [index 1]:[value 1] [index 2]:[value 2]......
[标签] [索引1]:[值1] [索引2]:[值2] ...
[label] [index 1]:[value 1] [index 2]:[value 2]......
我需要帮助来解决这个问题. 而且,如果有人能给我提供标签提示的方法,那将真的很有帮助.
I need help to make this right. And also if anyone would give me a clue about how to give labels it will be really helpful.
预先感谢您, 锡德拉(Sidra)
Thanking you in advance, Sidra
推荐答案
您不必将数据写入文件,而可以使用LIBSVM的Matlab接口.此接口包含两个功能,svmtrain
和svmpredict
.如果不带参数调用,每个函数都会打印帮助文本:
You don't have to write data to a file, you can instead use the Matlab interface to LIBSVM. This interface consists of two functions, svmtrain
and svmpredict
. Each function prints a help text if called without arguments:
Usage: model = svmtrain(training_label_vector, training_instance_matrix, 'libsvm_options');
libsvm_options:
-s svm_type : set type of SVM (default 0)
0 -- C-SVC
1 -- nu-SVC
2 -- one-class SVM
3 -- epsilon-SVR
4 -- nu-SVR
-t kernel_type : set type of kernel function (default 2)
0 -- linear: u'*v
1 -- polynomial: (gamma*u'*v + coef0)^degree
2 -- radial basis function: exp(-gamma*|u-v|^2)
3 -- sigmoid: tanh(gamma*u'*v + coef0)
4 -- precomputed kernel (kernel values in training_instance_matrix)
-d degree : set degree in kernel function (default 3)
-g gamma : set gamma in kernel function (default 1/num_features)
-r coef0 : set coef0 in kernel function (default 0)
-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)
-m cachesize : set cache memory size in MB (default 100)
-e epsilon : set tolerance of termination criterion (default 0.001)
-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)
-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)
-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)
-v n : n-fold cross validation mode
-q : quiet mode (no outputs)
和
Usage: [predicted_label, accuracy, decision_values/prob_estimates] = svmpredict(testing_label_vector, testing_instance_matrix, model, 'libsvm_options')
Parameters:
model: SVM model structure from svmtrain.
libsvm_options:
-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet
Returns:
predicted_label: SVM prediction output vector.
accuracy: a vector with accuracy, mean squared error, squared correlation coefficient.
prob_estimates: If selected, probability estimate vector.
在具有三个功能的四个点的数据集上训练线性SVM的示例代码:
Example code for training a linear SVM on a data set of four points with three features:
training_label_vector = [1 ; 1 ; -1 ; -1];
training_instance_matrix = [1 2 3 ; 3 4 5 ; 5 6 7; 7 8 9];
model = svmtrain(training_label_vector, training_instance_matrix, '-t 0');
将生成的model
应用于测试数据
Applying the resulting model
to test data
testing_instance_matrix = [9 5 1; 2 9 5];
predicted_label = svmpredict(nan(2, 1), testing_instance_matrix, model)
产生
predicted_label =
-1
-1
您也可以将真实的testing_label_vector
传递给svmpredict
,以便它直接计算精度.我在这里用NaNs代替了真正的标签.
You can also pass the true testing_label_vector
to svmpredict
so that it directly computes the accuracy; I here replaced the true labels by NaNs.
请注意,Matlab的 Statistics Toolbox 中还有一个功能svmtrain
与LIBSVM的功能不兼容–请确保调用正确的功能.
Please note that there is also a function svmtrain
in Matlab's Statistics Toolbox which is incompatible with the one from LIBSVM – make sure you call the correct one.
这篇关于LIBSVM数据准备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!