How to Fix: ValueError: Unknown label type: ‘continuous’


One common error you may encounter in Python is:

ValueError: Unknown label type: 'continuous'

This error usually occurs when you attempt to use sklearn to fit a classification model like logistic regression and the values that you use for the response variable are continuous instead of categorical.

The following example shows how to use this syntax in practice.

How to Reproduce the Error

Suppose we attempt to use the following code to fit a logistic regression model:

import numpy as np
from sklearn.linear_model import LogisticRegression

#define values for predictor and response variables
x = np.array([[2, 2, 3], [3, 4, 3], [5, 6, 6], [7, 5, 5]])
y = np.array([0, 1.02, 1.02, 0])

#attempt to fit logistic regression model
classifier = LogisticRegression()
classifier.fit(x, y)

ValueError: Unknown label type: 'continuous'

We receive an error because currently the values for our response variable are continuous.

Recall that a logistic regression model requires the values of the response variable to be categorical such as:

  • 0 or 1
  • “Yes” or “No”
  • “Pass” or “Fail”

Currently our response variable contains continuous values such as 0 and 1.02.

How to Fix the Error

The way to resolve this error is to simply convert the continuous values of the response variable to categorical values using the LabelEncoder() function from sklearn:

from sklearn import preprocessing
from sklearn import utils

#convert y values to categorical values
lab = preprocessing.LabelEncoder()
y_transformed = lab.fit_transform(y)

#view transformed values
print(y_transformed)

[0 1 1 0]

Each of the original values is now encoded as a 0 or 1.

We can now fit the logistic regression model:

#fit logistic regression model
classifier = LogisticRegression()
classifier.fit(x, y_transformed)

This time we don’t receive any error because the response values for the model are categorical.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

How to Fix: ValueError: Index contains duplicate entries, cannot reshape
How to Fix: Typeerror: expected string or bytes-like object
How to Fix: TypeError: ‘numpy.float64’ object is not callable

2 Replies to “How to Fix: ValueError: Unknown label type: ‘continuous’”

    1. Hi Sunny…The `TypeError: Singleton array array(LabelEncoder(), dtype=object) cannot be considered a valid collection.` typically occurs when you are trying to pass an array or object that is not iterable where an iterable (like a list or array) is expected.

      This error often happens in the context of using scikit-learn functions that expect a list or array of estimators or transformers, but you accidentally passed a single estimator or transformer instance without enclosing it in a list or array.

      ### Common Scenarios and Solutions

      1. **Pipelines or ColumnTransformer**:
      If you’re using a `Pipeline` or `ColumnTransformer` and trying to pass a single transformer, you should make sure it’s enclosed in a list or tuple as required.

      **Example**:
      “`python
      from sklearn.compose import ColumnTransformer
      from sklearn.preprocessing import LabelEncoder

      # Incorrect: Passing LabelEncoder() directly
      # transformers = [(‘label_encoder’, LabelEncoder(), [‘column_name’])]

      # Correct: Wrap LabelEncoder() in a list
      transformers = [(‘label_encoder’, [LabelEncoder()], [‘column_name’])]

      col_transformer = ColumnTransformer(transformers)
      “`

      2. **Ensemble Methods (like VotingClassifier or StackingClassifier)**:
      If you’re using an ensemble method that expects a list of estimators, make sure you’re passing a list of tuples where each tuple contains a name and an estimator.

      **Example**:
      “`python
      from sklearn.ensemble import VotingClassifier
      from sklearn.preprocessing import LabelEncoder

      # Incorrect: Passing LabelEncoder() directly
      # model = VotingClassifier(estimators=[LabelEncoder()])

      # Correct: Wrap LabelEncoder() in a named tuple within a list
      model = VotingClassifier(estimators=[(‘label_encoder’, LabelEncoder())])
      “`

      3. **Custom Functions**:
      If you’re writing custom functions that are supposed to handle multiple transformers or estimators, ensure you’re handling inputs properly.

      **Example**:
      “`python
      def apply_transformer(transformer, data):
      if not isinstance(transformer, list):
      transformer = [transformer] # Convert to list if it’s a single object
      # Apply the transformer(s)
      for trans in transformer:
      data = trans.fit_transform(data)
      return data
      “`

      ### General Solution
      Always check the documentation of the function or method you’re using to see if it expects a single instance or a list/array of instances. If it’s the latter, you should wrap your estimator or transformer in a list or tuple.

      If you provide more details about where this error occurred in your code, I can offer more specific guidance!

Leave a Reply

Your email address will not be published. Required fields are marked *