JuPyter:正在创建决策树,TypeError:“str”和“float”实例之间不支持“<”

2024-04-26 11:13:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用JuPyter笔记本创建决策树,当我开始创建决策并放置特性和目标类时,JuPyter会给我这个错误,这个错误在决策树的这个单元格中找到。dt = c.fit(X_train, y_train)

这就是错误。你知道吗

TypeError                             

    Traceback (most recent call last)
<ipython-input-10-0f9186b7935c> in <module>()
----> 1 dt = c.fit(X_train, y_train)

~\Anaconda3\lib\site-packages\sklearn\tree\tree.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
    788             sample_weight=sample_weight,
    789             check_input=check_input,
--> 790             X_idx_sorted=X_idx_sorted)
    791         return self
    792 

~\Anaconda3\lib\site-packages\sklearn\tree\tree.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
    138 
    139         if is_classification:
--> 140             check_classification_targets(y)
    141             y = np.copy(y)
    142 

~\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py in check_classification_targets(y)
    167     y : array-like
    168     """
--> 169     y_type = type_of_target(y)
    170     if y_type not in ['binary', 'multiclass', 'multiclass-multioutput',
    171                       'multilabel-indicator', 'multilabel-sequences']:

~\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py in type_of_target(y)
    286         return 'continuous' + suffix
    287 
--> 288     if (len(np.unique(y)) > 2) or (y.ndim >= 2 and len(y[0]) > 1):
    289         return 'multiclass' + suffix  # [1, 2, 3] or [[1., 2., 3]] or [[1, 2]]
    290     else:

~\Anaconda3\lib\site-packages\numpy\lib\arraysetops.py in unique(ar, return_index, return_inverse, return_counts, axis)
    221     ar = np.asanyarray(ar)
    222     if axis is None:
--> 223         return _unique1d(ar, return_index, return_inverse, return_counts)
    224     if not (-ar.ndim <= axis < ar.ndim):
    225         raise ValueError('Invalid axis kwarg specified for unique')

~\Anaconda3\lib\site-packages\numpy\lib\arraysetops.py in _unique1d(ar, return_index, return_inverse, return_counts)
    281         aux = ar[perm]
    282     else:
--> 283         ar.sort()
    284         aux = ar
    285     flag = np.concatenate(([True], aux[1:] != aux[:-1]))

TypeError: '<' not supported between instances of 'str' and 'float'

我很困惑,因为我的数据集特性是干净的,它都是Int,而目标类是唯一的范畴类。你知道吗

有人能告诉我发生了什么事,怎么做才能使dt = c.fit(X_train, y_train)起作用吗?c = DecisionTreeClassifier(min_samples_split=100)c是一个决策树分类器。你知道吗

这是我的示例数据集: enter image description here


Tags: inpyinputreturniflibpackagescheck