如何使用fitcsvm而不是svmtrain?

时间:2019-01-05 11:59:41

标签: matlab libsvm

我正在运行svmtrain函数,但它显示svmtrain已被删除,而改用fitcsvm。当我使用fitcsvm时,仍然无法建立SVM模型。我不知道如何解决。

代码和错误如下所示:

% Generate and plot X1
randn('seed',50)
m=[0 0; 1.2 1.2]'; % mean vectors
S=0.2*eye(2); % covariance matrix
points_per_class1=200;
X1=mvnrnd(m(:,1),S,points_per_class1)';
X1=[X1 mvnrnd(m(:,2),S,points_per_class1)'];
y1=[ones(1,points_per_class1) -ones(1,points_per_class1)];
% figure(1), plot(X1(1,y1==1),X1(2,y1==1),'r+', X1(1,y1==-1),X1(2,y1==-1),'b*')


% Generate X2
randn('seed',100)
points_per_class2=300;
X2=mvnrnd(m(:,1),S,points_per_class2)';
X2=[X2 mvnrnd(m(:,2),S,points_per_class2)'];
y2=[ones(1,points_per_class2) -ones(1,points_per_class2)];
% figure(2), plot(X2(1,y2==1),X2(2,y2==1),'r.', X2(1,y2==-1),X2(2,y2==-1),'bo');

%Train the model
model = fitcsvm (y1',X1'); % -t kernel_type : set type of kernel function (0 -- linear: u'*v)
figure(1);svmplot(y1',X1',model);title('Training data');


% Predict Result
[predict_label_1,accuracy_1,dec_values_1] = svmpredict(y1',X1',model);
[predict_label_2,accuracy_2,dec_values_2] = svmpredict(y2',X2',model);
result_1 = [y1' predict_label_1];
result_2 = [y2' predict_label_2];


% Plot the Result
figure(2);plot(1:length(y2),y2','r-*');hold on
plot(1:length(y2),predict_label_2,'b:o');grid on
legend('Real','Predict')
xlabel('Number')
ylabel('Label')
string = {['accuracy = ' num2str(accuracy_2(1)) '%']};
title(string)
Error using classreg.learning.internal.ClassLabel (line 29)
You must pass class labels as a vector.

Error in ClassificationSVM.prepareData (line 618)
            allClassNames =
            levels(classreg.learning.internal.ClassLabel(Y));

Error in classreg.learning.FitTemplate/fit (line 213)
                    this.PrepareData(X,Y,this.BaseFitObjectArgs{:});

Error in ClassificationSVM.fit (line 240)
            this = fit(temp,X,Y);

Error in fitcsvm (line 316)
    obj = ClassificationSVM.fit(X,Y,RemainingArgs{:});

0 个答案:

没有答案