【问题标题】:RandomForest score method ValueErrorRandomForest 评分方法 ValueError
【发布时间】:2017-04-01 21:12:38
【问题描述】:

我正在尝试查找给定数据集相对于某些训练数据的分数。我写了以下代码:

from sklearn.ensemble import RandomForestClassifier
import numpy as np

randomForest = RandomForestClassifier(n_estimators = 200)

li_train1 =  [[1,2,3,4,5,6,7,8,9],[1,2,3,4,5,6,7,8,9]]

li_train2 =  [[1,2,3,4,5,6,7,8,9],[1,2,3,4,5,6,7,8,9]]

li_text1 = [[10,20,30,40,50,60,70,80,90], [10,20,30,40,50,60,70,80,90]]

li_text2 = [[1,2,3,4,5,6,7,8,9],[1,2,3,4,5,6,7,8,9]]

randomForest.fit(li_train1, li_train2)

output =  randomForest.score(li_train1, li_text1)

在编译并尝试运行程序时出现错误:

Traceback (most recent call last):
  File "trial.py", line 16, in <module>
    output =  randomForest.score(li_train1, li_text1)
  File "/usr/local/lib/python2.7/dist-packages/sklearn/base.py", line 349, in score
    return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
  File "/usr/local/lib/python2.7/dist-packages/sklearn/metrics/classification.py", line 172, in accuracy_score
    y_type, y_true, y_pred = _check_targets(y_true, y_pred)
  File "/usr/local/lib/python2.7/dist-packages/sklearn/metrics/classification.py", line 89, in _check_targets
    raise ValueError("{0} is not supported".format(y_type))
ValueError: multiclass-multioutput is not supported

在检查与 score 方法相关的文档时,它说:

score(X, y, sample_weight=None)
X : array-like, shape = (n_samples, n_features)
    Test samples.

y : array-like, shape = (n_samples) or (n_samples, n_outputs)
    True labels for X.

在我的例子中,X 和 y 都是数组,二维数组。

我也经历了this 的问题,但我不明白我哪里错了。

编辑

所以根据答案和后面的cmets,我将程序编辑如下:

from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import MultiLabelBinarizer
import numpy as np

randomForest = RandomForestClassifier(n_estimators = 200)

mlb = MultiLabelBinarizer()

li_train1 =  [[1,2,3,4,5,6,7,8,9],[1,2,3,4,5,6,7,8,9]]

li_train2 =  [[1,2,3,4,5,6,7,8,9],[1,2,3,4,5,6,7,8,9]]

li_text1 = [100,200]

li_text2 = [[1,2,3,4,5,6,7,8,9],[1,2,3,4,5,6,7,8,9]]

randomForest.fit(li_train1, li_train2)

output =  randomForest.score(li_train1, li_text1)

在此编辑后,我收到错误:

Traceback (most recent call last):
  File "trial.py", line 19, in <module>
    output =  randomForest.score(li_train1, li_text1)
  File "/usr/local/lib/python2.7/dist-packages/sklearn/base.py", line 349, in score
    return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
  File "/usr/local/lib/python2.7/dist-packages/sklearn/metrics/classification.py", line 172, in accuracy_score
    y_type, y_true, y_pred = _check_targets(y_true, y_pred)
  File "/usr/local/lib/python2.7/dist-packages/sklearn/metrics/classification.py", line 82, in _check_targets
    "".format(type_true, type_pred))
ValueError: Can't handle mix of binary and multiclass-multioutput

【问题讨论】:

    标签: python machine-learning scikit-learn random-forest unsupervised-learning


    【解决方案1】:

    根据documentation

    警告:目前,sklearn.metrics 中的度量标准不支持多输出-多类分类任务。

    score 方法调用 sklearn 的准确度指标,但您定义的多类、多输出分类问题不支持此方法。

    您的问题并不清楚您是否真的打算解决多类、多输出问题。如果这不是您的意图,那么您应该重组您的输入数组。

    另一方面,如果您真的想解决这类问题,您只需定义自己的评分函数即可。

    更新

    由于您没有解决多类、多标签问题,因此您应该重组数据,使其看起来像这样:

    from sklearn.ensemble import RandomForestClassifier
    
    # training data
    X =  [
        [1,2,3,4,5,6,7,8,9],
        [1,2,3,4,5,6,7,8,9]
    ]
    
    y =  [0,1]
    
    # fit the model
    randomForest.fit(X,y)
    
    # test data
    Xtest =  [
        [1,2,0,4,5,6,0,8,9],
        [1,1,3,1,5,0,7,8,9]
    ]
    
    ytest =  [0,1]
    
    output =  randomForest.score(Xtest,ytest)
    print(output) # 0.5
    

    【讨论】:

    • restructure your input arrays: 什么意思,我应该做一维数组吗?
    • 您是否正在尝试解决多类、多标签问题?
    • 不,我还不知道,我只是在尝试一些东西,但现在让我们假设我不必解决多类多标签问题。
    • 在这种情况下,y 应该是一维数组,X 应该是二维数组。
    • 所以我将代码编辑为:output = randomForest.score(li_train1, li_text1[0]) 它给了我错误:ValueError: Found input variables with inconsistent numbers of samples: [9, 2] 知道为什么吗?
    猜你喜欢
    • 1970-01-01
    • 2020-12-07
    • 2016-03-21
    • 2020-06-12
    • 2020-02-07
    • 2018-06-04
    • 2021-07-26
    • 2011-12-23
    • 2012-12-18
    相关资源
    最近更新 更多