2
votes

I need to have multiple confusion matrix at a different threshold for a binary classifier.

I have look up everywhere but could not find an easy implementation for this.

Can anyone provide a way to set the scikit-learn's confusion matrix threshold?

I understand scikit-learn's confusion_matrix uses 0.5 as threshold.

model = LogisticRegression(random_state=0).fit(X_train, y_train)
y_pred = model.predict(X_test)
confusion_matrix(y_test, y_pred)
Output: array([[24705,     8],
              [  718,     0]])

Thanks!

1
What do you mean by a threshold for a confusion matrix? Please show an example of the input and output you expect.Mathias Müller
@MathiasMüller i have added the code snippet, basically i am using scikitlearn's confusion matrix method but how to change its threshold?Mel

1 Answers

8
votes

I figured it out, simply:

threshold = 0.2
y_pred = (model.predict_proba(X_test)[:, 1] > threshold).astype('float')
confusion_matrix(y_test, y_pred)

Hope this helps for everyone else looking at a simple way to change the threshold!