Python 错误 ValueError: Unknown Label Type: 'continuous'
本文将解决 Python 中出现 ValueError: Unknown label type: 'continuous' 错误的原因和解决方案。
ValueError: Unknown Label Type: 'continuous' 错误原因
当我们尝试在连续目标变量上训练 sklearn 导入的分类器时,Python 解释器会抛出此错误。
K 最近邻、决策树、逻辑回归等分类器预测输入变量的类别。 类变量采用离散或分类形式,例如 0 或 1、True 或 False,以及 Pass 或 Fail。
如果 sklearn 导入的分类算法,即逻辑回归是在连续目标变量上训练的,它会抛出 ValueError: Unknown label type:'continuous' 。
代码:
import numpy as np
from sklearn.linear_model import LogisticRegression
input_var=np.array([[1.1,1.2,1.5,1.6],[0.5,0.9,0.6,0.8]])
target_var=np.array([1.4,0.4])
classifier_logistic_regression=LogisticRegression()
classifier_logistic_regression.fit(input_var,target_var)
输出:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [6], in <module>
----> 1 lr.fit(x,y)
File c:\users\hp 840 g3\appdata\local\programs\python\python39\lib\site-packages\sklearn\linear_model\_logistic.py:1516, in LogisticRegression.fit(self, X, y, sample_weight)
1506 _dtype = [np.float64, np.float32]
1508 X, y = self._validate_data(
1509 X,
1510 y,
(...)
1514 accept_large_sparse=solver not in ["liblinear", "sag", "saga"],
1515 )
-> 1516 check_classification_targets(y)
1517 self.classes_ = np.unique(y)
1519 multi_class = _check_multi_class(self.multi_class, solver, len(self.classes_))
File c:\users\hp 840 g3\appdata\local\programs\python\python39\lib\site-packages\sklearn\utils\multiclass.py:197, in check_classification_targets(y)
189 y_type = type_of_target(y)
190 if y_type not in [
191 "binary",
192 "multiclass",
(...)
195 "multilabel-sequences",
196 ]:
--> 197 raise ValueError("Unknown label type: %r" % y_type)
ValueError: Unknown label type: 'continuous'
作为目标标签 y 的浮点值被传递给逻辑回归分类器,它接受分类或离散类标签。 结果,代码在 lr.fit()
函数中抛出错误,并且模型拒绝对给定数据进行训练。
使用 Scikit 的 LabelEncoder() 函数修复 ValueError: Unknown label type: 'continuous'
LabelEncoder()
函数将连续的目标变量编码为离散或分类标签。
分类器现在接受这些值。 分类器对给定数据进行训练并预测输出类别。
代码:
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn import preprocessing
from sklearn import utils
input_var=np.array([[1.1,1.2,1.5,1.6],[0.5,0.9,0.6,0.8]])
target_var=np.array([1.4,0.4])
predict_var=np.array([ [1.3, 1.7, 1.8,1.4], [0.2, 0.6, 0.3, 0.4] ])
encoded = preprocessing.LabelEncoder()
encoded_target= encoded.fit_transform(target_var)
print(encoded_target)
classifier_logistic_regression=LogisticRegression()
classifier_logistic_regression.fit(input_var,encoded_target)
predict=classifier_logistic_regression.predict(predict_var)
print(predict)
输出:
[1 0]
[1 0]
使用 LabelEncoder()
函数将目标变量 target_var 的浮点值编码为离散或分类,即 encoded_target 值。
分类器现在接受这些值。 分类器被训练来预测新数据的类别,用 predict_var 表示。
评估数据以修复 ValueError: Unknown label type: 'continuous'
有时必须仔细检查数据以确定问题是回归问题还是分类问题。 一些输出变量,如房价,无法分类或离散化。
在这种情况下,问题是回归之一。 因为回归模型接受连续的目标变量,所以目标变量不需要编码。
代码:
import numpy as np
from sklearn.linear_model import LinearRegression
input_var=np.array([[1.1,1.2,1.5,1.6],[0.5,0.9,0.6,0.8]])
target_var=np.array([1.4,0.4])
predict_var=np.array([ [1.3, 1.7, 1.8,1.4], [0.2, 0.6, 0.3, 0.4] ])
linear_Regressor_model=LinearRegression()
linear_Regressor_model.fit(input_var,target_var)
predict=linear_Regressor_model.predict(predict_var)
print(predict)
输出:
[ 1.6 -0.05263158]
输出变量 target_var 中的浮点值表明问题出在回归上。 该模型必须预测输入变量的值而不是其类别。
训练线性回归模型并预测新数据的结果值。
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串