Last Updated on

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.

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.

I would like to get your book on manchine learning

Thanks, you can see my catalog of books here:

https://machinelearningmastery.com/products/

“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/

Thank you for that great article. Back to how you start: “How do I calculate accuracy for my regression problem?” I am still not sure why this isn’t a valid question to ask. If your regression model predicts an amount between $0 and $100, we could still use various metrics to assess the accuracy of the prediction (average % error, max % error, error variance, etc.) No?

No. You calculate the error in the prediction for regression problems.

You can transform the regression into a classification problem, predict labels (e.g. at $10 increments) and report accuracy of that, but, it may not be the best way to model the problem (e.g. harder).

This explanation is just too awesome man.I had already studied about these concepts in depth a month ago.But most of the concepts used to appear so difficult to me until I read ur post.I am about to start a ml project in a couple of days,and I can’t express how nicely ur post has cleared my concepts.Thank you so much

I’m happy it helped!

Brilliant explanation of confusing topics

Thanks. I’m happy that it helped.

why the output of the regression problem is called continuous?

It is a real valued number, e.g. fractional or no interruption – continuous.

Thanks a lot sir great explaination. How to convert one class classification problem to regression?

It depends on the problem, perhaps you can divide your class into ordinal values that make sense for the domain?

Very clear explanation!!! Appreciate your efforts.

Thanks. I’m happy it helped.

Really a great article ! Thank you very much 😀

Thanks, I’m glad it helped.

This is a great article! I have read so many articles to understand this difference but none were so clear! Thanks!

I’m glad it helped.

This was really helpful Jason! Thanks for the post.

Thanks, I’m glad to hear that.

Your article is so wonderful

Could you please guide or make an article about the other problems of ML like: clustering, Ranking, Associations and correlation

Thanks for the suggestions!

what should i do if want to make prediction that has output in discrete? for example i want to make prediction of the sum of product , and the input is behaviour of customer . can i predict using regression?

Yes, I don’t see why not.

but if i make prediction with regression, the output will be continuous.

let say the output is 67.3, in reality the product can’t be a fraction number.

what should i do? ca i round the output?

Perhaps you can write some code to post-process the output?

Perhaps you can find an alternate representation for the model output that correctly constrains the output values. e.g. discrete classes?

Hello Doc! What does a two-class or a two-component regression data mean?

No idea?

Hello,

Thanks for your tutorials. What is the best strategy for a multi-class multi-label classification problem? For example, we have three outputs and each output has three classes.

Thanks.

I hope to have some posts on this topic soon.

hi jason ,

for classification problem -Is that forecasting possible ..

Yes, it is called time series classification.

Thanks a bunch! This post was really helpful and concise.

I’m glad it was helpful.

Hi Jason, I am still confusing after reading here. How to distinguish between classification and regression method? My response may be categorical, but my purpose is estimating the probability of each categorical. Is this still a classification problem? Is the response type decided what the method is instead of the type of purpose? I am so confused.

Good question.

Predicting the probability of a category is still a classification problem.

Hi Jason !

Thank you so much for doing this tutorial.

Could you show some example of how do we accomplish “range between $0 and $100 could be converted into 2 buckets:” in the actual code ?

I completed a LSTM regression model right now and I want to convert it into classification model. How do i do that exactly ?

Sorry, I cannot prepare code for you.

Got it. If the output that I want to predict is -1, 0, 1. What is the best approach using LSTM classification ?

Thank you so much for answering

LSTM is only appropriate if the input is a sequence.

In that case, you can use a Dense output layer with a linear or perhaps a tanh activation function and fit with MSE.

Convert Between Classification and Regression Problems. I am doing a similar approach and would like to know more in detail about it.

Where can I find it?

It is specific to the data.

Perhaps just try exploring a few different framings of the problem and see what works well?

Many thanks for yous post!!!!!

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

Thanks Jason for valuable info and the effort you put in to share this.

You’re welcome.

Thanks for your support!

Hi Jason, I feel a bit confused. By your definition, logistic regression is not regression but classification, is that correct?

Correct!

It is a classification algorithm with a TERRIBLE name 🙂

This is a common point of confusion, you’re not alone.

Nice explanation

Thanks.

I would say I’m really not good at math, but the way you can describe and teach complex things like this makes it even understandable for normal folks like me.

THANK YOU !!

Thanks, I’m happy to hear that!

Hello! I’ve been reading your tutorials last few days, and i’ve learned so much! Thanks!

I have a question related to defining an activation function to my classification problem. Say i have 2 outputs in it, and one can have 3 classes and the other can have 2 classes, something like this:

Output 1:

Class 0: $0 to $50

Class 1: $51 to $ 100

Class 2: Greater than $100

Output 2:

Class 0: Less than $2000

Class 1: Greater than $2000

Can i just label (sorry, don’t know if this is the correct term) those classes as -1, 0, 1 for the first output, and -1, 1(Output 2) and use a hyperbolic tanget as activation function?

Thanks!

For 2 classes, you can label them 0,1, for 3 classes, you would use a one hot encoding.

For two outputs, I would recommend a multi-output model:

https://machinelearningmastery.com/keras-functional-api-deep-learning/

I hear people say “how accurate is the model” in reference to a regression problem all the time. This is how people were accustomed to saying it in grade school. How should I correct them? Instead of saying “how accurate is the model” what should we say?

What is the error of your model?

What is the prediction error of your model?

Very clear and concise, thanks !

Thank you.