%pylab inline from sklearn.datasets import load_diabetes data = load_diabetes() X, y = data.data, data.target print X.shape print y.shape from sklearn.linear_model import Ridge, Lasso from sklearn.cross_validation import cross_val_score for Model in [Ridge, Lasso]: model = Model() print Model.__name__, cross_val_score(model, X, y).mean() alphas = np.logspace(-3, -1, 30) # plot the mean cross-validation score for a Ridge estimator and a Lasso estimator # as a function of alpha. Which is more difficult to tune? %load solutions/06B_basic_grid_search.py from sklearn.grid_search import GridSearchCV for Model in [Ridge, Lasso]: gscv = GridSearchCV(Model(), dict(alpha=alphas), cv=3).fit(X, y) print Model.__name__, gscv.best_params_ from sklearn.linear_model import RidgeCV, LassoCV for Model in [RidgeCV, LassoCV]: model = Model(alphas=alphas, cv=3).fit(X, y) print Model.__name__, model.alpha_ from sklearn.metrics import mean_squared_error # define a function that computes the learning curve (i.e. mean_squared_error as a function # of training set size, for both training and test sets) and plot the result %load solutions/06B_learning_curves.py