svm matalb 代码
时间: 2023-07-09 13:41:32 浏览: 49
以下是一个简单的 SVM 代码示例,使用 Matlab 中的 fitcsvm 函数:
```matlab
% 加载数据集
load fisheriris
X = meas(:,1:2);
y = grp2idx(species);
% 将数据集分为训练集和测试集
rng(1); % 设置随机数种子,保证结果可重复
cv = cvpartition(y,'HoldOut',0.3);
X_train = X(cv.training,:);
y_train = y(cv.training,:);
X_test = X(cv.test,:);
y_test = y(cv.test,:);
% 定义 SVM 模型
svm = fitcsvm(X_train, y_train);
% 预测测试集
y_pred = predict(svm, X_test);
% 输出模型评估结果
accuracy = sum(y_pred == y_test) / numel(y_test);
fprintf('Accuracy: %f\n', accuracy);
```
这段代码同样使用 Iris 数据集进行训练和测试,使用 SVM 模型进行分类,并输出模型的准确率评估结果。需要注意的是,Matlab 中的 SVM 函数有很多参数可以调节,这里只是使用了默认参数。如果需要更好的分类效果,可以考虑调整参数或者使用交叉验证等技术。
相关问题
SVMmatlab代码
以下是一个简单的SVM Matlab代码示例:
```matlab
% 加载数据
load fisheriris;
X = meas(:,1:2);
Y = species;
% 将鸢尾花数据集分成训练和测试集
cv = cvpartition(Y,'HoldOut',0.3);
Xtrain = X(cv.training,:);
Ytrain = Y(cv.training,:);
Xtest = X(cv.test,:);
Ytest = Y(cv.test,:);
% 训练SVM模型
SVMModel = fitcsvm(Xtrain,Ytrain);
% 预测测试集结果
Ypred = predict(SVMModel,Xtest);
% 计算精度
accuracy = sum(Ypred == Ytest)/length(Ytest);
% 显示分类结果
gscatter(Xtest(:,1),Xtest(:,2),Ytest)
hold on
h = ezplot(@(x1,x2)predict(SVMModel,[x1,x2]),get(gca,'XLim'));
h.Color = 'r';
h.LineWidth = 2;
title(sprintf('Accuracy = %.2f%%',accuracy*100))
legend('versicolor','virginica','SVM decision boundary')
hold off
```
此代码演示了如何使用SVM对鸢尾花数据集进行分类。首先,数据集被加载并分成训练和测试集。然后,使用fitcsvm函数训练SVM模型。接下来,使用predict函数预测测试集结果,并计算精度。最后,使用gscatter和ezplot函数可视化分类结果和决策边界。
svmmatlab代码
以下是一个简单的SVM Matlab代码示例:
首先,我们需要加载数据:
```matlab
load fisheriris
X = meas(:,3:4);
Y = (strcmp('virginica',species));
```
然后我们可以训练SVM模型:
```matlab
SVMModel = fitcsvm(X,Y,'KernelFunction','linear','Standardize',true);
```
这里使用线性核函数,并进行标准化处理。
接下来,我们可以使用训练好的模型进行预测:
```matlab
[label,score] = predict(SVMModel,X);
```
最后,我们可以将结果可视化:
```matlab
gscatter(X(:,1),X(:,2),Y)
hold on
h = svmplot(SVMModel);
title(sprintf('SVM Classification with \\bf%s\\rm Kernel',char(SVMModel.KernelFunction)))
legend({'Setosa','Non-Setosa','Support Vectors'},'Location','Best')
hold off
```
其中,svmplot 函数可以用来绘制SVM分类器的决策边界和支持向量。
完整代码如下:
```matlab
load fisheriris
X = meas(:,3:4);
Y = (strcmp('virginica',species));
SVMModel = fitcsvm(X,Y,'KernelFunction','linear','Standardize',true);
[label,score] = predict(SVMModel,X);
gscatter(X(:,1),X(:,2),Y)
hold on
h = svmplot(SVMModel);
title(sprintf('SVM Classification with \\bf%s\\rm Kernel',char(SVMModel.KernelFunction)))
legend({'Setosa','Non-Setosa','Support Vectors'},'Location','Best')
hold off
function h = svmplot(model)
w = model.Beta;
b = model.Bias;
if numel(w) > 2
error('svmplot only works for 2D models');
end
x = linspace(-1,1);
y = (-b - w(1)*x)/w(2);
h = plot(x,y,'k');
if model.KernelParameters.Scale
sv = model.SupportVectors;
if size(sv,2) > 2
error('svmplot only works for 2D models');
end
w = w*model.Sigma;
x = sv(:,1);
y = sv(:,2);
z = 1-b-w(1)*x-w(2)*y;
margin = 1./sqrt(sum(w.^2));
xlim = get(gca,'XLim');
ylim = get(gca,'YLim');
[X,Y] = meshgrid(linspace(xlim(1),xlim(2),100),linspace(ylim(1),ylim(2),100));
Z = reshape(1-b-w(1)*X(:)-w(2)*Y(:),size(X));
contour(X,Y,Z,[-margin,0,margin],'k');
plot(x,y,'ko','MarkerSize',10);
end
end
```