【问题标题】:Got continuous is not supported error in RandomForestRegressorRandomForestRegressor 中不支持连续错误
【发布时间】:2015-12-16 08:27:45
【问题描述】:

我只是想做一个简单的 RandomForestRegressor 示例。但是在测试准确性时,我得到了这个错误

/Users/noppanit/anaconda/lib/python2.7/site-packages/sklearn/metrics/classification.pyc

在accuracy_score(y_true, y_pred, normalize, sample_weight) 177 178 # 计算每个可能表示的准确度 --> 179 y_type, y_true, y_pred = _check_targets(y_true, y_pred) 180 如果 y_type.startswith('multilabel'): 181 different_labels = count_nonzero(y_true - y_pred, axis=1)

/Users/noppanit/anaconda/lib/python2.7/site-packages/sklearn/metrics/classification.pyc

在 _check_targets(y_true, y_pred) 90 if (y_type 不在 ["binary", "multiclass", "multilabel-indicator", 91“多标签序列”]): ---> 92 raise ValueError("{0} is not supported".format(y_type)) 93 94 if y_type in ["binary", "multiclass"]:

ValueError: continuous is not supported

这是数据样本。我无法显示真实数据。

target, func_1, func_2, func_2, ... func_200
float, float, float, float, ... float

这是我的代码。

import pandas as pd
import numpy as np
from sklearn.preprocessing import Imputer
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor, ExtraTreesRegressor, GradientBoostingRegressor
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score
from sklearn import tree

train = pd.read_csv('data.txt', sep='\t')

labels = train.target
train.drop('target', axis=1, inplace=True)
cat = ['cat']
train_cat = pd.get_dummies(train[cat])

train.drop(train[cat], axis=1, inplace=True)
train = np.hstack((train, train_cat))

imp = Imputer(missing_values='NaN', strategy='mean', axis=0)
imp.fit(train)
train = imp.transform(train)

x_train, x_test, y_train, y_test = train_test_split(train, labels.values, test_size = 0.2)

clf = RandomForestRegressor(n_estimators=10)

clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
accuracy_score(y_test, y_pred) # This is where I get the error.

【问题讨论】:

    标签: python pandas dataframe scikit-learn random-forest


    【解决方案1】:

    由于您正在执行分类任务,因此您应该使用度量 R-squared (确定的协同效应) 而不是 准确度得分(准确度得分用于分类问题)。

    R-squared可以通过调用RandomForestRegressor提供的score函数来计算,例如:

    rfr.score(X_test,Y_test)
    

    【讨论】:

      【解决方案2】:

      这是因为accuracy_score 仅用于分类任务。 对于回归,您应该使用不同的东西,例如:

      clf.score(X_test, y_test)
      

      X_test 是样本,y_test 是对应的真实值。它将在内部计算预测。

      【讨论】:

      • 有谁知道如何比较预测值和测试值,例如回归分类?
      • @Priyansh 您在回归中使用 R-squared(确定系数)比较预测值和测试值
      猜你喜欢
      • 1970-01-01
      • 2019-04-19
      • 2016-01-07
      • 2021-07-09
      • 2017-11-12
      • 2016-09-30
      • 2018-05-19
      • 2020-05-31
      相关资源
      最近更新 更多