繁体   English   中英

什么地方出了错? 错误:`data` 和 `reference` 应该是具有相同水平的因素

[英]What went wrong? Error: `data` and `reference` should be factors with the same levels

> # Check results > model_knn$results k Accuracy Kappa AccuracySD KappaSD 1 5 0.9632391 0.9439746 0.02452539 0.03727995 2 7 0.9699800 0.9544974 0.02451292 0.03708112 3 9 0.9677304 0.9509734 0.02617121 0.03986928 > # Predict the labels of the test set > predictions<-predict.train(object=model_knn,iris_norm.test[,1:4], type="raw") > > # Evaluate the predictions > table(predictions) predictions Iris-setosa Iris-versicolor Iris-virginica 12 14 10 > > #confusion matrix > > # ENTER YOUR CODE HERE > confusionMatrix(predictions,iris_norm.test[,5]) Error: `data` and `reference` should be factors with the same levels.

如果没有模型或数据来重现您的情况,我只能建议在将两个向量传递到confusionMatrix forcats::fct_unify之前使用forcats::fct_unify对齐因子级别:

library(forcats)
library(caret)

do.call(
  confusionMatrix,
  fct_unify(list(
    data = predictions,
    reference = iris_norm.test[,5]
  ))
)

fct_unify处理因子向量列表,并确保它们共享相同的一组级别。 使用与confusionMatrix do.call的预期参数相对应的名称构造该列表,我可以将它直接传递给do.call

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM