Files
mlessentials/Lab08/Exercise8.02/Exercise_8_2.ipynb
T
Your Name 54ccb1423f added
2021-02-08 11:17:02 +00:00

336 lines
7.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"executionInfo": {
"elapsed": 1517,
"status": "ok",
"timestamp": 1596621730956,
"user": {
"displayName": "Mahesh Dhyani",
"photoUrl": "",
"userId": "02713575687182950579"
},
"user_tz": -330
},
"id": "kHxgHJ7M-USR"
},
"outputs": [],
"source": [
"from sklearn import datasets, svm, model_selection"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"executionInfo": {
"elapsed": 2142,
"status": "ok",
"timestamp": 1596621731595,
"user": {
"displayName": "Mahesh Dhyani",
"photoUrl": "",
"userId": "02713575687182950579"
},
"user_tz": -330
},
"id": "POj6Wyt9-jMf"
},
"outputs": [],
"source": [
"# load data\n",
"digits = datasets.load_digits()\n",
"\n",
"# target\n",
"y = digits.target\n",
"\n",
"# features\n",
"X = digits.data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"executionInfo": {
"elapsed": 2144,
"status": "ok",
"timestamp": 1596621731603,
"user": {
"displayName": "Mahesh Dhyani",
"photoUrl": "",
"userId": "02713575687182950579"
},
"user_tz": -330
},
"id": "REhQ33GDkyzP"
},
"outputs": [],
"source": [
"# support vector machine classifier\n",
"clr = svm.SVC(gamma='scale')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"executionInfo": {
"elapsed": 2129,
"status": "ok",
"timestamp": 1596621731604,
"user": {
"displayName": "Mahesh Dhyani",
"photoUrl": "",
"userId": "02713575687182950579"
},
"user_tz": -330
},
"id": "sqIcOIZLFQH3"
},
"outputs": [],
"source": [
"# hyperparameter grid. contains linear and polynomial kernels\n",
"grid = [\n",
" {'kernel': ['linear']},\n",
" {'kernel': ['poly'], 'degree': [2, 3, 4]}\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"executionInfo": {
"elapsed": 2123,
"status": "ok",
"timestamp": 1596621731605,
"user": {
"displayName": "Mahesh Dhyani",
"photoUrl": "",
"userId": "02713575687182950579"
},
"user_tz": -330
},
"id": "_VVGUOSqLdRv"
},
"outputs": [],
"source": [
"# setting up the grid search to score on accuracy and evaluate over 10 folds\n",
"cv_spec = model_selection.GridSearchCV(estimator=clr, param_grid=grid, scoring='accuracy', cv=10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 230
},
"colab_type": "code",
"executionInfo": {
"elapsed": 4730,
"status": "ok",
"timestamp": 1596621734235,
"user": {
"displayName": "Mahesh Dhyani",
"photoUrl": "",
"userId": "02713575687182950579"
},
"user_tz": -330
},
"id": "YwxAHVSelu9X",
"outputId": "59845557-6880-4ac6-871f-daa9979382f9"
},
"outputs": [],
"source": [
"# start the grid search\n",
"cv_spec.fit(X, y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 55
},
"colab_type": "code",
"executionInfo": {
"elapsed": 4711,
"status": "ok",
"timestamp": 1596621734237,
"user": {
"displayName": "Mahesh Dhyani",
"photoUrl": "",
"userId": "02713575687182950579"
},
"user_tz": -330
},
"id": "llqjhh0SPGom",
"outputId": "876c3251-dc52-4087-d47d-5420b5cf76ea"
},
"outputs": [],
"source": [
"# what is the available information\n",
"print(cv_spec.cv_results_.keys())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 532
},
"colab_type": "code",
"executionInfo": {
"elapsed": 4691,
"status": "ok",
"timestamp": 1596621734238,
"user": {
"displayName": "Mahesh Dhyani",
"photoUrl": "",
"userId": "02713575687182950579"
},
"user_tz": -330
},
"id": "e9-x48AYQqBG",
"outputId": "f1825539-978c-4d9f-c53e-17a33390d06a"
},
"outputs": [],
"source": [
"cv_spec.cv_results_"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 106
},
"colab_type": "code",
"executionInfo": {
"elapsed": 4663,
"status": "ok",
"timestamp": 1596621734239,
"user": {
"displayName": "Mahesh Dhyani",
"photoUrl": "",
"userId": "02713575687182950579"
},
"user_tz": -330
},
"id": "YckzWb92QJt-",
"outputId": "a65f7018-a780-4b1a-85a4-4b8d0a613505"
},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"# convert the dictionary of results to a pandas dataframe\n",
"results = pd.DataFrame(cv_spec.cv_results_)\n",
"\n",
"print(\n",
"# show the hyperparameterizations\n",
"results.loc[:,['params','mean_test_score']].sort_values('mean_test_score', ascending=False)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"executionInfo": {
"elapsed": 3684,
"status": "ok",
"timestamp": 1596621734240,
"user": {
"displayName": "Mahesh Dhyani",
"photoUrl": "",
"userId": "02713575687182950579"
},
"user_tz": -330
},
"id": "J941cuZKnhXO"
},
"outputs": [],
"source": [
"# visualize the result\n",
"(\n",
" results.loc[:,['params','mean_test_score']]\n",
" .sort_values('mean_test_score', ascending=True)\n",
" .plot.barh(x='params', xlim=(0.8))\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "Exercise_8_2.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
}