There is an important difference between classification and regression problems.

Fundamentally, classification is about predicting a label and regression is about predicting a quantity.

I often see questions such as:

How do I calculate accuracy for my regression problem?

Questions like this are a symptom of not truly understanding the difference between classification and regression and what accuracy is trying to measure.

In this tutorial, you will discover the differences between classification and regression.

After completing this tutorial, you will know:

- That predictive modeling is about the problem of learning a mapping function from inputs to outputs called function approximation.
- That classification is the problem of predicting a discrete class label output for an example.
- That regression is the problem of predicting a continuous quantity output for an example.

Let’s get started.

## Tutorial Overview

This tutorial is divided into 5 parts; they are:

- Function Approximation
- Classification
- Regression
- Classification vs Regression
- Converting Between Classification and Regression Problems

## Function Approximation

Predictive modeling is the problem of developing a model using historical data to make a prediction on new data where we do not have the answer.

For more on predictive modeling, see the post:

Predictive modeling can be described as the mathematical problem of approximating a mapping function (f) from input variables (X) to output variables (y). This is called the problem of function approximation.

The job of the modeling algorithm is to find the best mapping function we can given the time and resources available.

For more on approximating functions in applied machine learning, see the post:

Generally, we can divide all function approximation tasks into classification tasks and regression tasks.

## Classification Predictive Modeling

Classification predictive modeling is the task of approximating a mapping function (f) from input variables (X) to discrete output variables (y).

The output variables are often called labels or categories. The mapping function predicts the class or category for a given observation.

For example, an email of text can be classified as belonging to one of two classes: “spam*“* and “*not spam*“.

- A classification problem requires that examples be classified into one of two or more classes.
- A classification can have real-valued or discrete input variables.
- A problem with two classes is often called a two-class or binary classification problem.
- A problem with more than two classes is often called a multi-class classification problem.
- A problem where an example is assigned multiple classes is called a multi-label classification problem.

It is common for classification models to predict a continuous value as the probability of a given example belonging to each output class. The probabilities can be interpreted as the likelihood or confidence of a given example belonging to each class. A predicted probability can be converted into a class value by selecting the class label that has the highest probability.

For example, a specific email of text may be assigned the probabilities of 0.1 as being “spam” and 0.9 as being “not spam”. We can convert these probabilities to a class label by selecting the “not spam” label as it has the highest predicted likelihood.

There are many ways to estimate the skill of a classification predictive model, but perhaps the most common is to calculate the classification accuracy.

The classification accuracy is the percentage of correctly classified examples out of all predictions made.

For example, if a classification predictive model made 5 predictions and 3 of them were correct and 2 of them were incorrect, then the classification accuracy of the model based on just these predictions would be:

1 2 3 |
accuracy = correct predictions / total predictions * 100 accuracy = 3 / 5 * 100 accuracy = 60% |

An algorithm that is capable of learning a classification predictive model is called a classification algorithm.

## Regression Predictive Modeling

Regression predictive modeling is the task of approximating a mapping function (f) from input variables (X) to a continuous output variable (y).

A continuous output variable is a real-value, such as an integer or floating point value. These are often quantities, such as amounts and sizes.

For example, a house may be predicted to sell for a specific dollar value, perhaps in the range of $100,000 to $200,000.

- A regression problem requires the prediction of a quantity.
- A regression can have real valued or discrete input variables.
- A problem with multiple input variables is often called a multivariate regression problem.
- A regression problem where input variables are ordered by time is called a time series forecasting problem.

Because a regression predictive model predicts a quantity, the skill of the model must be reported as an error in those predictions.

There are many ways to estimate the skill of a regression predictive model, but perhaps the most common is to calculate the root mean squared error, abbreviated by the acronym RMSE.

For example, if a regression predictive model made 2 predictions, one of 1.5 where the expected value is 1.0 and another of 3.3 and the expected value is 3.0, then the RMSE would be:

1 2 3 4 5 |
RMSE = sqrt(average(error^2)) RMSE = sqrt(((1.0 - 1.5)^2 + (3.0 - 3.3)^2) / 2) RMSE = sqrt((0.25 + 0.09) / 2) RMSE = sqrt(0.17) RMSE = 0.412 |

A benefit of RMSE is that the units of the error score are in the same units as the predicted value.

An algorithm that is capable of learning a regression predictive model is called a regression algorithm.

Some algorithms have the word “regression” in their name, such as linear regression and logistic regression, which can make things confusing because linear regression is a regression algorithm whereas logistic regression is a classification algorithm.

## Classification vs Regression

