Search code examples
pythonmachine-learningscikit-learnlasso-regressiongridsearchcv

ValueError: Invalid parameter alphas for estimator Lasso


this is my first time to post here.

I am a Python-Machine Learning newbie and I've been teaching myself with Scikit-Learn (v 0.22.1) in Jupyter Notebook(v 6.0.3). I would be very glad if you can help me out with this problem.

I copied this code exactly from auto_examples_python/datasets/plot_cv_diabetes.py (a downloadable file from scikit-learn 0.22.1) and this does not run on my Jupyter notebook:

    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)

It gives me the error:

 >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()`.

Also when I do this:

    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)

I get:

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

Thank you for your help.


Solution

  • According to Lasso doc you should use alpha. In fact, modifying:

    tuned_parameters = [{'alphas': alphas}]
    

    into:

    tuned_parameters = [{'alpha': alphas}]
    

    your code should work.