Skip to content

Instantly share code, notes, and snippets.

@jimbonant
Created June 2, 2022 13:51
Show Gist options
  • Save jimbonant/793a79b7cdc8378eb7ac6034c1f9bbaf to your computer and use it in GitHub Desktop.
Save jimbonant/793a79b7cdc8378eb7ac6034c1f9bbaf to your computer and use it in GitHub Desktop.
HyperParamTuningTest.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "HyperParamTuningTest.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyN5O+ieODWs+gm4J6+Yeupn",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/jimbonant/793a79b7cdc8378eb7ac6034c1f9bbaf/hyperparamtuningtest.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"id": "HnGn68STZZGo"
},
"outputs": [],
"source": [
"from sklearn import svm, datasets\n",
"import pandas as pd\n"
]
},
{
"cell_type": "code",
"source": [
"data = datasets.load_iris()"
],
"metadata": {
"id": "MYrCT7faZmTb"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"df = pd.DataFrame(data.data, columns=data.feature_names)"
],
"metadata": {
"id": "ew1GJjQTG1oY"
},
"execution_count": 8,
"outputs": []
},
{
"cell_type": "code",
"source": [
"\n",
"df['flower'] = data.target\n",
"df['flower'] = df['flower'].apply(lambda x:data.target_names[x])"
],
"metadata": {
"id": "9SKfSATvHrla"
},
"execution_count": 17,
"outputs": []
},
{
"cell_type": "code",
"source": [
"df[47:52]"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
},
"id": "Dj8WA8A3CxEr",
"outputId": "e1661fb4-3570-4110-aec2-5677c9ebde33"
},
"execution_count": 18,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) \\\n",
"47 4.6 3.2 1.4 0.2 \n",
"48 5.3 3.7 1.5 0.2 \n",
"49 5.0 3.3 1.4 0.2 \n",
"50 7.0 3.2 4.7 1.4 \n",
"51 6.4 3.2 4.5 1.5 \n",
"\n",
" flower \n",
"47 setosa \n",
"48 setosa \n",
"49 setosa \n",
"50 versicolor \n",
"51 versicolor "
],
"text/html": [
"\n",
" <div id=\"df-6fc2d915-d071-4cbf-a47f-400b32548a21\">\n",
" <div class=\"colab-df-container\">\n",
" <div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal length (cm)</th>\n",
" <th>sepal width (cm)</th>\n",
" <th>petal length (cm)</th>\n",
" <th>petal width (cm)</th>\n",
" <th>flower</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>47</th>\n",
" <td>4.6</td>\n",
" <td>3.2</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>48</th>\n",
" <td>5.3</td>\n",
" <td>3.7</td>\n",
" <td>1.5</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>49</th>\n",
" <td>5.0</td>\n",
" <td>3.3</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>50</th>\n",
" <td>7.0</td>\n",
" <td>3.2</td>\n",
" <td>4.7</td>\n",
" <td>1.4</td>\n",
" <td>versicolor</td>\n",
" </tr>\n",
" <tr>\n",
" <th>51</th>\n",
" <td>6.4</td>\n",
" <td>3.2</td>\n",
" <td>4.5</td>\n",
" <td>1.5</td>\n",
" <td>versicolor</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-6fc2d915-d071-4cbf-a47f-400b32548a21')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
" \n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
" </svg>\n",
" </button>\n",
" \n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" flex-wrap:wrap;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-6fc2d915-d071-4cbf-a47f-400b32548a21 button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-6fc2d915-d071-4cbf-a47f-400b32548a21');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
" </div>\n",
" "
]
},
"metadata": {},
"execution_count": 18
}
]
},
{
"cell_type": "code",
"source": [
"from sklearn.model_selection import train_test_split\n",
"X_train, X_test, Y_train, Y_test = train_test_split(data.data, data.target, test_size=.3)"
],
"metadata": {
"id": "Qj1DzecmLTCT"
},
"execution_count": 20,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
""
],
"metadata": {
"id": "dnjOkj_qM3PI"
}
},
{
"cell_type": "code",
"source": [
"model = svm.SVC(kernel='rbf', C=30, gamma='auto')\n",
"model.fit(X_train,Y_train)\n",
"model.score(X_test, Y_test)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "XALE2PVTM5eI",
"outputId": "fb9b32e9-b45e-4892-d6a7-568a536e9f3c"
},
"execution_count": 24,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.9333333333333333"
]
},
"metadata": {},
"execution_count": 24
}
]
},
{
"cell_type": "code",
"source": [
"from sklearn.model_selection import cross_val_score\n",
"\n",
"cross_val_score(svm.SVC(kernel='linear', C=10,gamma='auto'), data.data, data.target,cv=5)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "d2khA1L0Tjj1",
"outputId": "ac3500bd-6da5-4798-b301-791b3846a146"
},
"execution_count": 27,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([1. , 1. , 0.9 , 0.96666667, 1. ])"
]
},
"metadata": {},
"execution_count": 27
}
]
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "IkMt-3V0W3n0"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"cross_val_score(svm.SVC(kernel='rbf', C=10,gamma='auto'), data.data, data.target,cv=5)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "7b196110-2a02-4257-ba14-2841d14f95a1",
"id": "f7sjLEijW36w"
},
"execution_count": 28,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([0.96666667, 1. , 0.96666667, 0.96666667, 1. ])"
]
},
"metadata": {},
"execution_count": 28
}
]
},
{
"cell_type": "code",
"source": [
"cross_val_score(svm.SVC(kernel='rbf', C=20,gamma='auto'), data.data, data.target,cv=5)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "HGpvUINiXv-L",
"outputId": "f644bbac-ff00-4bc8-a770-2684571bd51b"
},
"execution_count": 29,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([0.96666667, 1. , 0.9 , 0.96666667, 1. ])"
]
},
"metadata": {},
"execution_count": 29
}
]
},
{
"cell_type": "code",
"source": [
"from sklearn.model_selection import GridSearchCV"
],
"metadata": {
"id": "YpVduFXnYy6S"
},
"execution_count": 30,
"outputs": []
},
{
"cell_type": "code",
"source": [
"clf = GridSearchCV(svm.SVC(gamma='auto'), {\n",
" 'C':[1,10,20],\n",
" 'kernel' : ['rbf', 'linear']\n",
"}, cv=5, return_train_score=False)\n",
"clf.fit(data.data,data.target) "
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Z0f5yKqJY5MP",
"outputId": "035d46a8-fc45-46d3-cf42-c6513c7ddad2"
},
"execution_count": 32,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"GridSearchCV(cv=5, estimator=SVC(gamma='auto'),\n",
" param_grid={'C': [1, 10, 20], 'kernel': ['rbf', 'linear']})"
]
},
"metadata": {},
"execution_count": 32
}
]
},
{
"cell_type": "code",
"source": [
"df2 = pd.DataFrame(clf.cv_results_)\n",
"df2"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 452
},
"id": "FyCoT6jLZjqt",
"outputId": "099a58aa-3d65-406e-c68a-1943be3f2476"
},
"execution_count": 34,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" mean_fit_time std_fit_time mean_score_time std_score_time param_C \\\n",
"0 0.002222 0.001512 0.000770 0.000059 1 \n",
"1 0.001299 0.000448 0.000568 0.000027 1 \n",
"2 0.001315 0.000028 0.000669 0.000025 10 \n",
"3 0.001061 0.000052 0.000588 0.000056 10 \n",
"4 0.001287 0.000082 0.000697 0.000070 20 \n",
"5 0.001086 0.000136 0.000604 0.000020 20 \n",
"\n",
" param_kernel params split0_test_score \\\n",
"0 rbf {'C': 1, 'kernel': 'rbf'} 0.966667 \n",
"1 linear {'C': 1, 'kernel': 'linear'} 0.966667 \n",
"2 rbf {'C': 10, 'kernel': 'rbf'} 0.966667 \n",
"3 linear {'C': 10, 'kernel': 'linear'} 1.000000 \n",
"4 rbf {'C': 20, 'kernel': 'rbf'} 0.966667 \n",
"5 linear {'C': 20, 'kernel': 'linear'} 1.000000 \n",
"\n",
" split1_test_score split2_test_score split3_test_score split4_test_score \\\n",
"0 1.0 0.966667 0.966667 1.0 \n",
"1 1.0 0.966667 0.966667 1.0 \n",
"2 1.0 0.966667 0.966667 1.0 \n",
"3 1.0 0.900000 0.966667 1.0 \n",
"4 1.0 0.900000 0.966667 1.0 \n",
"5 1.0 0.900000 0.933333 1.0 \n",
"\n",
" mean_test_score std_test_score rank_test_score \n",
"0 0.980000 0.016330 1 \n",
"1 0.980000 0.016330 1 \n",
"2 0.980000 0.016330 1 \n",
"3 0.973333 0.038873 4 \n",
"4 0.966667 0.036515 5 \n",
"5 0.966667 0.042164 6 "
],
"text/html": [
"\n",
" <div id=\"df-2f765e23-d6b9-487a-ba3f-ac0aa18ab77f\">\n",
" <div class=\"colab-df-container\">\n",
" <div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>mean_fit_time</th>\n",
" <th>std_fit_time</th>\n",
" <th>mean_score_time</th>\n",
" <th>std_score_time</th>\n",
" <th>param_C</th>\n",
" <th>param_kernel</th>\n",
" <th>params</th>\n",
" <th>split0_test_score</th>\n",
" <th>split1_test_score</th>\n",
" <th>split2_test_score</th>\n",
" <th>split3_test_score</th>\n",
" <th>split4_test_score</th>\n",
" <th>mean_test_score</th>\n",
" <th>std_test_score</th>\n",
" <th>rank_test_score</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.002222</td>\n",
" <td>0.001512</td>\n",
" <td>0.000770</td>\n",
" <td>0.000059</td>\n",
" <td>1</td>\n",
" <td>rbf</td>\n",
" <td>{'C': 1, 'kernel': 'rbf'}</td>\n",
" <td>0.966667</td>\n",
" <td>1.0</td>\n",
" <td>0.966667</td>\n",
" <td>0.966667</td>\n",
" <td>1.0</td>\n",
" <td>0.980000</td>\n",
" <td>0.016330</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.001299</td>\n",
" <td>0.000448</td>\n",
" <td>0.000568</td>\n",
" <td>0.000027</td>\n",
" <td>1</td>\n",
" <td>linear</td>\n",
" <td>{'C': 1, 'kernel': 'linear'}</td>\n",
" <td>0.966667</td>\n",
" <td>1.0</td>\n",
" <td>0.966667</td>\n",
" <td>0.966667</td>\n",
" <td>1.0</td>\n",
" <td>0.980000</td>\n",
" <td>0.016330</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.001315</td>\n",
" <td>0.000028</td>\n",
" <td>0.000669</td>\n",
" <td>0.000025</td>\n",
" <td>10</td>\n",
" <td>rbf</td>\n",
" <td>{'C': 10, 'kernel': 'rbf'}</td>\n",
" <td>0.966667</td>\n",
" <td>1.0</td>\n",
" <td>0.966667</td>\n",
" <td>0.966667</td>\n",
" <td>1.0</td>\n",
" <td>0.980000</td>\n",
" <td>0.016330</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.001061</td>\n",
" <td>0.000052</td>\n",
" <td>0.000588</td>\n",
" <td>0.000056</td>\n",
" <td>10</td>\n",
" <td>linear</td>\n",
" <td>{'C': 10, 'kernel': 'linear'}</td>\n",
" <td>1.000000</td>\n",
" <td>1.0</td>\n",
" <td>0.900000</td>\n",
" <td>0.966667</td>\n",
" <td>1.0</td>\n",
" <td>0.973333</td>\n",
" <td>0.038873</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.001287</td>\n",
" <td>0.000082</td>\n",
" <td>0.000697</td>\n",
" <td>0.000070</td>\n",
" <td>20</td>\n",
" <td>rbf</td>\n",
" <td>{'C': 20, 'kernel': 'rbf'}</td>\n",
" <td>0.966667</td>\n",
" <td>1.0</td>\n",
" <td>0.900000</td>\n",
" <td>0.966667</td>\n",
" <td>1.0</td>\n",
" <td>0.966667</td>\n",
" <td>0.036515</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>0.001086</td>\n",
" <td>0.000136</td>\n",
" <td>0.000604</td>\n",
" <td>0.000020</td>\n",
" <td>20</td>\n",
" <td>linear</td>\n",
" <td>{'C': 20, 'kernel': 'linear'}</td>\n",
" <td>1.000000</td>\n",
" <td>1.0</td>\n",
" <td>0.900000</td>\n",
" <td>0.933333</td>\n",
" <td>1.0</td>\n",
" <td>0.966667</td>\n",
" <td>0.042164</td>\n",
" <td>6</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-2f765e23-d6b9-487a-ba3f-ac0aa18ab77f')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
" \n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
" </svg>\n",
" </button>\n",
" \n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" flex-wrap:wrap;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-2f765e23-d6b9-487a-ba3f-ac0aa18ab77f button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-2f765e23-d6b9-487a-ba3f-ac0aa18ab77f');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
" </div>\n",
" "
]
},
"metadata": {},
"execution_count": 34
}
]
},
{
"cell_type": "code",
"source": [
"from sklearn.model_selection import RandomizedSearchCV\n",
"clf = RandomizedSearchCV(svm.SVC(gamma='auto'), {\n",
" 'C':[1,10,20],\n",
" 'kernel' : ['rbf', 'linear']\n",
"}, cv=5, return_train_score=False, n_iter=2)\n",
"clf.fit(data.data,data.target) "
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vBTExP6shl2L",
"outputId": "a2affed8-d66f-44ca-f44e-8b683066e68a"
},
"execution_count": 50,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"RandomizedSearchCV(cv=5, estimator=SVC(gamma='auto'), n_iter=2,\n",
" param_distributions={'C': [1, 10, 20],\n",
" 'kernel': ['rbf', 'linear']})"
]
},
"metadata": {},
"execution_count": 50
}
]
},
{
"cell_type": "code",
"source": [
"df = pd.DataFrame(clf.cv_results_)\n",
"df"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 246
},
"id": "BCC66zu2e1WB",
"outputId": "1c492ac0-ce9f-47cc-a6ea-f9716891966f"
},
"execution_count": 51,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" mean_fit_time std_fit_time mean_score_time std_score_time param_kernel \\\n",
"0 0.000822 0.000246 0.000514 0.000296 linear \n",
"1 0.000921 0.000197 0.000755 0.000633 rbf \n",
"\n",
" param_C params split0_test_score \\\n",
"0 20 {'kernel': 'linear', 'C': 20} 1.000000 \n",
"1 20 {'kernel': 'rbf', 'C': 20} 0.966667 \n",
"\n",
" split1_test_score split2_test_score split3_test_score split4_test_score \\\n",
"0 1.0 0.9 0.933333 1.0 \n",
"1 1.0 0.9 0.966667 1.0 \n",
"\n",
" mean_test_score std_test_score rank_test_score \n",
"0 0.966667 0.042164 2 \n",
"1 0.966667 0.036515 1 "
],
"text/html": [
"\n",
" <div id=\"df-ff0284ea-c51d-4229-9b49-346d9200f128\">\n",
" <div class=\"colab-df-container\">\n",
" <div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>mean_fit_time</th>\n",
" <th>std_fit_time</th>\n",
" <th>mean_score_time</th>\n",
" <th>std_score_time</th>\n",
" <th>param_kernel</th>\n",
" <th>param_C</th>\n",
" <th>params</th>\n",
" <th>split0_test_score</th>\n",
" <th>split1_test_score</th>\n",
" <th>split2_test_score</th>\n",
" <th>split3_test_score</th>\n",
" <th>split4_test_score</th>\n",
" <th>mean_test_score</th>\n",
" <th>std_test_score</th>\n",
" <th>rank_test_score</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.000822</td>\n",
" <td>0.000246</td>\n",
" <td>0.000514</td>\n",
" <td>0.000296</td>\n",
" <td>linear</td>\n",
" <td>20</td>\n",
" <td>{'kernel': 'linear', 'C': 20}</td>\n",
" <td>1.000000</td>\n",
" <td>1.0</td>\n",
" <td>0.9</td>\n",
" <td>0.933333</td>\n",
" <td>1.0</td>\n",
" <td>0.966667</td>\n",
" <td>0.042164</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.000921</td>\n",
" <td>0.000197</td>\n",
" <td>0.000755</td>\n",
" <td>0.000633</td>\n",
" <td>rbf</td>\n",
" <td>20</td>\n",
" <td>{'kernel': 'rbf', 'C': 20}</td>\n",
" <td>0.966667</td>\n",
" <td>1.0</td>\n",
" <td>0.9</td>\n",
" <td>0.966667</td>\n",
" <td>1.0</td>\n",
" <td>0.966667</td>\n",
" <td>0.036515</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-ff0284ea-c51d-4229-9b49-346d9200f128')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
" \n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
" </svg>\n",
" </button>\n",
" \n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" flex-wrap:wrap;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-ff0284ea-c51d-4229-9b49-346d9200f128 button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-ff0284ea-c51d-4229-9b49-346d9200f128');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
" </div>\n",
" "
]
},
"metadata": {},
"execution_count": 51
}
]
},
{
"cell_type": "code",
"source": [
"\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.linear_model import LogisticRegression"
],
"metadata": {
"id": "WMGkDsybmjnv"
},
"execution_count": 59,
"outputs": []
},
{
"cell_type": "code",
"source": [
"model_params = {\n",
" 'svm': {\n",
" 'model' : svm.SVC(gamma='auto'),\n",
" 'params' : {\n",
" 'C' : [1,10,20],\n",
" 'kernel' : ['rbf', 'linear']\n",
" }\n",
" },\n",
" 'random-forest': {\n",
" 'model' : RandomForestClassifier(),\n",
" 'params' : {\n",
" 'n_estimators': [1,5,10]\n",
" }\n",
" },\n",
" 'logistic_regression' : {\n",
" 'model': LogisticRegression(solver='liblinear', multi_class='auto'),\n",
" 'params' : {\n",
" 'C' : [1,5,10]\n",
" }\n",
" }\n",
"}"
],
"metadata": {
"id": "3fFrygclTqvW"
},
"execution_count": 60,
"outputs": []
},
{
"cell_type": "code",
"source": [
"scores = []\n",
"\n",
"for model_name, mp in model_params.items():\n",
" clf = GridSearchCV(mp['model'], mp['params'], cv=5, return_train_score=False)\n",
" clf.fit(data.data, data.target)\n",
" scores.append(\n",
" {\n",
" 'model': model_name,\n",
" 'best_score': clf.best_score_,\n",
" 'best_params' : clf.best_params_\n",
" }\n",
" )"
],
"metadata": {
"id": "HVKUNk3woa1d"
},
"execution_count": 61,
"outputs": []
},
{
"cell_type": "code",
"source": [
"df = pd.DataFrame(scores, columns = ['model','best_score','best_params'])\n",
"df"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 143
},
"id": "HE0fFscTp3Yx",
"outputId": "de41b93b-8498-45c2-f79b-fcaa1d662d3d"
},
"execution_count": 62,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" model best_score best_params\n",
"0 svm 0.980000 {'C': 1, 'kernel': 'rbf'}\n",
"1 random-forest 0.966667 {'n_estimators': 10}\n",
"2 logistic_regression 0.966667 {'C': 5}"
],
"text/html": [
"\n",
" <div id=\"df-a5122d2b-2c34-43c4-8d99-62e192dd2e1e\">\n",
" <div class=\"colab-df-container\">\n",
" <div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>model</th>\n",
" <th>best_score</th>\n",
" <th>best_params</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>svm</td>\n",
" <td>0.980000</td>\n",
" <td>{'C': 1, 'kernel': 'rbf'}</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>random-forest</td>\n",
" <td>0.966667</td>\n",
" <td>{'n_estimators': 10}</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>logistic_regression</td>\n",
" <td>0.966667</td>\n",
" <td>{'C': 5}</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-a5122d2b-2c34-43c4-8d99-62e192dd2e1e')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
" \n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
" </svg>\n",
" </button>\n",
" \n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" flex-wrap:wrap;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-a5122d2b-2c34-43c4-8d99-62e192dd2e1e button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-a5122d2b-2c34-43c4-8d99-62e192dd2e1e');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
" </div>\n",
" "
]
},
"metadata": {},
"execution_count": 62
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment