Class names in SVM Leave-one-out cross validation
6 views (last 30 days)
Show older comments
Hi everyone,
I'm currently working on my Bachelor thesis and trying to do LOO cross validation on a dataset I clustered with kmeans. My data Matrix is a 57x707 double called dyn_post_cluster_final and the Matrix which assigns each patient to a cluster is called Index_cluster{i,j} with i being k from kmeans and j being the number of runs. Index_cluster is a 57x1 double.
I want to do cross validation for each pair of clusters, so in this case k = 2: 1 vs. 2 and later k=3 1 vs. 2, 1 vs. 3 and 2 vs. 3
Here is the code:
k = 1;
num_runs = 1;
classification_rate = zeros(57, 1);
Index_cluster_k = Index_cluster{k, num_runs};
for p = find(Index_cluster_k == 1 | Index_cluster_k == 2)
testPatient = dyn_post_cluster_final(p, :);
testCluster = Index_cluster_k(p);
trainPatients = dyn_post_cluster_final(Index_cluster_k == 1 | Index_cluster_k == 2, :);
trainPatients(p, :) = [];
trainClusters = Index_cluster_k(Index_cluster_k == 1 | Index_cluster_k == 2);
trainClusters(p) = [];
trainClusters = categorical(trainClusters);
svmModel = fitcsvm(trainPatients, trainClusters, 'KernelFunction', 'linear', 'ClassNames', [1, 2]);
predictedCluster = predict(svmModel, testPatient);
classification_rate(p) = (predictedCluster == testCluster);
end
accuracy = mean(classification_rate(Index_cluster_k == 1 | Index_cluster_k == 2));
I always seem to incounter the same error which is:
Error using ClassificationSVM.prepareData
No class names are found in input labels.
Error in classreg.learning.FitTemplate/fit (line 246)
this.PrepareData(X,Y,this.BaseFitObjectArgs{:});
Error in ClassificationSVM.fit (line 243)
this = fit(temp,X,Y);
Error in fitcsvm (line 342)
obj = ClassificationSVM.fit(X,Y,RemainingArgs{:});
Error in svm (line 37)
svmModel = fitcsvm(trainPatients, trainClusters, 'KernelFunction', 'linear', 'ClassNames', [1, 2]);
I hope you can help me.
Thanks!
0 Comments
Answers (1)
Pratyush
on 3 Jul 2023
Hi Philip. It is my understanding that you are trying to train an SVM model using fitcsvm method but you get errors related to column names. Since I do not have access to your data, I tried to train an SVM using the fisheriris dataset available on MATLAB in your way. Here is what I think the problem is.
load fisheriris
inds = ~strcmp(species,'setosa');
X = meas(inds,3:4);
y = species(inds);
SVMModel = fitcsvm(X,y,'ClassNames',['versicolor', 'virginica']);
If we try to execute the above cell, we would get similar errors. This happens because the 'ClassNames' attributes expect a cell array containing class names in char array. Below is the correct way to write the above line.
SVMModel = fitcsvm(X,y,'ClassNames',{'versicolor', 'virginica'});
If we change the svmModel line in your code to the following, it may work
svmModel = fitcsvm(trainPatients, trainClusters, 'KernelFunction', 'linear', 'ClassNames', {'class1','class2'});
You may replace class1 and class2 with actual names of your dataset classes. Hope this helps.
Here are some docs to help you understand more on the fitcsvm method.
0 Comments
See Also
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!