这是我尝试运行的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,它们给了我一个很好的预测。
我不确定该怎么做才能解决这个问题。 感谢您的所有帮助。
回归器的预测方法需要一组特征向量。改变:
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]]
然后,代码将返回此一个特征向量的预测。
在我的情况下,没有定义类,我想使用 metrics.plot_roc_curve(model,X_test,y_test) 绘制roc_curve。因此,我遇到了同样的错误。我通过定义类解决了它。
因此,如果模型是您的分类器,例如
model = XGBClassifier(...)
您可以设置
model.classes_ = np.array([0,1])
如果您进行二元分类。