gpt4 book ai didi

python - scikit 学习 AdaBoostRegressor IndexError : index 1 is out of bounds for axis 0 with size 1

转载 作者:行者123 更新时间:2023-11-28 18:39:34 24 4
gpt4 key购买 nike

这是我要运行的 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,它们会给我一个很好的预测。

我不确定该怎么做才能解决这个问题。感谢所有帮助。

最佳答案

regressor 的预测方法需要一组特征向量。变化:

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]]

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

关于python - scikit 学习 AdaBoostRegressor IndexError : index 1 is out of bounds for axis 0 with size 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28207805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com