matlab自带的 svm 散点图
时间: 2023-10-27 17:08:22 浏览: 102
matlab画散点图.pdf
Matlab自带的SVM散点图可以通过使用plot函数和svmtrain/svmclassify函数来绘制。具体步骤如下:
1.加载数据并将其分为训练集和测试集。
例如:
load fisheriris
inds = ~strcmp(species,'setosa');
X = meas(inds,:);
y = species(inds,:);
y = nominal(y);
N = size(X,1); %观测值数
idx = randperm(N);
trainingSize = floor(N/2);
Xtrain = X(idx(1:trainingSize),:);
Xtest = X(idx(trainingSize+1:end),:);
ytrain = y(idx(1:trainingSize));
ytest = y(idx(trainingSize+1:end));
2.训练SVM分类器
例如:
SVM = svmtrain(Xtrain,ytrain);
SVMstruct = svmtrain(Xtrain,ytrain,'ShowPlot',true);
3.使用测试集测试分类器
例如:
group = svmclassify(SVM,Xtest);
4.使用plot函数绘制散点图
例如:
gscatter(X(:,1),X(:,2),species,'rgb','osd');
hold on;
plot(X(:,1),X(:,2),'k.');
plot(SVM.SupportVectors(:,1),SVM.SupportVectors(:,2),'ko','MarkerSize',10);
legend('setosa','versicolor','virginica','Observations','Support Vector');
hold off;
阅读全文