I am working on a binary classification problem where it is much more important to not have false positives; quite a lot of false negatives is ok. I have used a bunch of classifiers in sklearn for example, but I think none of them have the ability to adjust the precision-recall tradeoff explicitly (they do produce pretty good results but not adjustable).

What classifiers have adjustable precision/recall? Is there any way to influence the precision/recall tradeoff on standard classifiers, eg Random Forest or AdaBoost?


3 Answers 3


Almost all of scikit-learn's classifiers can give decision values (via decision_function or predict_proba).

Based on the decision values it is straightforward to compute precision-recall and/or ROC curves. scikit-learn provides those functions in its metrics submodule.

A minimal example, assuming you have data and labels with appropriate content:

import sklearn.svm
import sklearn.metrics
from matplotlib import pyplot as plt

clf = sklearn.svm.LinearSVC().fit(data, labels)
decision_values = clf.decision_function(data)

precision, recall, thresholds = sklearn.metrics.precision_recall_curve(labels, decision_values)

plt.plot(recall, precision)
  • $\begingroup$ Perfect, thank you! Not sure how I missed that :) $\endgroup$
    – Alex I
    Commented Mar 6, 2015 at 4:46
  • $\begingroup$ Looks like precision_recall_curve calculate the whole F1. How to only calculate the negative ones? $\endgroup$
    – Mithril
    Commented Jun 29, 2017 at 6:50

I have just solved this for myself before bumping into this Q so I have decided to share my solution.

It uses the same approach that Marc Claesen has proposed but answers the actuall question on how to adjust the classifier to move higher on precision axis trading off the recall.

X_test is the data and y_test are the true labels. The classifier should be fitted already.

y_score = clf.decision_function(X_test)


min_prcsn=0.25 # here is your precision lower bound e.g. 25%
min_thrshld=min([thrshld[i] for i in range(len(thrshld)) if prcsn[i]>min_prcsn])

And this is how you'd use the newly learne d minimal threshold to adjust your prediction (that you would otherwise just get calling predict(X_test))

y_pred_adjusted=[1 if y_s>min_thrshld else 0 for y_s in y_score]

Would be great to hear your feedback on this adjustment recipe.


Here is function


which produces class probabilities. Then use various metrics to adjust threshold, P, which you use in decision by following pseudo-code

if p(y)>P, then y=1 else y=0 endif


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.