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

4 views (last 30 days)
Hello,
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',...
'HyperparameterOptimizationOptions',struct('AcquisitionFunctionName',...
'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 ??
figure;
hgscatter = gscatter(X_train_w_best_feature(:,1),X_train_w_best_feature(:,2),y_train);
hold on;
h_sv=plot(Md1.SupportVectors(:,1),Md1.SupportVectors(:,2),'ko','markersize',8);
% test set? data? ?? ??? ????.
gscatter(X_test_w_best_feature(:,1),X_test_w_best_feature(:,2),y_test,'rb','xx')
% 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);
uistack(h1,'bottom');
uistack(h2,'bottom');
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)
predict@classreg.learning.classif.ClassificationModel(this,X,varargin{:});
Error in svm_test_MATLAB (line 88)
pred_mesh = predict(Md1, dd);

Answers (1)

Yogesh Khurana
Yogesh Khurana on 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!