简体   繁体   English

遍历每个元素并使用该元素作为GBM中的输出列名称

[英]Loop through each element and use the element as the output column name in GBM

I want to loop through a list of parameters in gbm and generate a dataframe that records results of each parameter combination. 我想循环遍历gbm中的参数列表,并生成一个记录每个参数组合结果的数据帧。

Below is my code: 以下是我的代码:

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)

This is the error that I got: 这是我得到的错误:

    totalreturn_annual[p] = totalreturn_annual.append(totalreturn_annual)

IndexError: list assignment index out of range

I wonder why I got the error. 我想知道为什么我得到错误。

You initialize totalreturn_annual as an empty list in the first line of your code, thus it will not take an index. 您将totalreturn_annual初始化为代码第一行中的空列表,因此它不会采用索引。 This is resulting in the index error. 这导致索引错误。 This should work: 这应该工作:

np.append(totalreturn_annual, totalreturn_annual_temp)

instead of: 代替:

totalreturn_annual[p] = np.append(totalreturn_annual, totalreturn_annual_temp)

The np.append(a,b) function appends b to a; np.append(a,b)函数将b附加到a; a is modified in place. a被修改到位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM