首页 > 解决方案 > Pandas 返回:ValueError: Unknown label type: 'continuous'

问题描述

我在使用 pandas 和 sklearn 进行机器学习时遇到了麻烦。我的问题是

ValueError:未知标签类型:“连续”

我试过了

model = sklearn.tree.DecisionTreeClassifier()
model.fit(X, y)

它返回此错误:

ValueError                                Traceback (most recent call last)
<ipython-input-45-3caead2f350b> in <module>
----> 1 model.fit(ninp, out)

c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sklearn\tree\_classes.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
    888         """
    889 
--> 890         super().fit(
    891             X, y,
    892             sample_weight=sample_weight,

c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sklearn\tree\_classes.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
    179 
    180         if is_classification:
--> 181             check_classification_targets(y)
    182             y = np.copy(y)
    183 

c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sklearn\utils\multiclass.py in check_classification_targets(y)
    170     if y_type not in ['binary', 'multiclass', 'multiclass-multioutput',
    171                       'multilabel-indicator', 'multilabel-sequences']:
--> 172         raise ValueError("Unknown label type: %r" % y_type)
    173 
    174 

ValueError: Unknown label type: 'continuous'

标签: pythonscikit-learn

解决方案


分类器将一组示例分类为离散的类(即,它分配与 K 个类中的一个对应的标签)。如果您的目标(y变量的内容)是连续的(例如介于 0 和 1 之间的浮点数),则决策树不知道如何处理它。

您有 2 个解决方案:

  1. 您的问题是分类任务,您需要对目标变量进行建模,使其代表类别而不是连续变量
  2. 您的问题不是分类任务,而是回归任务,您需要使用适当的模型(例如DecisionTreeRegressor

推荐阅读