Can I create an array of classification models?

3 次查看(过去 30 天)
I would like to use a loop to obtain an array of classification models, and then loop through this array, using this trained model one by one to make classification. Can I do that?
For example: I was working on SVM problems and doing the following things:
for i = 1: 6
mds(i) = fitcsvm(X, y, 'Standardize',true, 'KernelFunction', 'linear', 'boxconstraint', C, 'ClassNames', [0, 1]) ;
end
I got error "You cannot assign to an object of class double using () indexing."

回答(1 个)

Shivam Chaturvedi
Shivam Chaturvedi 2016-2-29
Hi Dejun,
As this is an object, there are 2 methods that you can try to make an array of the model object:
1. Append the new object to an existing list
ex:
models = [];
for i=1:6
thisModel = fitcsvm(...);
models = [models thisModel];
end
This will concatenate the 'thisModel' variable every time to the list giving you an array of models.
2. Use a cell array
For this, you would just need to replace
mds(i)
to
mds{i}
This should work ideally in its own. You can also initialize mds as:
mds = {}
before the start of the loop.

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by