Clear Filters
Clear Filters

Any idea how to fix fitcsvm code of mine, the program runs in 2016a but as I installed 2018a I get this error of svmtrain saying svmtrain was removed and suggested to use fitcsvm so i changed it to fitcsvm but an error occurred.

3 views (last 30 days)
SPECTF = csvread('ThesisDataW1v2.csv');
labels = SPECTF(:,1);
features = SPECTF(:,2:end);
features_sparse = sparse(features);
libsvmwrite('ThesisDataW1v2SPARSE.csv', labels, features_sparse);
clear all;clc
[label, features] = libsvmread('ThesisDataW1v2SPARSE.csv');
[N,D] = size(features);
labelList = unique(label(:));
NClass = length(labelList);
%-----------------------------------------------------------------------------------
trainIndex = zeros(N,1); trainIndex(1:200) = 1;
testIndex = zeros(N,1); testIndex(1:200) = 1;
trainData = features(trainIndex==1,:);
trainLabel = label(trainIndex==1,:);
testData = features(testIndex==1,:);
testLabel = label(testIndex==1,:);
totalLabel = [trainLabel;testLabel];
totalData = [trainData;testData];
% cross validation scale 1
% This is the big scale (rough)
stepSize = 1;
log2c_list = -4:stepSize:30;
log2g_list = 0:0:0;
numLog2c = length(log2c_list);
cvMatrix = zeros(numLog2c,1);
parfor i = 1:numLog2c
log2c = log2c_list(i);
% -v 3 --> 3-fold cross validation
param = ['-b 1 -t 0 -q -v 3 -c ', num2str(2^log2c)];
cv = fitcsvm(trainLabel, trainData, param);
cvMatrix(i) = cv;
% fprintf('%g %g %g (best c=%g, g=%g, rate=%g)\n', log2c, log2g, cv, bestc, bestg, bestcv)
end
[M,I]= max(cvMatrix(:));
bestLog2c=log2c_list(I);
disp(['CV scale1: best log2c:',num2str(bestLog2c),' accuracy:',num2str(M),'%'])
disp(['CV scale1: best C:',num2str(2^bestLog2c),' accuracy:',num2str(M),'%'])
% Plot the results
figure;
imagesc(cvMatrix); colormap('jet'); colorbar;
set(gca,'XTick', 1)
set(gca,'XTickLabel',log2g_list)
xlabel('');
set(gca,'YTick',1:numLog2c)
set(gca,'YTickLabel',log2c_list)
ylabel('Log_2c');
% cross validation scale 2
% This is the medium scale
%
prevStepSize = stepSize;
stepSize = prevStepSize/2;
log2c_list = bestLog2c-prevStepSize:stepSize:bestLog2c+prevStepSize;
log2g_list = 0:0:0;
numLog2c = length(log2c_list);
cvMatrix = zeros(numLog2c,1);
parfor i = 1:numLog2c
bestcv = 0;
log2c = log2c_list(i);
% -v 3 --> 3-fold cross validation
param = ['-b 1 -t 0 -q -v 3 -c ', num2str(2^log2c)];
cv = fitcsvm(trainLabel, trainData, param);
cvMatrix(i) = cv;
end
[M,I]= max(cvMatrix(:));
bestLog2c=log2c_list(I);
disp(['CV scale1: best log2c:',num2str(bestLog2c),' accuracy:',num2str(M),'%'])
disp(['CV scale1: best C:',num2str(2^bestLog2c),' accuracy:',num2str(M),'%'])
% Plot the results
figure;
imagesc(cvMatrix); colormap('jet'); colorbar;
set(gca,'XTick', 1)
set(gca,'XTickLabel',log2g_list)
xlabel('');
set(gca,'YTick',1:numLog2c)
set(gca,'YTickLabel',log2c_list)
ylabel('Log_2c');
%--------------scale3--------------------------------------------------------------------
prevStepSize = stepSize;
stepSize = prevStepSize/2;
log2c_list = bestLog2c-prevStepSize:stepSize:bestLog2c+prevStepSize;
log2g_list = 0:0:0;
numLog2c = length(log2c_list);
cvMatrix = zeros(numLog2c,1);
parfor i = 1:numLog2c
bestcv = 0;
log2c = log2c_list(i);
% -v 3 --> 3-fold cross validation
param = ['-b 1 -t 0 -q -v 3 -c ', num2str(2^log2c)];
cv = fitcsvm(trainLabel, trainData, param);
cvMatrix(i) = cv;
end
[M,I]= max(cvMatrix(:));
bestLog2c=log2c_list(I);
disp(['CV scale1: best log2c:',num2str(bestLog2c),' accuracy:',num2str(M),'%'])
disp(['CV scale1: best C:',num2str(2^bestLog2c),' accuracy:',num2str(M),'%'])
% Plot the results
figure;
imagesc(cvMatrix); colormap('jet'); colorbar;
set(gca,'XTick', 1)
set(gca,'XTickLabel',log2g_list)
xlabel('');
set(gca,'YTick',1:numLog2c)
set(gca,'YTickLabel',log2c_list)
ylabel('Log_2c');
% ################################################################
% Test phase
% Use the parameters to classify the test set
% ################################################################
param = ['-q -t 0 -c ', num2str(2^bestLog2c),' -b 1'];
bestModel = fitcsvm(testLabel, testData, param);
[predict_label, accuracy, prob_values] = svmpredict(testLabel, testData, bestModel, '-b 1'); % test the training data
supportVectorIndex = bestModel.sv_indices;
%--------------------------------------------------------------------
b=-bestModel.rho;
coef1v2 = [bestModel.sv_coef(1:1391,1)];
supportVectors1v2 = [bestModel.SVs(1:1391,:)];
w1v2 = supportVectors1v2' * coef1v2;
Here's the error as I used svmtrain:
Error using svmtrain (line 230)
svmtrain has been removed. Use fitcsvm instead.
Error in Untitled (line 33)
parfor i = 1:numLog2c
Here's the error as I used fitcsvm:
Error using ClassificationSVM/prepareData (line 623)
The value of X must not be sparse.
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{:});
Error in Untitled (line 33)
parfor i = 1:numLog2c
Hope you could help. Thank you !
  4 Comments
Walter Roberson
Walter Roberson on 27 Dec 2019
fitcsvm() does not permit 'param' option.
You were not using MATLAB's previous SVM routines: you were using the third-party libsvm https://www.mathworks.com/matlabcentral/answers/8704-i-can-not-use-libsvm#answer_12007
In theory you could still install that.
Elbert Abrea
Elbert Abrea on 28 Dec 2019
What should I install sir? The older version of matlab? Since 2018a which I am using does not support svmtrain function.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 30 Dec 2019
Since 2018a which I am using does not support svmtrain function.
That is not exactly true.
Before MATLAB had any SVM support built-in, the third party package libsvm was built and provided a number of interface routines to the libraries and executables they created. One of the function names provided by that third-party package was svmtrain()
Eventually Mathworks added an SVM interface as part of the Statistics Toolbox (in the days before it was named Statistics and Machine Learning Toolbox). They changed the name of most of the functions, but confusingly they too had a function named svmtrain() and that function was subtly incompatible with the libsvm() function.
In time, Mathworks created fitsvm() and related SVM functionality, and started warning that svmtrain() would be removed -- referring to their svmtrain() . Eventually, they did indeed remove their svmtrain() .
When you attempt to run your code now, it sees the svmtrain() call you have, and assumes that it refers to the Mathworks svmtrain() function that was removed, and it warns that svmtrain() does not exist anymore and that you need to use fitcsvm() and related functions.
However, the code you are attempting to run never used Mathwork's svm functions from the Statistics Toolbox: it is older than that, and uses the third-party libsvm tools.
At this point you have two choices:
  1. You can upgrade your code to use fitcsvm() and related functions. It might be a bit of a challenge to exactly match some of the old-old features you were using by way of that '-b 1 -t 0 -q -v 3 -c' option. Some of it can be done if you have enough documentation on libsvm and on the new functionality; OR
  2. You can install the third-party libsvm tools and use those from your existing version of MATLAB. Do not worry about svmtrain supposedly not being supported for this purpose: the svmtrain that is not supported is the Mathworks one, which is one you never used; if you install the third-party libsvm tools then the svmtrain that it provides can be called from current MATLAB. 32 bit vs 64 bit could potentially be a problem, but I believe you can get a 64 bit version of libsvm.
The link https://www.mathworks.com/matlabcentral/answers/8704-i-can-not-use-libsvm#answer_12007 talks about where you can obtain the third-party libsvm tools.

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!