clear all
X=[0 0;2 4;3 3;3 4;4 2;4 4;4 3;5 3;6 2;7 1;2 9;3 8;4 6;4 7;5 6;5 8;6 6;7 4;8 4;10 10]; %训练数据点
Y=[1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2]'; %训练数据已知分类情况
cv = cvpartition(length(Y), 'HoldOut', 0.2); % 使用 80/20 比例进行交叉验证
trainIndices = training(cv); % 获取训练集索引
testIndices = test(cv); % 获取测试集索引
X_train = X(trainIndices, :); % 训练集特征变量
Y_train = Y(trainIndices); % 训练集类标签
X_test = X(testIndices, :); % 测试集特征变量
Y_test = Y(testIndices); % 测试集类标签

% 创建并配置 SVM 模型
svmModel = fitcsvm(X_train, Y_train); % 默认参数设置
[~, ~, scores] = predict(svmModel, X_test); % 返回预测结果及其得分
accuracy = sum(scores == Y_test)/numel(Y_test); % 计算准确率
disp(['Accuracy on the testing set: ', num2str(accuracy)]);

Accuracy on the testing set: 0        0.75

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