# Why real-world output of my classifier has similar label ratio to training data?

I trained a neural network on balanced dataset, and it has good accuracy ~85%. But in real world positives appear in about 10% of the cases or less. When I test network on set with real world distribution it seems to assign more positive labels than needed tending to balanced proportion as in training set. What can be the reason of such behavior and what should I do solve it? I'm using Keras and combination of LSTM and CNN layers.

• Would you elaborate the clause But in real world positives appear in about 10%. By positive you've meant positive labels or something? Apr 7 '19 at 12:17
• @Vaalizaadeh, yes positive labels, so basically one category is less represented in real world.
– Bien
Apr 7 '19 at 12:34

What can be the reason of such behavior?

A classifier only tries to capture the features-label relationship as accurate as it can, it does not learn nor does it guarantee that the ratio of predicted labels to be close to the true ratio. However, if sampled classes (even balanced) are good representatives of true class regions and classifier finds good decision boundaries, the closeness of ratios will happen naturally. Therefore, the explanation is that the sampled negative class is not a good representative of its true occupied regions, meaning some regions are not sampled well, and/or classifier is finding bad decision boundaries. Here is a visual illustration (drawn by myself):

In the case of good training set, predictions resemble the real-world ratio (2:1), even though model is trained on a balanced set (1:1). In the case of bad training set and/or bad decision boundary, predictions are completely incompatible (1:2) with the real-world ratio (2:1).

What should I do to solve it?

1. If the problem is related to bad negative representatives

1. Use more negative samples in the training set, i.e. imbalanced training (you can nullify the effect of imbalanced samples with class weights), or

2. Train with balanced classes but change the decision threshold (a post-training solution). That is, instead of assigning instances with output > 0.5 to positive class, use a harder-to-pass threshold like output > 0.8 to decrease the number of positive predictions.

2. If the problem is related to classifier performance, we should come up with a better classifier which is an open-ended endeavor.

However, in my opinion, you should not select models based on the ratio of positive predictions. You should ecide based on a metric like macro-f1 (or any other one). Therefore, by using a validation set, a model that produces more positive samples and has a higher macro-f1 should be preferred over a model that produces less positives but has a lower macro-f1.

EDIT:

As @BenReiniger pointed out in another post, a hidden assumption here (specially in the sketch) is that classes are "clearly separable". This assumption becomes more justified in higher dimensions. As an example, dogs and cats are clearly separable based on their images (high dimensions) compared to their length (one dimension).

• I like that illustration! Source? Apr 7 '19 at 21:24
• @BenReiniger Thanks Ben! I drew it by paint. Apr 7 '19 at 23:22
• @Esmailian, I would like to say that this is something that applies to many of your illustrations. They look like something out of a write-up in a blog. You are a natural teacher :) Apr 8 '19 at 10:20
• @SimonLarsson Thanks man! Apr 8 '19 at 10:51

Esmailian's answer is great, another possible solution is intercept correction, it is more commonly used in logistic regression, but the principal will apply here as well, see here or here, there's a lot of material on it online...