ROC in Multiclass kNN

Andres Mora

Im trying to run some ROC analysis on a multiclass knn model and dataset

so far i have this code for the kNN model. It works well.
X_train_new is a dataset with 131 numeric variables (columns) and 7210 observations.
Y_train is the outcome variable which i have as factor. its a dataset with only 1 column (activity) and 7210 observations (there are 6 possible factors)

ctrl <- trainControl(method  = "cv",
                     number  = 10)

model2 <-    train(X_train_new,
                   Y_train$activity,
                   method     = "knn",
                   tuneGrid   = expand.grid(k = 5),
                   trControl  = ctrl,
                   metric     = "Accuracy"
)

X_test_new is a dataset with 131 numeric variables (columns) and 3089 observations.
Y_test is the outcome variable which i have as factor. its a dataset with only 1 column and 3089 observations (there are 6 possible factors)

I run the predict function

knnPredict_test <- predict(model2 , newdata = X_test_new )

I would like to do some ROC analysis on each class vs all. Im trying

a = multiclass.roc ( Y_test$activity, knnPredict_test )

knnPredict_test is a vector with predicted classes:

knnPredict_test <- predict(model2 ,newdata = X_test_new )
> length(knnPredict_test)
[1] 3089
> glimpse(knnPredict_test)
 Factor w/ 6 levels "laying","sitting",..: 2 1 5 1 3 2 4 5 3 2 ...

This is the error im getting

Error in roc.default(response, predictor, levels = X, percent = percent,  :   
Predictor must be numeric or ordered.
Paul

To get the ROC, you need a numeric prediction. However, by default predict will give you the predicted classes. Use type = "prob".

Here is a reproducable example which has the same error.

library(caret)

knnFit <- train(
  Species ~ .,
  data = iris,
  method = "knn"
)

predictions_bad <- predict(knnFit)

pROC::multiclass.roc(iris$Species, predictions_bad)
#> Error in roc.default(response, predictor, levels = X, percent = percent,  : 
#>   Predictor must be numeric or ordered.

Using type = "prob" fixes the error.

predictions_good <- predict(knnFit, type = "prob")

pROC::multiclass.roc(iris$Species, predictions_good)
#> Call:
#> multiclass.roc.default(response = iris$Species, predictor = predictions_good)
#> 
#> Data: multivariate predictor predictions_good with 3 levels of iris$Species: setosa, versicolor, virginica.
#> Multi-class area under the curve: 0.9981

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sklearn: ROC for multiclass classification

sklearn multiclass roc auc score

ROC_Curve multiclass format is not supported

ROC curve and AUC for a multiclass and multilabel problem. ValueError: Target scores need to be probabilities for multiclass roc_auc

How to plot ROC curve with scikit learn for the multiclass case?

Cross_val_score is not working with roc_auc and multiclass

Getting error while trying to plot ROC for multiclass classification

How can I add a title to multiclass.roc plots in pROC?

ValueError: multiclass format is not supported on ROC_Curve for text classification

'Multiclass-multioutput is not supported' Error in Scikit learn for Knn classifier

h2o Flow UI: Build Model but no ROC CURVE or AUC for multiclass?

ValueError: multiclass-multioutput format is not supported using sklearn roc_auc_score function

ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets in ROC curve calculation

R: Multiclass Matrices

Multiclass Text Classification in Python

Multiclass Classification with xgboost in R

variable importance in multiclass

Multiclass classification or regression?

Multiclass Classification with LightGBM

Multiclass classification on iris dataset

sklearn metrics for multiclass classification

Multiclass classification with xgboost classifier?

How to create a multiclass dataset?

ValueError: multiclass format is not supported

sklearn multiclass svm function

Spark Multiclass Classification Example

Duplicate `operationId` with Multiclass API

Keras LSTM multiclass classification

Multiclass Neural Network Issue