简体   繁体   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.

Without the model or data to reproduce your case, I can only suggest to align the factor levels using forcats::fct_unify before passing the two vectors into confusionMatrix :如果没有模型或数据来重现您的情况,我只能建议在将两个向量传递到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 works on a list of factor vectors and makes sure they all share the same set of levels. fct_unify处理因子向量列表,并确保它们共享相同的一组级别。 Constructing that list with names corresponding to the expected arguments of confusionMatrix , I can pass it right into with do.call .使用与confusionMatrix do.call的预期参数相对应的名称构造该列表,我可以将它直接传递给do.call

暂无
暂无

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

相关问题 混淆矩阵错误:错误:`data`和`reference`应该是具有相同水平的因子 - Confusion Matrix Error: Error: `data` and `reference` should be factors with the same levels ConfusionMatrix 错误:`data` 和 `reference` 应该是具有相同水平的因素 - ConfusionMatrix Error: `data` and `reference` should be factors with the same levels r - 错误:`data` 和 `reference` 应该是具有相同水平的因素 - r - Error: `data` and `reference` should be factors with the same levels 使用混淆矩阵`data`和`reference`的错误应该是具有相同水平的因素 - error using confusionMatrix `data` and `reference` should be factors with the same levels confusionMatrix - 错误:`data` 和 `reference` 应该是具有相同水平的因素 - confusionMatrix - Error: `data` and `reference` should be factors with the same levels 应该是具有相同水平,误差和参考的因素 - should be factors with the same levels, error and reference Adaboost:混淆矩阵的问题 - `data` 和 `reference` 应该是具有相同水平的因素 - Adaboost: Problem with confusion matrix - `data` and `reference` should be factors with the same levels 错误:`data` 和 `reference` 应该是具有相同水平的因素。 使用混淆矩阵(插入符号) - Error: `data` and `reference` should be factors with the same levels. Using confusionMatrix (caret) R:RF模型中的混淆矩阵返回错误:数据和“参考”应该是具有相同水平的因子 - R: Confusion matrix in RF model returns error: data` and `reference` should be factors with the same levels 错误:`data` 和 `reference` 应该是具有相同级别的因子&#39;不返回混淆矩阵 - Error: `data` and `reference` should be factors with the same levels' doesn't return confusion matrix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM