mirror of
https://github.com/fenago/data-science.git
synced 2026-05-05 00:51:50 +00:00
238 lines
5.0 KiB
Plaintext
238 lines
5.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"colab_type": "text",
|
|
"id": "RRCLZIC_rtl4"
|
|
},
|
|
"source": [
|
|
"# Randomized Search with Cross Validation"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "avRjeBTHrtl6"
|
|
},
|
|
"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": "r7AT180zrtl_",
|
|
"outputId": "4c755e98-711e-4230-9a6b-5c106927808f"
|
|
},
|
|
"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": "JtepJmqyrtmE",
|
|
"outputId": "ef50f2d7-2b5e-47da-a71f-64d64da0ad7d"
|
|
},
|
|
"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": "9Ku1uPghrtmJ"
|
|
},
|
|
"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": "U8qIrVjtrtmO"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"from sklearn.ensemble import RandomForestClassifier\n",
|
|
"from sklearn.model_selection import RandomizedSearchCV"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "ICgnbVyHrtmT"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"clf = RandomForestClassifier()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "jdmj0suprtmb"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"params = {'n_estimators':[500, 1000, 2000], 'max_depth': np.arange(1, 8)}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "07xYf1qlrtmh"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"clf_cv = RandomizedSearchCV(clf, param_distributions=params, cv=5)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 461
|
|
},
|
|
"colab_type": "code",
|
|
"id": "IeCFXs-Irtml",
|
|
"outputId": "20609e49-11b3-48f3-f12f-8ae95a7adbfc"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"clf_cv.fit(features, labels.ravel())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 35
|
|
},
|
|
"colab_type": "code",
|
|
"id": "eclW-K0krtmr",
|
|
"outputId": "8aeb220b-c3ae-4055-bca0-ee77e100e7be"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"print(\"Tuned Random Forest 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": "LglPICW4rtmx",
|
|
"outputId": "944898c0-ccac-4997-e9dc-b70978a9d4be"
|
|
},
|
|
"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": 159
|
|
},
|
|
"colab_type": "code",
|
|
"id": "7Vm-gC0Qrtm0",
|
|
"outputId": "f7d7d68b-1773-472a-e9cc-0e22f3398260"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"model = clf_cv.best_estimator_\n",
|
|
"model"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"name": "Exercise7.08.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
|
|
}
|