Classification predictive modeling problems are different from regression predictive modeling problems.

- Classification is the task of predicting a discrete class label.
- Regression is the task of predicting a continuous quantity.

There is some overlap between the algorithms for classification and regression; for example:

- A classification algorithm may predict a continuous value, but the continuous value is in the form of a probability for a class label.
- A regression algorithm may predict a discrete value, but the discrete value in the form of an integer quantity.

Some algorithms can be used for both classification and regression with small modifications, such as decision trees and artificial neural networks. Some algorithms cannot, or cannot easily be used for both problem types, such as linear regression for regression predictive modeling and logistic regression for classification predictive modeling.

Importantly, the way that we evaluate classification and regression predictions varies and does not overlap, for example:

- Classification predictions can be evaluated using accuracy, whereas regression predictions cannot.
- Regression predictions can be evaluated using root mean squared error, whereas classification predictions cannot.

## Convert Between Classification and Regression Problems

In some cases, it is possible to convert a regression problem to a classification problem. For example, the quantity to be predicted could be converted into discrete buckets.

For example, amounts in a continuous range between $0 and $100 could be converted into 2 buckets:

- Class 0: $0 to $49
- Class 1: $50 to $100

This is often called discretization and the resulting output variable is a classification where the labels have an ordered relationship (called ordinal).

In some cases, a classification problem can be converted to a regression problem. For example, a label can be converted into a continuous range.

Some algorithms do this already by predicting a probability for each class that in turn could be scaled to a specific range:

1 |
quantity = min + probability * range |

Alternately, class values can be ordered and mapped to a continuous range:

- $0 to $49 for Class 1
- $50 to $100 for Class 2

If the class labels in the classification problem do not have a natural ordinal relationship, the conversion from classification to regression may result in surprising or poor performance as the model may learn a false or non-existent mapping from inputs to the continuous output range.

## Further Reading

This section provides more resources on the topic if you are looking to go deeper.

## Summary

In this tutorial, you discovered the difference between classification and regression problems.

Specifically, you learned:

- That predictive modeling is about the problem of learning a mapping function from inputs to outputs called function approximation.
- That classification is the problem of predicting a discrete class label output for an example.
- That regression is the problem of predicting a continuous quantity output for an example.

Do you have any questions?

Ask your questions in the comments below and I will do my best to answer.

nice post to clear the basic concepts

Thanks!

Really a good one sir,i had little bit confusion regarding these two.Thank you very much

I’m glad it helped.

Very specific and crystal clear.. we need more of these in your books too 🙂

Thanks.

“Some algorithms have the word “regression” in their name, such as linear regression and logistic regression, which can make things confusing because linear regression is a regression algorithm whereas logistic regression is a classification algorithm”

Thank you for that clarification, even after all this time in ML that always nagged at me.\

I’m glad it helped James.

This topic is known to me yet the article made it straight to the point, thanks!

One thing, in “A classification can have real-valued or discrete input variables.” you probably meant continious vs. discrete

Thanks.

Very basic and insightful. I experience same misunderstanding from people when hiring talent and sometimes explaining these concepts to people. Will take a cue from your explanation going forward. Thanks !!!!

Valuable tutorial Jason

Thank you very much

hanks, I’m glad it helped.

Merci bien pour votre clarification de la différences entre les deux types ..

You’re welcome, I’m glad it helped.

Thank a lot Sir.its indeed a good post for basics on ml….

I’m glad it helped.

Hey Jason,

This is really helpful for beginners.

Could you provide single example of following section:

“Convert Between Classification and Regression Problems”

Thanks for the suggestion.

Great explanation of important concepts. Liked your comment on logistic regresión being a classification problem. The regression in the name does make it confusing when starting out.

I’m glad it helped!

I’m actually getting to focus on ML, absolutely new to it. Thanks for your eye-opening and insightful explanations

I’m glad the post helped.

Thank you!

You’re welcome.

How is it possible to reverse the process of discretizing data given that there is a loss of information? For example, following the same example above values of $0 to $49 would be represented by categorical value of 0.

Can’t figure out how the reverse is possible without knowing original value (in this case price in USD).

You cannot as information is lost.

Any help about CNN and RNN implementation using pima indian dataset in python

The Pima Indians dataset would be a bad for for a CNN+LSTM model.

You can learn about the model here:

https://machinelearningmastery.com/cnn-long-short-term-memory-networks/

Great blog! could u please explain how to determine the variance and bias for a model prediction?

No, they are abstract concepts for understanding how algorithms work:

