9
$\begingroup$

I am a 10th grade student working on a binary classification problem and I have decided to use the logistic regression model from Scikit-Learn. I am looking to predict patient adherence given the time of day, day of week, or both. I have simulated data and have made it so that certain timeslots have many more 0s (meaning the patient did not take the medicine) to simulate a trend, but my model is still predicting "1" for every single input. I believe my data is very imbalanced and without any class weights, the model puts every input into the "1" class. Obviously, this results in terrible accuracy, AUC and everything in between. Sklearn does have a class_weight parameter, but since that is dichotomous and only gives the "balanced" option, it really does not help and in some cases makes accuracy far worse than just assuming everything to be in the 1 class. I think changing the threshold to 0.75 will work, given what I have seen from the data, but I can't find anything about adjusting the threshold in any documentation.

Is there anyway to adjust this threshold? Or any other way to deal with my imbalanced data?

Let me know if you want me to elaborate on the specifics of my data.

$\endgroup$
2
  • 4
    $\begingroup$ You should give more details on the context! What are you trying to classify? Why? Also, a "probability threshold" is not part of a logistic regresson model, read logistic regression is not classification $\endgroup$ Commented Dec 29, 2018 at 9:32
  • $\begingroup$ Or instead of using a threshold you can assign weights to the classes. $\endgroup$
    – skan
    Commented Jan 11, 2023 at 17:34

2 Answers 2

15
$\begingroup$

There is almost never a good reason to do this! As Kjetil said above, see here.

You should be able to get the probability outputs from ‘predict_proba’, then you can just write

decisions = (model.predict_proba() >= mythreshold).astype(int)

Note as stated that logistic regression itself does not have a threshold. However sklearn does have a “decision function” that implements the threshold directly in the “predict” function, unfortunately. Hence they consider logistic regression a classifier, unfortunately.

$\endgroup$
2
  • 3
    $\begingroup$ Good answer. I took the liberty of putting your snipped into a code block, and (a bit more borderline of an edit) changed your * 1 to astype(int) since it's a bit more self explanatory. Feel free to edit back if you prefer the * 1. $\endgroup$ Commented Dec 31, 2018 at 1:55
  • 2
    $\begingroup$ Little note: predict_proba() returns an array of shape (n_data_rows, n_classes). Thus, for binary classification you get a shape of (n_data_rows, 2). If you apply the threshold as above, you're not applying it on the target class. As Wenyi Yan has shown below, you will have to select it by model.predict_proba()[:, 1] (sklearn sorts the classes - The target is usually =1 and, thus, will be on the second position of the predict_proba()). $\endgroup$
    – Markus
    Commented Jul 2, 2021 at 15:03
3
$\begingroup$

Let's say that your customized threshold is 0.6. It should be:

y_pred_new_threshold = (logreg.predict_proba(X_test)[:,1]>=0.6).astype(int)
$\endgroup$

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.