Plotting ROC curve for RidgeClassifier in Python

Viewed 867

I want to plot the ROC curve for the RidgeClassifier. But the code comes with an error: I googled for solutions and it comes up to change predict_proba to predict, but it does not work!

predY = classifier.predict_proba(X_test)

Error:

AttributeError: 'RidgeClassifier' object has no attribute 'predict_proba'

This is what I get with predict:

IndexError: too many indices for array
2 Answers

The problem here is that not all scikit-learn classifiers have a predict_proba method, since there is not always a reasonable definition of computed probability for these models. In this case, try with the decision_function method instead:

confidence = classifier.decision_function(X_test)

According to the documentation, a Ridge.Classifier has no predict_proba attribute. This must be because the object automatically picks a threshold during the fit process.

Given the documentation, I believe there is no way to plot a ROC curve for this model. Fortunately, you can use sklearn.linear_model.LogisticRegression and set penalty='l2'. By doing so, you are setting the same optimization problem considered by a RidgeClassifier.

from sklearn.linear_model import LogisticRegression

classifier = LogisticRegression(penalty='l2')
classifier.fit(X, y)
predY = classifier.predict_proba(X_test)

Now you can pass predY to sklearn.metrics.roc_curve.

Related