What should I do if I encounter the error message “ValueError: Unknown label type: ‘continuous’” in my code?

What should I do if I encounter the error message “ValueError: Unknown label type: ‘continuous’” in my code?

If you encounter the error message “ValueError: Unknown label type: ‘continuous’” while running your code, it means that the label type for your data is not recognized. To resolve this error, you should check the data type of your labels and make sure it is in a format that is recognized by the program. This could involve converting the labels to a different data type or adjusting the code to handle the specific label type. It is important to address this error to ensure the correct functioning of your code.

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 like 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_modelimport 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 requires the values of the response variable to be 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)

Additional Resources

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

Cite this article

stats writer (2024). What should I do if I encounter the error message “ValueError: Unknown label type: ‘continuous’” in my code?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-should-i-do-if-i-encounter-the-error-message-valueerror-unknown-label-type-continuous-in-my-code/

stats writer. "What should I do if I encounter the error message “ValueError: Unknown label type: ‘continuous’” in my code?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/what-should-i-do-if-i-encounter-the-error-message-valueerror-unknown-label-type-continuous-in-my-code/.

stats writer. "What should I do if I encounter the error message “ValueError: Unknown label type: ‘continuous’” in my code?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-should-i-do-if-i-encounter-the-error-message-valueerror-unknown-label-type-continuous-in-my-code/.

stats writer (2024) 'What should I do if I encounter the error message “ValueError: Unknown label type: ‘continuous’” in my code?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-should-i-do-if-i-encounter-the-error-message-valueerror-unknown-label-type-continuous-in-my-code/.

[1] stats writer, "What should I do if I encounter the error message “ValueError: Unknown label type: ‘continuous’” in my code?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. What should I do if I encounter the error message “ValueError: Unknown label type: ‘continuous’” in my code?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top