ValueError:估算器套索的无效参数Alpha

时间:2020-02-03 14:49:05

标签: python machine-learning scikit-learn lasso-regression gridsearchcv

这是我第一次在这里发布。

我是一名Python机器学习新手,我一直在Jupyter Notebook(v 6.0.3)中使用Scikit-Learn(v 0.22.1)自学。如果您能帮助我解决这个问题,我将非常高兴。

我完全从auto_examples_python / datasets / plot_cv_diabetes.py(从scikit-learn 0.22.1下载的文件)中复制了此代码,但该代码无法在我的Jupyter笔记本上运行:

    import numpy as np
    import matplotlib.pyplot as plt

    from sklearn import datasets
    from sklearn.linear_model import LassoCV, Lasso
    from sklearn.model_selection import GridSearchCV, KFold

    X, y = datasets.load_diabetes(return_X_y = True)
    X = X[:150]
    y = y[:150]

    lasso = Lasso(alpha = 1.0, random_state = 0, max_iter = 10000)
    alphas = np.logspace(-4, -0.5, 30)
    tuned_parameters = [{'alphas': alphas}]
    n_folds = 5

    clf = GridSearchCV(lasso, tuned_parameters, cv=n_folds, refit = False)

    clf.fit(X, y)

它给了我错误:

 >ValueError: Invalid parameter alphas for estimator Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=10000,
  normalize=False, positive=False, precompute=False, random_state=0,
  selection='cyclic', tol=0.0001, warm_start=False). Check the list of available parameters with `estimator.get_params().keys()`.

同样,当我这样做时:

    scores = clf.cv_results_['mean_test_score']
    scores_std = clf.cv_results_['std_test_score']

    plt.figure().set_size_inches(8, 6)
    plt.semilogx(alphas, score)

我得到:

   >AttributeError: 'GridSearchCV' object has no attribute 'cv_results_'

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

根据Lasso doc,您应该使用alpha。 实际上,修改:

tuned_parameters = [{'alphas': alphas}]

进入:

tuned_parameters = [{'alpha': alphas}]

您的代码应该可以使用。