How to use for loops for hyperparameter tuning using fitcnb

4 views (last 30 days)
I have different types of distribution I want to test for my fitcnb Naive Bayes model to see which is the bestand I'm running into an error
My code is:
%Set different types of distribution dist_1= {'kernel','mvmn','mvmn','kernel','mvmn','mvmn','mvmn','mvmn','mvmn'}; dist_2= {'normal','mvmn','mvmn','normal','mvmn','mvmn','mvmn','mvmn','mvmn'}; dist_3= {'kernel','mvmn','mvmn','normal','mvmn','mvmn','mvmn','mvmn','mvmn'}; dist_4={'normal','mvmn','mvmn','kernel','mvmn','mvmn','mvmn','mvmn','mvmn'}; distributionoptions = [dist_1 dist_2 dist_3 dist_4];
for d = distributionoptions MdlA = fitcnb(Xtrain,Ytrain,'ClassNames', class_names,'Crossval',{'Off'},'DistributionNames',d); end
The error I get is Error using classreg.learning.modelparams.NaiveBayesParams/checkDistributionNames (line 91) 'DistributionNames' value must be a character vector or string scalar, or a string array or cell array whose length equals the number of predictors.

Answers (1)

Don Mathis
Don Mathis on 15 Nov 2018
I think you accidentally concatenated your cell arrays together. Instead use curly braces:
distributionoptions = {dist_1 dist_2 dist_3 dist_4};
After that, inside the loop, d will be a 1x1 cell array, so you need to do d{1} in there:
for d = distributionoptions
MdlA = fitcnb(Xtrain,Ytrain,'ClassNames', class_names,'Crossval',{'Off'},'DistributionNames',d{1});
end

Community Treasure Hunt

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

Start Hunting!