MATLAB Answers

Why when I change the number of features in sequentialfs in this code, I get the following error in SVM?

조회 수: 6(최근 30일)
Penny13 6 Sep 2019
답변: Yogesh Khurana 17 Dec 2019
When I perform SVM using the following code and I have 26 features, when I change the number of feature from 2 to 4 for example, I get the following error in line related to sequentialfs?
Thanks for your guidance,
%%% The code
close all;
clear all;
%% When they are from different patients
X_train = xlsread('PM105_antibodies_uptake_intensity.xlsx');
[num,str] = xlsread('labels_PM105_intensity.xlsx');
y_train = grp2idx(str);
X_test = xlsread('PM80_antibodies_uptake_intensity.xlsx');
[num2,str2] = xlsread('labels_PM80_intensity.xlsx');
y_test = grp2idx(str2);
% features_antibodies_uptake
% labels
c = cvpartition(y_train,'k',5);
opts = statset('display','iter');
classf = @(train_data, train_labels, test_data, test_labels)...
sum(predict(fitcsvm(train_data, train_labels,'KernelFunction','rbf'), test_data) ~= test_labels);
[fs, history] = sequentialfs(classf, X_train, y_train, 'cv', c, 'options', opts,'nfeatures',2);
%% Best hyperparameter
X_train_w_best_feature = X_train(:,fs);
Md1 = fitcsvm(X_train_w_best_feature,y_train,'KernelFunction','rbf','OptimizeHyperparameters','auto',...
'expected-improvement-plus','ShowPlots',true)); % Bayes' Optimization ??.
%% Final test with test set
X_test_w_best_feature = X_test(:,fs);
test_accuracy_for_iter = sum((predict(Md1,X_test_w_best_feature) == y_test))/length(y_test)*100
%% hyperplane ??
hgscatter = gscatter(X_train_w_best_feature(:,1),X_train_w_best_feature(:,2),y_train);
hold on;
% test set? data? ?? ??? ????.
% decision plane
XLIMs = get(gca,'xlim');
YLIMs = get(gca,'ylim');
[xi,yi] = meshgrid([XLIMs(1):0.01:XLIMs(2)],[YLIMs(1):0.01:YLIMs(2)]);
dd = [xi(:), yi(:)];
pred_mesh = predict(Md1, dd);
redcolor = [1, 0.8, 0.8];
bluecolor = [0.8, 0.8, 1];
pos = find(pred_mesh == 1);
h1 = plot(dd(pos,1), dd(pos,2),'s','color',redcolor,'Markersize',5,'MarkerEdgeColor',redcolor,'MarkerFaceColor',redcolor);
pos = find(pred_mesh == 2);
h2 = plot(dd(pos,1), dd(pos,2),'s','color',bluecolor,'Markersize',5,'MarkerEdgeColor',bluecolor,'MarkerFaceColor',bluecolor);
legend([hgscatter;h_sv],{'setosa','versicolor','support vectors'})
%%% The error
Error using classreg.learning.impl.CompactSVMImpl/score (line 62)
You must pass X as a matrix with 4 columns.
Error in classreg.learning.classif.CompactClassificationSVM/score (line 564)
f = score(this.Impl,X,true,varargin{:});
Error in classreg.learning.classif.ClassificationModel/predict (line 411)
scores = score(this,X,varargin{:});
Error in classreg.learning.classif.CompactClassificationSVM/predict (line 406)
Error in svm_test_MATLAB (line 88)
pred_mesh = predict(Md1, dd);


Yogesh Khurana
Yogesh Khurana 17 Dec 2019
It is clear from the error message that the features you are passing to the predict function is not in correct format in which the function accepts. The second argument for predict function is the “Predictor data to be classified”. The predictor data should have format like each row in that matrix corresponds to one observation that is to be predicted while each column defines each variable or feature. So, the number of columns should be equal to number of features. In your case it should be 4.
I am attaching a link to which you can refer to get the details on the format that is passed to predict function. Please refer to the following link for more details:
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by