python - 在 RandomForestRegressor 中得到连续不支持的错误

标签 python pandas dataframe scikit-learn random-forest

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

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

in accuracy_score(y_true, y_pred, normalize, sample_weight) 177 178 # Compute accuracy for each possible representation --> 179 y_type, y_true, y_pred = _check_targets(y_true, y_pred) 180 if y_type.startswith('multilabel'): 181 differing_labels = count_nonzero(y_true - y_pred, axis=1)

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

in _check_targets(y_true, y_pred) 90 if (y_type not in ["binary", "multiclass", "multilabel-indicator", 91 "multilabel-sequences"]): ---> 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.

最佳答案

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

clf.score(X_test, y_test)

其中X_test是样本,y_test是相应的ground truth值。它将在内部计算预测。

关于python - 在 RandomForestRegressor 中得到连续不支持的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32664717/

相关文章:

python - 在具有层次索引的 Pandas 数据框中使用 iloc 时遇到问题

python - DynamoDb 安全更新

python - 分组后折叠数据框 pandas

python - 查找具有非 na 值的列,并使用非 na 列的名称创建第三列填充值

python-3.x - 根据列将多个无标题列中的值替换为 0、1、2

python - Python 异常后重启线程

python - 使用 aggfunc=sum 的 Pandas Dataframes 值计算在几列上

python - Pandas:编辑数据框的一部分,使其影响主数据框

python - withColumn 与 UDF 产生 AttributeError : 'NoneType' object has no attribute '_jvm'

Python:如何在文件的同一行继续写入?