问题描述
我想在 Matlab 中使用 carsmall
数据制作一个朴素贝叶斯模型。
这是我的代码:
load carsmall
car = table(Model_Year,Weight);
naive_model = fitcnb(car,Origin)
使用 ClassificationNaiveBayes/findNoDataCombos 时出错(第 256 行)
正态分布不能适合意大利类和预测变量 Model_Year 的组合。数据的方差为零。
解决方法
由于“Italy”的情况只出现一次就没有方差,正态分布对于这种情况是没有意义的。这会导致 fitcnb 出现错误,删除此元素可以正常工作。 我建议你组织更多你的代码,也许你以后打算这样做,但这是一个很好的做法。所以,这里是包含更多细节的新代码。
clear all
load carsmall
X = [Model_Year Weight];
Y = cellstr(Origin);
%The next line helps to see how many classnames you have
tabulate(Y);
Y(36)=[]; %removing the only case of italy
X (36,:)=[];%removing the only case of italy
%Train a naive Bayes classifier. It is good practice to specify the class order.
naive_model = fitcnb(X,Y,'ClassNames',{'USA','France','Japan','Germany','Sweden'});