What is Decision Threshold ?
sklearn does not let us set the decision threshold directly, but it gives us the access to decision scores ( Decision function o/p ) that is used to make the prediction.
We can select the best score from decision function output and set it as Decision Threshold value and consider all those Decision score values which are less than this Decision Threshold as a negative class ( 0 ) and all those decision score values that are greater than this Decision Threshold value as a positive class ( 1 ).
Using Precision-Recall curve for various Decision Threshold values, we can select the best value for Decision Threshold such that it gives High Precision ( Without affection Recall much ) or High Recall ( Without affecting Precision much ) based on whether our project is precision-oriented or recall-oriented respectively.
Precision Recall Curve
The curve is plots values of precision scores (y-axis) against those of recall scores (x-axis) and these values are plotted at various probability thresholds.
There can be two ways of obtaining a more optimal probability threshold for the positive class:
- Minimize the difference between precision and recall scores
- Select the probability threshold of which precision and recall scores are closest to each other
- Euclidean Distance
- The most optimal point on the PR curve should be (1,1), i.e. precision and recall scores of 1.
- Select the probability threshold as the most optimal one if precision and recall scores are closest fo the ones mentioned in the previous point in terms of Euclidean distance, i.e.
# Plot Precision-Recall curve using sklearn.
from sklearn.metrics import precision_recall_curve
precision, recall, threshold = precision_recall_curve(y_test, decision_function)
# Plot the output.
plt.plot(threshold, precision[:-1], c ='r', label ='PRECISION')
plt.plot(threshold, recall[:-1], c ='b', label ='RECALL')
plt.grid()
plt.legend()
plt.title('Precision-Recall Curve')
Here in the above plot, we can see that if we want high precision value, then we need to increase the value of decision threshold ( x-axis ), but which would decrease the value of recall ( which is not favourable). so we need to choose that value of Decision Threshold which would increase Precision but not much decrease in Recall. One such value form the above plot is around 0.6 Decision Threshold.
Comments