55
$\begingroup$

Suppose I want to learn a classifier that predicts if an email is spam. And suppose only 1% of emails are spam.

The easiest thing to do would be to learn the trivial classifier that says none of the emails are spam. This classifier would give us 99% accuracy, but it wouldn't learn anything interesting, and would have a 100% rate of false negatives.

To solve this problem, people have told me to "downsample", or learn on a subset of the data where 50% of the examples are spam and 50% are not spam.

But I'm worried about this approach, since once we build this classifier and start using it on a real corpus of emails (as opposed to a 50/50 test set), it may predict that a lot of emails are spam when they're really not. Just because it's used to seeing much more spam than there actually is in the dataset.

So how do we fix this problem?

("Upsampling," or repeating the positive training examples multiple times so 50% of the data is positive training examples, seems to suffer from similar problems.)

$\endgroup$
0

6 Answers 6

47
$\begingroup$

Most classification models in fact don't yield a binary decision, but rather a continuous decision value (for instance, logistic regression models output a probability, SVMs output a signed distance to the hyperplane, ...). Using the decision values we can rank test samples, from 'almost certainly positive' to 'almost certainly negative'.

Based on the decision value, you can always assign some cutoff that configures the classifier in such a way that a certain fraction of data is labeled as positive. Determining an appropriate threshold can be done via the model's ROC or PR curves. You can play with the decision threshold regardless of the balance used in the training set. In other words, techniques like up -or downsampling are orthogonal to this.

Assuming the model is better than random, you can intuitively see that increasing the threshold for positive classification (which leads to less positive predictions) increases the model's precision at the cost of lower recall and vice versa.

Consider SVM as an intuitive example: the main challenge is to learn the orientation of the separating hyperplane. Up -or downsampling can help with this (I recommend preferring upsampling over downsampling). When the orientation of the hyperplane is good, we can play with the decision threshold (e.g. signed distance to the hyperplane) to get a desired fraction of positive predictions.

$\endgroup$
7
  • $\begingroup$ Thanks, that was very helpful. How do you know what the threshold should be? Do you want to set the threshold so that the proportion of positive predictions is equal to the proportion of positive examples in the population? $\endgroup$
    – Jessica
    Commented Nov 2, 2014 at 21:20
  • 2
    $\begingroup$ @Jessica As I mentioned, a convenient way of choosing the threshold is via receiver operating characteristic (ROC) curves. Every threshold corresponds to a point in ROC space. When you plot the curve you can choose a threshold based on what fits your specific needs. (you could also use precision-recall curves as an alternative) $\endgroup$ Commented Nov 2, 2014 at 21:34
  • $\begingroup$ I do not agree that the distance of a test point to the hyperplane learned by a SVM is any measure of confidence of the prediction. There has been efforts to make SVM output prediction confidences. Look up Platt scaling for example. But it does not do as well as Gaussian process classification (wrt prediction confidence). $\endgroup$
    – Leila
    Commented Jan 13, 2015 at 21:55
  • 1
    $\begingroup$ @Seeda Platt scaling is about shoe-horning decision values into probabilities. Platt scaling is as simple as running (scaled) decision values through the logistic function, which is monotonically increasing and hence does not affect rankings whatsoever (= confidence). All it does is map the output from $\mathbb{R}$ to $[0,1]$. $\endgroup$ Commented Jan 13, 2015 at 21:56
  • $\begingroup$ @MarcClaesen I am not suggesting to use Platt scaling; it is an "attempt" to generate prediction confidences but there are better alternatives. All I am saying is that using the distance to the hyperplane is not meaningful and reviewing the literature, I have never come across it even though it is the first thing that comes to one's mind trying to generate confidences out of an SVM. $\endgroup$
    – Leila
    Commented Jan 13, 2015 at 22:05
15
$\begingroup$

The real problem here is your choice of metric: % accuracy is a poor measure of a model's success on an un-balanced dataset (for the exactly reason you mention: it's trivial to achieve 99% accuracy in this case).

Balancing your dataset before fitting the model is a bad solution as it biases your model and (even worse) throws out potentially useful data.

You're much better off balancing your accuracy metric, rather than balancing your data. For example you could use balanced accuracy when evaluating you model: (error for the positive class + error for the negative class)/2. If you predict all positive or all negative, this metric will be 50% which is a nice property.

In my opinion, the only reason to down-sample is when you have too much data and can't fit your model. Many classifiers (logistic regression for example) will do fine on un-balanced data.

