RandomForestRegressor 中出现连续不支持错误

2024-03-03

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

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

在accuracy_score(y_true,y_pred,标准化,样本权重)中 177 攀上漂亮女局长之后178 --> 179 y_type, y_true, y_pred = _check_targets(y_true, y_pred) 180 if y_type.startswith('multilabel'): [第 181 章]

/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}".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.

这是因为准确率_分数 http://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html#sklearn.metrics.accuracy_score仅用于分类任务。 对于回归,您应该使用不同的东西,例如:

clf.score(X_test, y_test)

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

RandomForestRegressor 中出现连续不支持错误 的相关文章

随机推荐