无法分类

时间:2015-05-21 21:07:03

标签: matlab knn

我是matlab的新手,我想进行10次交叉验证,然后对矩阵进行分类,然后显示其ROC。

indices = crossvalind ('KFold', labels, 10); 

cp= classperf (labels);

for i= 1:10 
   test= find (indices == i);
   train= find (indices ~= i);
   class =fitcknn (ANorm, labels);
   classperf (cp, class, test);
end

cp.errorRate;

plotroc(class,labels);

但是,我不断收到这些错误:

Error using classreg.learning.internal.DisallowVectorOps/subsref (line 16)
You cannot index into an object of class ClassificationKNN using () indexing.

Error in classperf (line 219)
gps = varargin{1}(:);

Error in M (line 108)
classperf (cp, class, test);

有人可以告诉我为什么会发生这些错误吗?

1 个答案:

答案 0 :(得分:1)

您应该使用predict方法。我没有fitcknn,因此我使用了ClassificationKNN.fit。您在代码中获得的class对象不能与classperf一起使用。你应该发送数字/字符串标签。因此错误。

labels=[zeros(100,1);ones(100,1)];
ANorm=[1+2*randn(100,30);4+1.5*randn(100,30)];

indices = crossvalind ('KFold', labels, 10); 
cp=classperf(labels);

for i= 1:10 
   test=  (indices == i);
   train=  (indices ~= i);
   labelsTest=labels(test);
   mdl =ClassificationKNN.fit (ANorm, labels);
   labelsPredict = predict(mdl,ANorm(test,:));
   classperf(cp,labelsPredict,test);
end

cp.errorRate