$\endgroup$
2
  • $\begingroup$ Can I ask how classifiers can do well on imbalanced data? Perhaps it is just my data but I have tried training logistic regression, random forests and C5.0 models on my training data, both imbalanced and balanced using mixed over/undersampling. The models trained on the imbalanced data perform far worse on my test set than those trained on balanced data. $\endgroup$ Commented Feb 25, 2018 at 19:59
  • $\begingroup$ is something like a threshold when you say the only reason to down-sample is when you have too much data and can't fit your model.? $\endgroup$
    – A1010
    Commented Aug 6, 2020 at 13:31
13
$\begingroup$

As always @Marc Claesen as a great answer.

I'd just add that the key concept that seems to be missing is the concept of a cost function. In any model you have an implicit or explicit cost of false negatives to false positives (FN/FP). For the unbalanced data described one is often willing to have a 5:1 or 10:1 ratio. There are many ways of introducing cost functions into models. A traditional method is to impose a probability cut-off on the probabilities produced by a model - this works well for logistic regression.

A method used for strict classifiers that do not naturally output probability estimates is to undersample the majority class at a ratio that will induce the cost function you are interested in. Note that if you sample at 50/50 you are inducing an arbitrary cost function. The cost function is different but just as arbitrary as if you sampled at the prevalence rate. You can often predict an appropriate sampling ratio that corresponds to your cost function (it is usually not 50/50), but most practitioners that I've talked to just try a couple of sampling ratios and choose the one closest to their cost function.

$\endgroup$
2
  • 4
    $\begingroup$ Thanks for bringing that up, that's an interesting idea that I hadn't considered. How can you tell which sampling ratio corresponds to your cost function? $\endgroup$
    – Jessica
    Commented Nov 2, 2014 at 21:22
  • $\begingroup$ You can calculate the sampling ratio from the cost matrix, a good reference is Elkan, C. (2001). The Foundations of Cost-Sensitive Learning. In B. Nebel (Ed.), Proceedings of the 7th International Joint Conference on Artificial Intelligence (pp. 973–978). Morgan Kaufmann. $\endgroup$
    – Johannes
    Commented Jun 12, 2021 at 20:22
10
$\begingroup$

Answering Jessica's question directly - one reason for downsampling is when you're working with a large dataset and facing memory limits on your computer or simply want to reduce processing time. Downsampling (i.e., taking a random sample without replacement) from the negative cases reduces the dataset to a more manageable size.

You mentioned using a "classifier" in your question but didn't specify which one. One classifier you may want to avoid are decision trees. When running a simple decision tree on rare event data, I often find the tree builds only a single root given it has difficulty splitting so few positive cases into categories. There may be more sophisticated methods to improve the performance of trees for rare events - I don't know of any off the top of my head.

Therefore using a logistic regression which returns a continuous predicted probability value, as suggested by Marc Claesen, is a better approach. If you're performing a logistic regression on the data, the coefficients remain unbiased despite there being fewer records. You will have to adjust the intercept, $\beta_0$, from your downsampled regression according to the formula from Hosmer and Lemeshow, 2000:

$$\beta_c=\beta_0 - \log\left(\frac{p_+}{1-p_+}\right)$$

where $p_+$ is the fraction of positive cases in your pre-downsampling population.

Finding your preferred spam ID threshold with the ROC can be done by first scoring the complete dataset with the model coefficients traned on the downsampled dataset, and then ranking the records from highest to lowest predicted probability of being spam. Next, take the top $n$ scored records, where $n$ is whatever threshold you want to set (100, 500, 1000, etc.) and then calculate the percentage of false positive cases in the top $n$ cases and the percentage of false negative cases in the remaining lower tier of $N$-$n$ cases in order to find the right balance of sensitivity/specificity that serves your needs.

$\endgroup$
3
$\begingroup$

Of course classifying everything as 'not spam' allows you to say that, given 100 mails, it classifies correctly 99 of them, but it also classifies as 'not spam' the only one labelled as spam (100% False Positive). It turns out that the metric you choose to evaluate the algorithm is not adapt. This video exemplifies the concept.

Roughly speaking, balancing the dataset allows you to weight the misclassification errors. An algorithm that uses an unbalanced training set presumably will not learn to discriminate from the features, because it would not give much importance to the fact that misclassifies the data of the scanty class.

$\endgroup$
0
$\begingroup$

I would not go for either downsampling or upsampling as both tricks the learning algorithm, however, if the data was imbalanced the accuracy measure becomes invalid or uninformative, therefore, it is better to use precision and recall measures, both depends mainly on the TP (the correctly classified spams in your case) this gives a good idea about the real performance of your system in terms detecting spams regardless the number of the negative examples.

$\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.