Files
mlessentials/Lab08/Examples/tuning_using_randomizedsearchcv.ipynb
T
Your Name 8ac470b565 notebooks
2021-02-05 20:44:51 +00:00

138 lines
3.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "om9yhBqIyrLD"
},
"outputs": [],
"source": [
"from sklearn import datasets, model_selection, linear_model\n",
"\n",
"# load the data\n",
"diabetes = datasets.load_diabetes()\n",
"\n",
"# target\n",
"y = diabetes.target\n",
"\n",
"# features\n",
"X = diabetes.data\n",
"\n",
"# initialise the ridge regression\n",
"reg = linear_model.Ridge()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "6VemB0iGzG8T"
},
"outputs": [],
"source": [
"from scipy import stats\n",
"\n",
"# alpha ~ gamma(1,1)\n",
"param_dist = {'alpha': stats.gamma(a=1, loc=1, scale=2)}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 245
},
"colab_type": "code",
"executionInfo": {
"elapsed": 2593,
"status": "ok",
"timestamp": 1571317362660,
"user": {
"displayName": "Andrew Worsley",
"photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mAp-Td-yKvu76Tg0Swzal8U17btuwNIXFmWVwZo=s64",
"userId": "11337101975325054847"
},
"user_tz": -660
},
"id": "Nni22PRlzUuL",
"outputId": "15deef80-db48-426c-9539-2fcd9e7e85ac"
},
"outputs": [],
"source": [
"# set up the random search to sample 100 values and score on negative mean squared error\n",
"rscv = model_selection.RandomizedSearchCV(estimator=reg, param_distributions=param_dist, n_iter=100, scoring='neg_mean_squared_error')\n",
"\n",
"# start the search\n",
"rscv.fit(X,y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 121
},
"colab_type": "code",
"executionInfo": {
"elapsed": 2586,
"status": "ok",
"timestamp": 1571317362662,
"user": {
"displayName": "Andrew Worsley",
"photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mAp-Td-yKvu76Tg0Swzal8U17btuwNIXFmWVwZo=s64",
"userId": "11337101975325054847"
},
"user_tz": -660
},
"id": "shd5Br5eznmb",
"outputId": "66d160c8-3dca-428b-9e34-e05f0044c9f3"
},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"# convert the results dictionary to a pandas data frame\n",
"results = pd.DataFrame(rscv.cv_results_)\n",
"\n",
"# show the top 5 hyperparamaterizations\n",
"print(results.loc[:,['params','rank_test_score']].sort_values('rank_test_score').head(5))"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "tuning_using_randomizedsearchcv.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 1
}