-
-
Save timelfrink/f8fae851a43813854a693ce9ecaff355 to your computer and use it in GitHub Desktop.
This file contains 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": [ | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"from sklearn import tree\n", | |
"from sklearn.datasets import load_wine\n", | |
"from sklearn.metrics import accuracy_score" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"wine = load_wine()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<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>alcohol</th>\n", | |
" <th>malic_acid</th>\n", | |
" <th>ash</th>\n", | |
" <th>alcalinity_of_ash</th>\n", | |
" <th>magnesium</th>\n", | |
" <th>total_phenols</th>\n", | |
" <th>flavanoids</th>\n", | |
" <th>nonflavanoid_phenols</th>\n", | |
" <th>proanthocyanins</th>\n", | |
" <th>color_intensity</th>\n", | |
" <th>hue</th>\n", | |
" <th>od280/od315_of_diluted_wines</th>\n", | |
" <th>proline</th>\n", | |
" <th>target</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>14.23</td>\n", | |
" <td>1.71</td>\n", | |
" <td>2.43</td>\n", | |
" <td>15.6</td>\n", | |
" <td>127.0</td>\n", | |
" <td>2.80</td>\n", | |
" <td>3.06</td>\n", | |
" <td>0.28</td>\n", | |
" <td>2.29</td>\n", | |
" <td>5.64</td>\n", | |
" <td>1.04</td>\n", | |
" <td>3.92</td>\n", | |
" <td>1065.0</td>\n", | |
" <td>0.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>13.20</td>\n", | |
" <td>1.78</td>\n", | |
" <td>2.14</td>\n", | |
" <td>11.2</td>\n", | |
" <td>100.0</td>\n", | |
" <td>2.65</td>\n", | |
" <td>2.76</td>\n", | |
" <td>0.26</td>\n", | |
" <td>1.28</td>\n", | |
" <td>4.38</td>\n", | |
" <td>1.05</td>\n", | |
" <td>3.40</td>\n", | |
" <td>1050.0</td>\n", | |
" <td>0.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>13.16</td>\n", | |
" <td>2.36</td>\n", | |
" <td>2.67</td>\n", | |
" <td>18.6</td>\n", | |
" <td>101.0</td>\n", | |
" <td>2.80</td>\n", | |
" <td>3.24</td>\n", | |
" <td>0.30</td>\n", | |
" <td>2.81</td>\n", | |
" <td>5.68</td>\n", | |
" <td>1.03</td>\n", | |
" <td>3.17</td>\n", | |
" <td>1185.0</td>\n", | |
" <td>0.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>14.37</td>\n", | |
" <td>1.95</td>\n", | |
" <td>2.50</td>\n", | |
" <td>16.8</td>\n", | |
" <td>113.0</td>\n", | |
" <td>3.85</td>\n", | |
" <td>3.49</td>\n", | |
" <td>0.24</td>\n", | |
" <td>2.18</td>\n", | |
" <td>7.80</td>\n", | |
" <td>0.86</td>\n", | |
" <td>3.45</td>\n", | |
" <td>1480.0</td>\n", | |
" <td>0.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>13.24</td>\n", | |
" <td>2.59</td>\n", | |
" <td>2.87</td>\n", | |
" <td>21.0</td>\n", | |
" <td>118.0</td>\n", | |
" <td>2.80</td>\n", | |
" <td>2.69</td>\n", | |
" <td>0.39</td>\n", | |
" <td>1.82</td>\n", | |
" <td>4.32</td>\n", | |
" <td>1.04</td>\n", | |
" <td>2.93</td>\n", | |
" <td>735.0</td>\n", | |
" <td>0.0</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" alcohol malic_acid ash alcalinity_of_ash magnesium total_phenols \\\n", | |
"0 14.23 1.71 2.43 15.6 127.0 2.80 \n", | |
"1 13.20 1.78 2.14 11.2 100.0 2.65 \n", | |
"2 13.16 2.36 2.67 18.6 101.0 2.80 \n", | |
"3 14.37 1.95 2.50 16.8 113.0 3.85 \n", | |
"4 13.24 2.59 2.87 21.0 118.0 2.80 \n", | |
"\n", | |
" flavanoids nonflavanoid_phenols proanthocyanins color_intensity hue \\\n", | |
"0 3.06 0.28 2.29 5.64 1.04 \n", | |
"1 2.76 0.26 1.28 4.38 1.05 \n", | |
"2 3.24 0.30 2.81 5.68 1.03 \n", | |
"3 3.49 0.24 2.18 7.80 0.86 \n", | |
"4 2.69 0.39 1.82 4.32 1.04 \n", | |
"\n", | |
" od280/od315_of_diluted_wines proline target \n", | |
"0 3.92 1065.0 0.0 \n", | |
"1 3.40 1050.0 0.0 \n", | |
"2 3.17 1185.0 0.0 \n", | |
"3 3.45 1480.0 0.0 \n", | |
"4 2.93 735.0 0.0 " | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data = pd.DataFrame(data= np.c_[wine['data'], wine['target']],\n", | |
" columns= wine['feature_names'] + ['target'])\n", | |
"data.head()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"X_train = data[:-20]\n", | |
"X_test = data[-20:]\n", | |
"\n", | |
"y_train = X_train.target\n", | |
"y_test = X_test.target\n", | |
"\n", | |
"X_train = X_train.drop('target',1)\n", | |
"X_test = X_test.drop('target',1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"clf = tree.DecisionTreeClassifier()\n", | |
"clf = clf.fit(X_train, y_train)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"y_pred = clf.predict(X_test)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"accuracy_score: 0.90\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"accuracy_score: %.2f\"\n", | |
" % accuracy_score(y_test, y_pred))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Wow, 90% accuracy, let's create an API!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pickle\n", | |
"pickle.dump(clf, open('models/final_prediction.pickle', 'wb'))" | |
] | |
} | |
], | |
"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.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment