Grid search in svm

11 次查看(过去 30 天)
SP
SP 2022-7-20
Hi,
I am having training data (train.mat) and testing data (test.mat), I need to perform grid search in this. How can I do svm training with this? Kindly help me with this. Thanks in advance.
  2 个评论
Walter Roberson
Walter Roberson 2022-7-20
What is to be searched by the grid method?
SP
SP 2022-7-20
I need to do svm hyperparameter tuning using grid search method.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2022-7-20
Nbox = 15; %change these as desired
Nkern = 10; %change these as desired
bcvec = logspace(-3, 3, Nbox);
ksvec = logspace(-3, 3, Nkern);
goodness = zeros(Nbox, Nkern);
for ksidx = 1 : Nkern
ks = ksvec(ksidx);
for bcidx = 1 : Nbox
bc = bcvec(bcidx);
mdl = fitcsvm(Training_Data, Training_Class, ...
'Standardize', true, ...
'KernelFunction','rbf', ...
'BoxConstraint', bc, ...
'KernelScale', ks);
predictions = predict(mdl, Test_Data);
correct_matches = nnz(predictions == Test_Class);
goodness(bcidx, ksidx) = correct_matches;
end
end
best_score = max(goodness(:));
[bcidx, ksidx] = find(goodness == best_score);
best_bc = bcvec(bcidx);
best_ks = ksvec(ksidx);
best_fits = table(best_bc, best_ks, 'VariableNames', {'BoxConstraint', 'KernelScale'});
The output will be a table, best_fits, listing all of the combinations of BoxConstraint and KernelScale that together lead to the best scores discovered, where "score" here is determined [in this code] solely by the number of correct matches.
You would have to change how the goodness is calculated if you wanted to be concerned about aspects such as balancing the accuracies of the various classes. With the current code, if it was (for example) 100% successful in matching the largest class, and 0% successful in all of the other classes, then the goodness calculated by this code might be as high as it gets, even though it might be terrible for the other classes. You would probably be better off calculating the goodness some other way.
  6 个评论
SP
SP 2022-7-21
I have attached my mat files. It consists of matrix values. The mat files consists of 30 columns in which first 15 columns (1 to 15 columns) belongs to Class_1 and rest of the 15 columns (16 to 30 columns) belongs to Class_2.
Walter Roberson
Walter Roberson 2022-7-22
temp = load('train.mat');
Training_Data1 = temp.finaltrain(:,1:end/2);
Training_Data2 = temp.finaltrain(:,end/2+1:end);
Training_Data = [Training_Data1; Training_Data2];
Training_Class = [ones(size(Training_Data1, 1), 1); 2*ones(size(Training_Data2,1),1)];
temp = load('test.mat');
Test_Data1 = temp.finaltest(:,1:end/2);
Test_Data2 = temp.finaltest(:,end/2+1:end);
Test_Data = [Test_Data1; Test_Data2];
Test_Class = [ones(size(Test_Data1, 1), 1); 2*ones(size(Test_Data2,1),1)];
Nbox = 25; %change these as desired
Nkern = 20; %change these as desired
bcvec = logspace(-3, 3, Nbox);
ksvec = logspace(-3, 3, Nkern);
goodness = zeros(Nbox, Nkern);
wb = waitbar(0, 'please wait, processing kernel scales');
cleanMe = onCleanup(@() delete(wb));
for ksidx = 1 : Nkern
waitbar(ksidx/Nkern, wb);
ks = ksvec(ksidx);
for bcidx = 1 : Nbox
bc = bcvec(bcidx);
mdl = fitcsvm(Training_Data, Training_Class, ...
'Standardize', true, ...
'KernelFunction','rbf', ...
'BoxConstraint', bc, ...
'KernelScale', ks);
predictions = predict(mdl, Test_Data);
correct_matches = nnz(predictions == Test_Class);
goodness(bcidx, ksidx) = correct_matches;
end
end
clear cleanMe %get rid of waitbar
best_score = max(goodness(:));
[bcidx, ksidx] = find(goodness == best_score);
best_bc = bcvec(bcidx);
best_ks = ksvec(ksidx);
best_fits = table(best_bc, best_ks, 'VariableNames', {'BoxConstraint', 'KernelScale'});
best_fits

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Genomics and Next Generation Sequencing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by