我想遍历gbm中的参数列表,并生成一个记录每个参数组合结果的数据框。
下面是我的代码:
from sklearn.ensemble import GradientBoostingRegressor
import pandas as pd
totalreturn_annual = []
params = {'n_estimators': [1, 10, 50, 100, 200], 'max_depth': [1,3,5,7,9],
'learning_rate': [0.01,0.05,0.1,0.2,0.3], 'min_samples_split ':[0.1,0.3,0.5,0.7,0.9]}
params = pd.Dataframe(params)
for p in range(16):
model_cape = GradientBoostingRegressor(random_state = 10, max_features = 'sqrt',
n_estimators = params.iloc[p,0], learning_rate = params.iloc[p,2],
alpha = params.iloc[p,3], max_depth = params.iloc[p,1],).fit(xs, ys_cape)
totalreturn_annual[p] = np.append(totalreturn_annual, totalreturn_annual_temp)
这是我得到的错误:
totalreturn_annual[p] = totalreturn_annual.append(totalreturn_annual)
IndexError: list assignment index out of range
我想知道为什么会收到错误消息。
答案 0 :(得分:0)
您将totalreturn_annual
初始化为代码第一行中的空列表,因此不会使用索引。这导致索引错误。这应该起作用:
np.append(totalreturn_annual, totalreturn_annual_temp)
代替:
totalreturn_annual[p] = np.append(totalreturn_annual, totalreturn_annual_temp)
np.append(a,b)函数将b附加到a;一个被修改。