I’ve just realized something pretty annoying: doing Logistic Regression with a binary target to predict, I thought first that I was obtaining not so bad results (though overfitting a bit at first glance):

But at looking further, something was definitely going wrong:

Indeed, 78% of predicted outputs are good, but in fact the model is not able at all to classify correctly the 1 outputs.

How do you deal with this kind of issue? Did it happen to you before? Did you rely in the past on mean accuracy like me before realizing it was a “trap” ?

It’s not really “mean accuracy” but just accuracy. Imagine that you have 100 lines of data coming in 99 - 1, and 1 - 0. Your model has predicted 1 for all values. That is, it has an accuracy of 99% when it was wrong once. This is most noticeable in data unbalance, as in your case, one value greatly outweighs the other.

It is for this reason that accuracy is a poor metric for model evaluation.

Precision" and “recall” metrics will be important to you.

Methods to combat such errors, try to teach the model on balanced data.
Another is the change of the probability threshold at which the model refers to a value of 0 or 1.

Note that all my features (n = 62) are binary features.

Indeed, the 1 recall score is very bad, so I guess I should focus only on recall metric or f1-score. “Precision” is missleading here.

I didn’t try to change the threshold, but it’s a bit arbitrary, don’t you think ? It means, if I understand well, that I could force the model to ouput “1” if proba(1) >=0.3 instead of 0.5, for example? Fine, but it’s a kind of supervision that looks strange to me, because I couldn’t find any “objective” reason to fix the threshold at 0.3.

Since 0 output weight = 76% in the dataset, p = 0.76, so maybe it makes sense from a logical point of view to fix the threshold at 1 - p = 0.24 ? I am afraid this could lead to overfitting.

Study the articles on Precision-Recall Curves, it will help you better define the threshold parameter, as well as explore the approaches that are used for this.

If in simple language, then you are right this observation. You can see that due to unbalanced data, the model tends to underestimate the probability values for 1 very much. So you lower the threshold, allowing it to predict 1 more often.

But it is obvious that if you lower the threshold too much, there will be a lot of errors associated with 0, so you need to find balance. To do this, use Precision-Recall Curves.

The retraining error is best seen when you check your model on the test dataset. If the metrics for the test dataset are much worse than for the train and validation, then the model has been overfitted.

Thanks for all this Max, I will check Precision-Recall curves and post my results later. Note that my scores with training and testing datasets are not so bad, the recall score becomes bad when I apply the model to the whole dataset.

I understand a bit better why I should use the precision-recall curve instead of ROC curve since the calculation for the former doesn’t take in account the true negatives. Though I am still a bit confused with true/false positive/negative terminology.

Reviewing both precision and recall is useful in cases where there is an imbalance in the observations between the two classes. Specifically, there are many examples of no event (class 0) and only a few examples of an event (class 1).

The reason for this is that typically the large number of class 0 examples means we are less interested in the skill of the model at predicting class 0 correctly, e.g. high true negatives.

Key to the calculation of precision and recall is that the calculations do not make use of the true negatives. It is only concerned with the correct prediction of the minority class, class 1.

Indeed, this is exactly the case here. I am above all interested in the correct prediction of the minority class-1.

Note that I cannot import plot_precision_recall_curve from scikit.metrics, I got an error!
So, I finally managed to plot the precision-recall curve using the code contained in the tutorial.

As for understanding the concepts of “false positive” and “false negative”, you can use the following description.

Imagine that you have developed a test to determine the disease.

The test reports that the patient is healthy, but he is sick is a false-positive result (0, 1).

The test reports that the patient is sick and is sick is a true-positive result (1, 1)

The test reported that the patient was sick, but in fact, he was healthy - this was a false-negative result (1, 0).

The test showed that the patient is healthy and healthy, which is a true-negative result (0, 0).

At the end of each example, I gave what it looks like in a binary classifier where the 1-st number of predictions and the second expected result.

Concerning the reading of the chart. When your model does not have 100% accuracy, this behavior is perfectly normal. It’s worth remembering that the measurements on the graph correspond to the change of the probability threshold, which we talked about earlier. So you can read the graph from right to left.
The Recall is equal to 1, only if there are no false negatives, this is possible with a probability threshold of 0.0 when all probability values are greater than or equal to 0.0, so there will be no 1 value that was predicted as 0. Since absolutely all predictions are equal to 1
Precision is 1, only when there are no false positives, this is possible at probability threshold 1.0.

Now let’s read one of the points on the graph. If we take the Recall point 0.6 and Precision point 0.78 it will mean the following. With a probability threshold of about 0.4, we will correctly predict 1 in 60% of the total number of expected 1. In this case, the accuracy of prediction 1 will be 78%, that is, in 22% of cases, we take 0 for 1.

It may be easier for you to read the graph if you change it by postponing the x-axis probability thresholds. And on the y-axis of the graph, you can read the chart with recall and precision.

Next, you need to determine the balance of these indicators for your task. Or you can continue to work on improving the hyperparameters of the model.

I’m not sure that I was able to explain these concepts enough.

Thanks again, your explanations are always well structured and super interesting! I think we are really developping a great discussion about something important!

Note that _ shape is only 783 when lr_precision and lr_recall shape is 784, do you know why?

Let’s say know I decide to select 0.8 threshold as optimal, how then can I force the model to act accordingly? I mean there is an implicit with the predict logistic regression method : f(x) > 0.5 where f is the decision_function and 0.5 the threshold value by default. Manually, this is easy to do, but I guess this is not the best way.

Also, I am very interesting to plot (with imshow) for analyse purpose the coef_ attributes of the features in the decision function. If I change the threshold manually, it will not affect the coef_ right ? That is, I am not sure to understand very well the relation, if there is one, between the coef_ and the probability threshold since there is no clear way as far I know to “redo” the model with a new “built-in” threshold different than 0.5 (not sure I explain myself very well here). Since I am searching for heuristics easy to memorize for humans, it’s very important to extract clear general rules from the features that best predict the positive class and that’s why the _coef are so importants.

Unfortunately, I can’t imagine what caused the difference in size. But it doesn’t affect the shape of the chart anyway.

In other questions I recommend you to read these discussions on the stack overflow. They discuss a similar situation.

And with simple words from me. Then if you want to try to retrain the model using this knowledge. You should make several models with different values for class_weight and compare the results. It should affect _coef as well.

But when you have already trained the model, there is nothing wrong with writing a prediction function with a change in threshold. You can see examples in the links I gave you.

Playing with class_weight as you said, I faced the following behavior after applying the model to (x,y) : the precision increases a lot indeed, but the false negatives too, and above all, I lost nearly 100% of the true positives!

But finally, after doing more researches, I tried LogisticRegressionCV (built-in cross-validation support) and got promising results:

I completed my model adding other features that wasn’t making any significative difference since now. But this is not the case anymore. The new features changed everything!

Can’t believe it. Machine learning is really surprising sometimes, you think you are stucked and suddenly you make a giant step !