如何在python中修复“ IndexError:元组索引超出范围”?

时间:2019-07-16 15:40:11

标签: python numpy scikit-learn grid-search

我正在使用sklearn模块来查找最佳拟合模型和模型参数。但是,我在下面有一个意外的索引错误:

> IndexError                                Traceback (most recent call
> last) <ipython-input-38-ea3f99e30226> in <module>
>      22             s = mean_squared_error(y[ts], best_m.predict(X[ts]))
>      23             cv[i].append(s)
> ---> 24     print(np.mean(cv, 1))
> IndexError: tuple index out of range

我想做的是找到最合适的回归变量及其参数,但是我遇到了以上错误。我调查了SO并尝试了this solution,但仍然出现了同样的错误。有解决此错误的想法吗?谁能指出我为什么会发生此错误?有什么想法吗?

我的代码

from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from xgboost.sklearn import XGBRegressor

from sklearn.datasets import make_regression

models = [SVR(), RandomForestRegressor(), LinearRegression(), Ridge(), Lasso(), XGBRegressor()]
params = [{'C': [0.01, 1]}, {'n_estimators': [10, 20]}]

X, y = make_regression(n_samples=10000, n_features=20)

with warnings.catch_warnings():
    warnings.filterwarnings("ignore")
    cv = [[] for _ in range(len(models))]
    fold = KFold(5,shuffle=False)
    for tr, ts in fold.split(X):
        for i, (model, param) in enumerate(zip(models, params)):
            best_m = GridSearchCV(model, param)
            best_m.fit(X[tr], y[tr])
            s = mean_squared_error(y[ts], best_m.predict(X[ts]))
            cv[i].append(s)
    print(np.mean(cv, 1))

所需的输出

如果有一种方法可以解决上述错误,那么我希望选择具有参数的最佳拟合模型,然后将其用于估算。有什么想法可以改善上述尝试吗?谢谢

2 个答案:

答案 0 :(得分:1)

定义时

cv = [[] for _ in range(len(models))]

每个型号都有一个空列表。 但是,在循环中,您经过enumerate(zip(models, params))仅具有两个元素,因为您的params列表具有两个元素(因为list(zip(x,y)) has length等于min(len(x),len(y))。

因此,您得到一个IndexError,因为在用cv计算平均值时,np.mean中的某些列表为空(除了前两个以外)。

解决方案: 如果您不需要在其余模型上使用GridSearchCV,则可以使用空字典扩展params列表:

params = [{'C': [0.01, 1]}, {'n_estimators': [10, 20]}, {}, {}, {}, {}]

答案 1 :(得分:1)

问题的根本原因是,当您要求评估GridSearchCV中的6个模型时,您仅提供前两个模型的参数:

models = [SVR(), RandomForestRegressor(), LinearRegression(), Ridge(), Lasso(), XGBRegressor()]
params = [{'C': [0.01, 1]}, {'n_estimators': [10, 20]}]

在此设置中enumerate(zip(models, params))的结果,即:

for i, (model, param) in enumerate(zip(models, params)):
    print((model, param))

(SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto',
  kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False), {'C': [0.01, 1]})
(RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=None,
           max_features='auto', max_leaf_nodes=None,
           min_impurity_decrease=0.0, min_impurity_split=None,
           min_samples_leaf=1, min_samples_split=2,
           min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,
           oob_score=False, random_state=None, verbose=0, warm_start=False), {'n_estimators': [10, 20]})

即,仅忽略了最后4个模型,因此您在cv中会得到空的条目:

print(cv)
# result:
[[5950.6018771284835, 5987.293514740653, 6055.368320208183, 6099.316091619069, 6146.478702335218], [3625.3243553665975, 3301.3552182952058, 3404.3321983193728, 3521.5160621260898, 3561.254684271113], [], [], [], []]

会在尝试获取np.mean(cv, 1)时导致下游错误。

Psi在其答案中已经正确指出,解决方案是在您实际上执行任何CV搜索的模型中使用空字典。省略XGBRegressor(尚未安装),结果如下:

models = [SVR(), RandomForestRegressor(), LinearRegression(), Ridge(), Lasso()]
params2 = [{'C': [0.01, 1]}, {'n_estimators': [10, 20]}, {}, {}, {}]

cv = [[] for _ in range(len(models))]
fold = KFold(5,shuffle=False)
for tr, ts in fold.split(X):
    for i, (model, param) in enumerate(zip(models, params2)):
        best_m = GridSearchCV(model, param)
        best_m.fit(X[tr], y[tr])
        s = mean_squared_error(y[ts], best_m.predict(X[ts]))
        cv[i].append(s)

其中print(cv)给出:

[[4048.660483326826, 3973.984055352062, 3847.7215568088545, 3907.0566348092684, 3820.0517432992765], [1037.9378737329769, 1025.237441119364, 1016.549294695313, 993.7083268195154, 963.8115632611381], [2.2948917095935095e-26, 1.971022007799432e-26, 4.1583774042712844e-26, 2.0229469068846665e-25, 1.9295075684919642e-26], [0.0003350178681602639, 0.0003297411022124562, 0.00030834076832371557, 0.0003355298330301431, 0.00032049282437794516], [10.372789356303688, 10.137748082073076, 10.136028304131141, 10.499159069700834, 9.80779910439471]]

print(np.mean(cv, 1))可以正常工作,给出:

[3.91949489e+03 1.00744890e+03 6.11665355e-26 3.25824479e-04
 1.01907048e+01]

因此,就您而言,您确实应该将params更改为:

params = [{'C': [0.01, 1]}, {'n_estimators': [10, 20]}, {}, {}, {}, {}]

如Psi所建议。