Created
August 8, 2019 16:03
-
-
Save MarwanDebbiche/045152e63b6e81394dd121be99f6b4fe to your computer and use it in GitHub Desktop.
Iris Classification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from sklearn.datasets import load_iris\n", | |
"from sklearn.linear_model import LogisticRegression\n", | |
"from sklearn.model_selection import train_test_split\n", | |
"from sklearn.metrics import confusion_matrix, accuracy_score\n", | |
"import pandas as pd\n", | |
"import joblib\n", | |
"import numpy as np\n", | |
"\n", | |
"np.random.seed(12)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Load Data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"iris_data = load_iris()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"data = pd.DataFrame(iris_data[\"data\"], columns=iris_data[\"feature_names\"])\n", | |
"target = pd.Series(\n", | |
" [\n", | |
" iris_data[\"target_names\"][cat_idx]\n", | |
" for cat_idx in iris_data[\"target\"]\n", | |
" ],\n", | |
" dtype=\"category\"\n", | |
")\n", | |
"\n", | |
"features = iris_data[\"feature_names\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Training" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n", | |
" intercept_scaling=1, max_iter=1000, multi_class='auto',\n", | |
" n_jobs=None, penalty='l2', random_state=None, solver='lbfgs',\n", | |
" tol=0.0001, verbose=0, warm_start=False)" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"clf = LogisticRegression(solver='lbfgs', multi_class='auto', max_iter=1000)\n", | |
"clf.fit(X_train[features], y_train)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Validation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"y_train_pred = clf.predict(X_train[features])\n", | |
"y_test_pred = clf.predict(X_test[features])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[38, 0, 0],\n", | |
" [ 0, 39, 2],\n", | |
" [ 0, 1, 40]])" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"confusion_matrix(y_train, y_train_pred)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0.975" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"accuracy_score(y_train, y_train_pred)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[12, 0, 0],\n", | |
" [ 0, 8, 1],\n", | |
" [ 0, 0, 9]])" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"confusion_matrix(y_test, y_test_pred)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0.9666666666666667" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"accuracy_score(y_test, y_test_pred)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Save the model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with open(\"models/iris_classifier.joblib\", \"wb\") as f:\n", | |
" joblib.dump(clf, f)\n", | |
"\n", | |
"with open(\"models/iris_classifier_features.joblib\", \"wb\") as f:\n", | |
" joblib.dump(features, f)" | |
] | |
} | |
], | |
"metadata": { | |
"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.7.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment