mirror of
https://github.com/fenago/data-science.git
synced 2026-05-05 00:51:50 +00:00
238 lines
4.9 KiB
Plaintext
238 lines
4.9 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"colab_type": "text",
|
|
"id": "EiRcuZEgBUSr"
|
|
},
|
|
"source": [
|
|
"# Grid Search with Cross Validation"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "2B25z9iTBUSx"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# import libraries\n",
|
|
"import pandas as pd"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 266
|
|
},
|
|
"colab_type": "code",
|
|
"id": "3hi2YJ1YBUS6",
|
|
"outputId": "f6e6d95a-4e9c-4bb4-9441-86eaaf0d1fb5"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# data doesn't have headers, so let's create headers\n",
|
|
"_headers = ['buying', 'maint', 'doors', 'persons', 'lug_boot', 'safety', 'car']\n",
|
|
"# read in cars dataset\n",
|
|
"df = pd.read_csv('../Dataset/car.data', names=_headers, index_col=None)\n",
|
|
"df.info()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 214
|
|
},
|
|
"colab_type": "code",
|
|
"id": "Y4wiYrMdBUTI",
|
|
"outputId": "32726b67-d1fe-4feb-e0fd-85546b621abb"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# encode categorical variables\n",
|
|
"_df = pd.get_dummies(df, columns=['buying', 'maint', 'doors', 'persons', 'lug_boot', 'safety'])\n",
|
|
"_df.head()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "V8nt2KJZBUTS"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# separate features and labels DataFrames\n",
|
|
"features = _df.drop(['car'], axis=1).values\n",
|
|
"labels = _df[['car']].values"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "85aKwASjBUTa"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"from sklearn.tree import DecisionTreeClassifier\n",
|
|
"from sklearn.model_selection import GridSearchCV"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "rTR2pG1aBUTo"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"clf = DecisionTreeClassifier()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "nKGE6HIRBUTt"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"params = {'max_depth': np.arange(1, 8)}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "wrrJQttkBUT5"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"clf_cv = GridSearchCV(clf, param_grid=params, cv=5)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 319
|
|
},
|
|
"colab_type": "code",
|
|
"id": "tjkKzynOBUT_",
|
|
"outputId": "d03ac1a0-c96e-48cb-c8da-ec73f604182c"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"clf_cv.fit(features, labels)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 35
|
|
},
|
|
"colab_type": "code",
|
|
"id": "LN-GZzdoBUUO",
|
|
"outputId": "9900ce91-6df3-4bfe-bb0e-23a3dd29dc95"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"print(\"Tuned Decision Tree Parameters: {}\".format(clf_cv.best_params_))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 35
|
|
},
|
|
"colab_type": "code",
|
|
"id": "1vZftcIMBUUd",
|
|
"outputId": "cd468b70-0008-440f-c2d4-23d52e53995a"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"print(\"Best score is {}\".format(clf_cv.best_score_))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 124
|
|
},
|
|
"colab_type": "code",
|
|
"id": "9prZ_aBxBUUp",
|
|
"outputId": "755e6818-ab48-4490-c0a2-4d5b67dcfd1b"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"model = clf_cv.best_estimator_\n",
|
|
"model"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"name": "Exercise7.07.ipynb",
|
|
"provenance": []
|
|
},
|
|
"file_extension": ".py",
|
|
"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"
|
|
},
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"npconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": 3
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 1
|
|
}
|