[英]Confusion Matrix Error: Error: `data` and `reference` should be factors with the same levels
[英]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.