How to use svmtrain for data classification and prediction
In this article, the editor introduces in detail "how to use svmtrain for data classification and prediction". The content is detailed, the steps are clear, and the details are handled properly. I hope this "how to use svmtrain for data classification and prediction" article can help you solve your doubts.
After installing the libsvm toolkit, use svmtrain for data classification and prediction
% clear environment variables
Close all
Clear
Clc
Format compact
%% data extraction
% load test data wine
% contains data as classnumber = 3
Matrix of% wine:178*13
Column vector of% wine_labes:178*1
Load wine.mat
%% draw box visualization diagram of test data
Figure
Boxplot (wine,'orientation','horizontal','labels',categories)
Title ('box visualization of wine data', 'FontSize',12)
Xlabel ('attribute value', 'FontSize',12)
Grid on
%% draw the fractal dimension visualization diagram of the test data
Figure
Subplot (3, 5, 1)
Hold on
For run = 1PUR 178
Plot (run,wine_labels (run),'*')
End
Xlabel ('sample', 'FontSize',10)
Ylabel ('category tag', 'FontSize',10)
Title ('class','FontSize',10)
For run = 2:14
Subplot (3, 5, run)
Hold on
Str = ['attrib', num2str (run-1)]
For I = 1PUR 178
Plot (iGraine wine),'*')
End
Xlabel ('sample', 'FontSize',10)
Ylabel ('attribute value', 'FontSize',10)
Title (str,'FontSize',10)
End
Selected training set and test set
1-30 of the first category, 60-95 of the second category, 131-153 of the third category as the training set
Train_wine = [wine (1 wine 30:); wine (60 15); wine (131 15)]
The tags of the corresponding training sets should also be separated.
Train_wine_labels = [wine_labels (1:30); wine_labels (60:95); wine_labels (131 wine_labels 153)]
% take 31-59 of the first category, 96-130 of the second category, and 154-178 of the third category as the test set
Test_wine = [wine (31 wine 59:); wine (96 14 130:); wine (154 14: 178)]
The tags of the corresponding test sets should also be separated.
Test_wine_labels = [wine_labels (31:59); wine_labels (9614); wine_labels (154)]
%% data preprocessing
% data preprocessing to normalize the training set and test set to the [0d1] interval
[mtrain,ntrain] = size (train_wine)
[mtest,ntest] = size (test_wine)
Dataset = [train_wine;test_wine]
% mapminmax is the normalized function that comes with MATLAB
[dataset_scale,ps] = mapminmax (dataset',0,1)
Dataset_scale = dataset_scale'
Train_wine = dataset_scale (1)
Test_wine = dataset_scale (mtrain+1): (mtrain+mtest),:)
%% SVM network training
Model = svmtrain (train_wine_labels, train_wine,'- c 2-g 1')
% SVM Network Forecast
[predict_label, accuracy] = svmpredict (test_wine_labels, test_wine, model)
%% result analysis
Actual classification and prediction classification diagram of% test set
% from the chart, we can see that only one test sample was misclassified.
Figure
Hold on
Plot (test_wine_labels,'o')
Plot (predict_label,'r*')
Xlabel ('test set sample', 'FontSize',12)
Ylabel ('category tag', 'FontSize',12)
Legend ('actual test set classification', 'predictive test set classification')
Title ('actual and predictive classification diagrams of test sets', 'FontSize',12)
Grid on
After reading this, the article "how to use svmtrain for data classification and prediction" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.