01 logo

How To Fix ValueError: Unknown label type: 'continuous' in scikit-learn

Understanding what is causing ValueError for continuous variables in scikit-learn and how to get rid of it

By Giorgos MyrianthousPublished 3 years ago • 3 min read
How To Fix ValueError: Unknown label type: 'continuous' in scikit-learn
Photo by Yash Patel on Unsplash

Introduction

People who are fairly new to Machine Learning and Python Programming may experience some troubles when attempting to train a model using scikit-learn package.

One of the most commonly reported problems is related to the type of the target variable that may trigger specific errors when an inappropriate value is observed. One such error is the ValueError: Unknown label type: 'continuous' whose full traceback is shared below.

In today's short tutorial we will attempt to reproduce the error, understand why this particular exception is being raised and finally showcase how to deal with it.

Reproducing the error

First, let's create some sample data that we will be using in order to train an example Logistic Regression model with scikit-learn. Note that the target variable is continuous.

Now if we attempt to execute the above code snippet, the following error is reported to the standard output:

Essentially, the error is telling us that the type of the target variable is continuous which is not compatible with the specific model we are trying to fit (i.e. LogisticRegression).

Getting rid of the error

The fact that Logistic Regression is actually used for classification and not for regression comes as a surprise to many beginners. And I think this is absolutely understandable given that the model name itself can be quite confusing to new-comers.

A classifier - i.e. a model that is supposed to perform classification - expects the labels (aka the target variable) to be categorical and not continuous (this is the expectation from models when performing regression, instead).

So the first option you have when it comes to using Logistic Regression (or pretty much any classifier) is to perform label encoding. Note however that this may not be the most appropriate solution and the answer really depends on your specific use-case! So take this with a grain of salt.

Prior to label encoding, we can verify the type of the target variable using the utils.multiclass.type_of_target method.

Now going forward, we can perform label encoding in order to normalise the target variable using the LabelEncoder in scikit-learn.

Now we can verify that the newly encoded target variable is of multiclass type:

And we can now go forward and train our Logistic Regression model using the encoded target variable, without any troubles:

But before using label encoding you may still need consider whether Logistic Regression is a suitable model for your specific use-case. If you want to perform regression over the data, then you probably have to explore some alternative options.

Problems that involve a continuous response, are usually referred to as regression tasks. Note that the inputs can still be either continuous or discrete values. Regression problems can further be characterised as multivariate problems when multiple input variables are involved and time-series forecasting when the independent variables are ordered based on time. Regression tasks involve problems that require predicting a quantitative response variable whereas whereas classification tasks involve problems with categorical response variables.

On the other hand, problems involving a categorical response are referred to as classification tasks. Additionally, when a problem involves only two categories or labels, we usually refer to them as binary classification tasks whereas when more than two labels exist we usually call them multi-class classification tasks.

Lastly, problems that require the assignment of more than a single label to one data point are usually called multi-label classification problems.

In most of the cases, we have to select a proper Statistical Learning method or approach depending on the format of the target variable.

Final Thoughts

In today's short tutorial we discussed about ValueError: Unknown label type: 'continuous' error being raised by scikit-learn classifiers that observe invalid values in the provided target variables, based on the label type that a specific model expects.

We reproduced the error, discussed about why it is being raised in the first place and how to get rid of it.

how to

About the Creator

Giorgos Myrianthous

I strive to build data-intensive systems that are not only functional, but also scalable, cost effective and maintainable over the long term.

Enjoyed the story? Support the Creator.

Subscribe for free to receive all their stories in your feed.

Subscribe For Free

Reader insights

Share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment
    Written by Giorgos Myrianthous