scikit-learn AdaBoostRegressor IndexError:索引1超出轴0的大小为1

时间:2015-01-29 05:51:23

标签: python debugging scikit-learn

这是我正在尝试运行的python代码。

from numpy import *
import pylab as pl
from sklearn.utils import shuffle
from sklearn.metrics import mean_squared_error
from sklearn import datasets
from sklearn.ensemble import AdaBoostRegressor

boston = datasets.load_boston()
X, y = shuffle(boston.data, boston.target)
offset = int(0.7*len(X))
X_train, y_train = X[:offset], y[:offset]
X_test, y_test = X[offset:], y[offset:]

regressor = AdaBoostRegressor(n_estimators=5)
regressor.fit(X_train, y_train)

x = [11.95, 0.00, 18.100, 0, 0.6590, 5.6090, 90.00, 1.385, 24, 680.0, 20.20, 332.09, 12.13]
y = regressor.predict(x)
print "Prediction for " + str(x) + " = " + str(y)

这是我得到的错误。

Traceback (most recent call last):
  File "bug.py", line 18, in <module>
    y = regressor.predict(x)
  File "/usr/local/lib/python2.7/dist-packages/sklearn/ensemble/weight_boosting.py", line 1075, in predict
    return self._get_median_predict(X, len(self.estimators_))
  File "/usr/local/lib/python2.7/dist-packages/sklearn/ensemble/weight_boosting.py", line 1050, in _get_median_predict
    median_estimators = sorted_idx[np.arange(X.shape[0]), median_idx]
IndexError: index 1 is out of bounds for axis 0 with size 1

我可以将回归函数更改为KNeighborsRegressor或DecisionTreeRegressor,他们给我一个预测就好了。

我不知道该怎么办才能解决这个问题。感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

回归量的预测方法是期望一系列特征向量。变化:

x = [11.95, 0.00, 18.100, 0, 0.6590, 5.6090, 90.00, 1.385, 24, 680.0, 20.20, 332.09, 12.13]

为:

x = [[11.95, 0.00, 18.100, 0, 0.6590, 5.6090, 90.00, 1.385, 24, 680.0, 20.20, 332.09, 12.13]]

然后代码将返回此一个特征向量的预测。

答案 1 :(得分:0)

在我的例子中,类没有定义,我想使用 metrics.plot_roc_curve(model,X_test,y_test) 绘制 roc_curve。因此我遇到了同样的错误。我通过定义类来解决它。

因此,如果模型是您的分类器,例如

model = XGBClassifier(...)

你可以设置

model.classes_ = np.array([0,1])

如果您进行二元分类。