confusionMatrix error, Random Forest Prediction

I am running Churn Analysis on a telecom dataset using the code in the link below:

https://towardsdatascience.com/predict-customer-churn-with-r-9e62357d47b4

While running the following code ( from the Random Forest Prediction and Confusion Matrix section) I get an error:

pred_rf <- predict(rfModel, testing)
caret::confusionMatrix(pred_rf, testing$Churn)

Error:

caret::confusionMatrix(pred_rf, testing$Churn)
Error: data and reference should be factors with the same levels.

Not quite sure I understand the error message and how to fix it. The input dataset is from here:

what is the output of

str(pred_rf)
str(testing$Churn)

?

The output for str(pred_rf) and str(testing$Churn) is:

str(pred_rf)
Factor w/ 2 levels "No","Yes": 1 1 1 2 2 1 1 1 2 2 ...

  • attr(*, "names")= chr [1:2108] "3" "8" "18" "20" ...

str(testing$Churn)
chr [1:2108] "1" "0" "0" "0" "0" "0" "0" "1" "1" "1" "0" "0" "0" "0" "1" "0" "0" ...

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.