Scikitlearn DecisionTreeClassifier多输出决策(带字符串和浮点列)

2024-09-29 19:33:47 发布

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

我有一个多输出问题,我正试图用DecisionTreeClassifier来解决。在

我的训练集由一套不同特点的书组成。基于这个特性,我想预测每本书的两个输出:类别(戏剧、幻想、科幻)和这本书的页数。每本书只能属于一个类别。在

我使用的代码与此类似

model = tree.DecisionTreeClassifier() 
model.fit(X_train,y_train)

其中y_train是一个包含两列的Dataframe,第一列是string列(category),第二列是float列(页数)。在

运行代码时,我得到下面列出的错误。在

如果我省略其中一列(单输出问题),一切正常。在

根据调用堆栈,DecissionTreeClassifier试图将我的问题视为多标签问题,但在尝试为第二列(包含浮点)排序标签时失败。在

是否有其他方法来完成这项工作/格式化输入数据,以便DecisionTreeClassifier(或任何其他分类器)来处理它。我不能使用两个独立的分类器(DecisionTreeClassifierDecisionTreeRegressor),因为两个输出是相关的(幻想书往往较长)。在

理想情况下,我希望使用model.predict_proba()来获得每个类别的概率和每个类别的页数。结果应该是这样的

^{pr2}$

。。意思是90%的概率,这是一本100页的戏剧书,10%的概率是这是一本200页的幻想书。。。。在

错误的调用堆栈:

TypeError                                 Traceback (most recent call last)
<ipython-input-54-c1b30fced85c> in <module>()
      1 model = tree.DecisionTreeClassifier(min_samples_leaf=50) # prej smo imeli 5
----> 2 model.fit(data,y_train)
      3 #model.score(data,resitev)

~\AppData\local\AmlWorkbench\Python\lib\site-packages\sklearn\tree\tree.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
    737             sample_weight=sample_weight,
    738             check_input=check_input,
--> 739             X_idx_sorted=X_idx_sorted)
    740         return self
    741 

~\AppData\local\AmlWorkbench\Python\lib\site-packages\sklearn\tree\tree.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
    144 
    145         if is_classification:
--> 146             check_classification_targets(y)
    147             y = np.copy(y)
    148 

~\AppData\local\AmlWorkbench\Python\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']:

~\AppData\local\AmlWorkbench\Python\lib\site-packages\sklearn\utils\multiclass.py in type_of_target(y)
    235                          'got %r' % y)
    236 
--> 237     if is_multilabel(y):
    238         return 'multilabel-indicator'
    239 

~\AppData\local\AmlWorkbench\Python\lib\site-packages\sklearn\utils\multiclass.py in is_multilabel(y)
    151                  _is_integral_float(np.unique(y.data))))
    152     else:
--> 153         labels = np.unique(y)
    154 
    155         return len(labels) < 3 and (y.dtype.kind in 'biu' or  # bool, int, uint

~\AppData\local\AmlWorkbench\Python\lib\site-packages\numpy\lib\arraysetops.py in unique(ar, return_index, return_inverse, return_counts, axis)
    208     ar = np.asanyarray(ar)
    209     if axis is None:
--> 210         return _unique1d(ar, return_index, return_inverse, return_counts)
    211     if not (-ar.ndim <= axis < ar.ndim):
    212         raise ValueError('Invalid axis kwarg specified for unique')

~\AppData\local\AmlWorkbench\Python\lib\site-packages\numpy\lib\arraysetops.py in _unique1d(ar, return_index, return_inverse, return_counts)
    275         aux = ar[perm]
    276     else:
--> 277         ar.sort()
    278         aux = ar
    279     flag = np.concatenate(([True], aux[1:] != aux[:-1]))

TypeError: unorderable types: float() < str()

Tags: inpytreeinputmodelreturnlibpackages

热门问题