【问题标题】:ValueError: Invalid parameter alphas for estimator LassoValueError:估计器套索的无效参数 alpha
【发布时间】:2020-05-19 08:22:02
【问题描述】:

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

我是一名 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_'

感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    根据Lasso doc,您应该使用alpha。 其实就是修改:

    tuned_parameters = [{'alphas': alphas}]
    

    进入:

    tuned_parameters = [{'alpha': alphas}]
    

    您的代码应该可以工作。

    【讨论】:

      猜你喜欢
      • 2020-08-11
      • 2018-06-28
      • 2021-08-13
      • 2021-05-27
      • 2016-12-20
      • 2021-02-26
      • 2020-07-12
      • 2016-01-03
      • 2020-02-22
      相关资源
      最近更新 更多