https://machinelearningmastery.com/gentle-introduction-to-the-bias-variance-trade-off-in-machine-learning/

Thanks for the post, clearly explained. Also, I am thinking about using Python to try these classification. regression, models etc. is there a useful online tutorials to follow ?

Yes, right here:

https://machinelearningmastery.com/start-here/#python

Nice explanation. thanks.

You’re welcome.

Thanks very well explained. !!

Thanks.

Hi Jason:

I am a patent attorney, used to be a physics animal. I have written several hundred patent applications directed to software, mostly cyber security and cloud security system patents. Lately, like in every field, AI and ML in particular is everywhere. I am not afraid of the math, but the vocabulary was an issue – until I read your post.

I can’t tell you how much I appreciated that. As you know, vocabulary, at least consistent vocabulary usage, is a bit of an issue in the data science and software worlds. Your post cleared up many questions in just a few minutes of reading – THANK YOU!

You are an awesome writer and teacher

I’m glad it helped Phil!

Hi,

Thank you for your great tutorials.

Could you please give us a tutorial on how to classify images using transfer learning and Tensorflow?

Or guide me where I can find great tutorial about it like yours?

I have an example in Keras here:

https://machinelearningmastery.com/use-pre-trained-vgg-model-classify-objects-photographs/

Wow! thank you for replying

Appreciate your effort and clearly helpful.

thanks !

You’re welcome.

simple and valuable , thanks.

Thanks, I’m glad to hear that.

very well explained, actually the solution of my problem.

“How do I calculate accuracy for my regression problem?” this was my actual question but now it’s clear.

Thanks man !!

I’m so glad to hear that!

Hello Sir, did you had any suggestion regarding the reference such as Journal or Book that I could use to explain about Regression Predictive Modeling?

Thank you

What questions do you have exactly?

The question will be, “why we use Regression Predictive Modeling for Stock index forecasting”, any suggestion will be really appreciated, thank you, Sir.

I understand that short term price movements are a random walk and that a persistence model will be the best that you can achieve:

https://machinelearningmastery.com/gentle-introduction-random-walk-times-series-forecasting-python/

thank you so much, Sir, I’m writing my bachelor thesis and I’ve read a lot of your research on google scholar, those are very helpful!

Thanks.

Thanks Jason for amazing article…you Rock Jason !!!

Thanks, I’m glad it helped.

please clear with example that what is called classification and what is regression problem. how to get to know the problem is a classification problem or a regression problem.

Classification is about predicting a label (e.g. ‘red’). Regression is about predicting a quantity (e.g. 100).

Does that help?

Fantastic post, thank you for sharing! I was recently training a model as a binary classification problem using sigmoid as a single output. However, I was able to get far better results using MSE rather than binary cross-entropy. Since MSE is mostly used for regression, does this mean I was forced to convert it to a regression problem? This has been tickling my brain for quite some time now….

Thanks.

MSE with a sigmoid output function? Wow, and it worked without error?

Be careful when evaluating the skill of the model, ensure it is doing what you think.

Thanks………

You’re welcome.

Excellent post! I appreciate too much your work Ph.D. Jason Brownlee.

I have a question. What type of task should I perform if my dependent variable observations are dichotomous, but I need to infer continuous values?

If you have categorical inputs and require a real-valued output, this sounds like regression, and a challenging case.

Perhaps you can try modeling it directly as a regression problem and see how you go. You may want to integer encode or one hot encode the inputs.

Thank you very much for your help!

I have used two variants to determine my continuous output:

1. The probability of belonging to the positive class in a classification model.

2. The output of a regression model.

The accuracy in terms of a CMC curve for the classification model outperformed the regression model. But I am not sure if I have misunderstood some results.

Is it feasible to assume the probability of belonging to the positive class in a classification model as the similarity to this class? When the classifier outputs the probability (p) to belong to the negative class, I computed the probability to belong to the positive class as 1-p.

Predicting class probability is a classification problem. Some algorithms can predict a probability.

Thank you very much, Dr. Jason Brownlee! Your advice is helpful for my work. You are very gentle sharing your great knowledge!!!!

I’m glad it helped.

Dear Jason,

The article really explained the concepts well. Thank you for the links to tutorials using Python.

Thanks, I’m glad it helped.

This is simply awesome and it helped clarify a number of issues in ML. Thanks Dr. Brownlee

I’m glad it helped.

thanks for this article. it is very helpfull

I’m glad it helped.

Hi, Jason.

What happens when a problem has many ordinal categories? For example, if I wanted to predict, I don’t know, the goals (soccer) a team scores in a match (which generally will be in the range of 0 to 10) is a classification problem or a regression problem?

There are no rules. Try both and see which results in the better or more stable model.

A basic question: are the classifiers generally faster than the regressors because the discrete outputs are easier to compute?

No.

Thanks a lot for sharing this beautiful concept, i am looking for more.

I’m glad it helped.

Really helpful.

Now my concept is crystal clear!!

Thanks 🙂

I’m glad to hear that.

this is great, thanks again jason!

is there a certain threshold of a RMSE for a regression problem that would provide the impetus to discretize the output variable into an ordinal classification problem? i.e. 0.5?

i could be completely wrong here, but it seems like discretizing the output of a regression is simply just artificially inflating your predictive model’s precision accuracy…

what are you thoughts?

I think it depends on the problem, whether the interpretation of the output as discrete quantities makes sense.

Thanks

You’re welcome.

Thanks Jason! Your language in explaining the topic was easy to understand.

Thanks, I’m happy that it helped.

Thanks Jason, it is great help to me.

One thing I need to confirm, if I use movielens dataset to predict the rating, the regression prediction is the correct one, right? What if I use classification prediction in this case? May I got wrong prediction?

You can choose to model it as regression or classification.

There is no objective “correct” in machine learning, there is just a dataset and your goals/stakeholders.

Are there any particular scenario that you can mention like a question which asks to implement Linear Regression and another question which ask to implement logistic regression (classification) so that we can implement in R.

When predicting a quantity, use linear regression.

When predicting a binary label, use logistic regression.

Hi jason,

thanks.

I consider this tutorial more convenient for my question about Regression vs Classification (classes).

In the case you can convert your model from Regression to Classification, as it is explained in your last section, which could be more convenient to apply Regression (continuos) or Classification (discrete labels e.g. you divided your continuos output in some segment of interest and associated them to some artificial range such very low, low, medium, high, super, ultra, super ultra, tremendously, such it is the current case for frequency spectrum :-)))…Which model performs better Regression or Classification? using the same common layers (with the exception at the output layer and activation function?).

We cannot know which approach will perform best for a given problem, you must use systematic experimentation and discover what works best.

This is the job of applied machine learning.

OK . Thks

from content, you mentioned that A classification algorithm may predict a continuous value, but the continuous value is in the form of a probability for a class label.

from the summary, you mentioned That classification is the problem of predicting a discrete class label output for an example.

quite confusing

Sorry, perhaps ignore the prediction of a probability for now then and focus on predicting a real value for regression and a label for classification.

When it is suitable to use regression over classification?

Use regression when you want to predict a quantity.

Use classification when you want to predict a label.

Does that help?

So nice discussion Thank you Jason , i am really interested in applications of Classification and Regression problems in real world like Automotive industry.

Thanks.

Hi Jason – I want the input to be the name of the event e.g. “Swimming” and the output to be like “Physical:80%, Social:10%, Emotional:10%”.

Another Example:

“Cooking” is “Vocational:30, Social:20, Nutritional:50”

This is clearly not a classification problem, as the output is not binary. Do you agree it can be best implemented by regression models?

Thanks,

It is a multi-label classification task.

I don’t have examples of this type of task sorry.

Hi Jason. I was hoping if you could help clarify. You say to use regression for predicting quantities and classification for labels. If I was interested in the predicting the probability of something being in a specific class, would it still be a classification problem?

Yes, that is still classification.

You are always Awesome, Jason. Thanks for all your efforts. It cleared my doubt about these 2 ML methods.

I’m happy it helped.

Hi Jason,

Thanks for the very informative blog. I do have a question. i want to predict Y, which can take the values 0,1or 2 based on an independent variable (X) which takes a value from 0 to 9. However, there are additional 8 categorical variables that are my control variables. I want to know if X predicts Y. I performed a ordinal logistic regression, however, I was told classification would be a better approach. My question is, how does one perform classification if control variables are to be taken into account too?

Perhaps a model can take the dependent and control variables as input and predict y?

Also, perhaps try a one hot encoding if the vars are ordinal/categorical.

Hi Jason,

is it possible to generalize the amount of data that is needed for a regression and classification Problem?

In my Opinion, to make a valid answer of how much something is, it needs more Data to learn from than “just” saying it is 1 or 0. Or is there no difference in the needed amount of Data for Regression/Classification Prolems?

Thanks for an answer!

The amount of data required depends on the complexity of the problem and the chosen algorithm.

Perhaps this will help:

https://machinelearningmastery.com/much-training-data-required-machine-learning/