如何在Matlab中使用libsvm?

我是Matlab的新手,不知道如何使用libsvm。是否有示例代码可以用来对具有两个特征的数据进行分类,并使用支持向量机(SVM)然后可视化结果?使用核函数(RBF、多项式和Sigmoid)的情况如何?我看到了libsvm包中的readme文件,但我无法理解其中的内容。您能否提供一个在Matlab中使用支持向量机(SVM)对两个类别进行分类的示例,像这样:

Attribute_1    Attribute_2   Class170            66            -1160            50            -1170            63            -1173            61            -1168            58            -1184            88            +1189            94            +1185            88            +1

任何帮助都将不胜感激。


回答:

在libsvm包中,在文件matlab/README中,您可以找到以下示例:

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 dataFor 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 columnmatlab> model = svmtrain(label_vector, K1, '-t 4');matlab> [predict_label, accuracy, dec_values] = svmpredict(label_vector, K1, model); % test the training dataWe 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 Datamatlab> 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 Kernelmatlab> 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 Kernelmatlab> 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 kernelmatlab> accuracy_P % Display the accuracy using precomputed kernelNote 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.

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注