mirror of
https://github.com/fenago/data-science.git
synced 2026-11-05 05:22:47 +00:00
3.8 KiB
3.8 KiB
In [0]:
from sklearn import datasets, model_selection, linear_model
# load the data
diabetes = datasets.load_diabetes()
# target
y = diabetes.target
# features
X = diabetes.data
# initialise the ridge regression
reg = linear_model.Ridge()In [0]:
from scipy import stats
# alpha ~ gamma(1,1)
param_dist = {'alpha': stats.gamma(a=1, loc=1, scale=2)}In [9]:
# set up the random search to sample 100 values and score on negative mean squared error
rscv = model_selection.RandomizedSearchCV(estimator=reg, param_distributions=param_dist, n_iter=100, scoring='neg_mean_squared_error')
# start the search
rscv.fit(X,y)Out [9]:
/usr/local/lib/python3.6/dist-packages/sklearn/model_selection/_split.py:1978: FutureWarning: The default value of cv will change from 3 to 5 in version 0.22. Specify it explicitly to silence this warning. warnings.warn(CV_WARNING, FutureWarning)
RandomizedSearchCV(cv='warn', error_score='raise-deprecating',
estimator=Ridge(alpha=1.0, copy_X=True, fit_intercept=True,
max_iter=None, normalize=False,
random_state=None, solver='auto',
tol=0.001),
iid='warn', n_iter=100, n_jobs=None,
param_distributions={'alpha': <scipy.stats._distn_infrastructure.rv_frozen object at 0x7f6f66ae0550>},
pre_dispatch='2*n_jobs', random_state=None, refit=True,
return_train_score=False, scoring='neg_mean_squared_error',
verbose=0)In [10]:
import pandas as pd
# convert the results dictionary to a pandas data frame
results = pd.DataFrame(rscv.cv_results_)
# show the top 5 hyperparamaterizations
print(results.loc[:,['params','rank_test_score']].sort_values('rank_test_score').head(5)) params rank_test_score
47 {'alpha': 1.082807253775902} 1
72 {'alpha': 1.0996845885124735} 2
18 {'alpha': 1.1006233499723193} 3
52 {'alpha': 1.1012596885127424} 4
59 {'alpha': 1.1267717423087455} 5