├── LICENSE
├── README.md
├── Section 1
├── 1.3.ipynb
├── 1.4.ipynb
└── 1.5.ipynb
├── Section 2
├── 2.1.ipynb
├── 2.2.ipynb
├── 2.3.ipynb
└── german_credit.csv.txt
├── Section 3
├── 3.1.ipynb
├── 3.2.ipynb
├── 3.3.ipynb
├── 3.4.ipynb
└── 3.5.ipynb
├── Section 4
├── 4.1.ipynb
├── 4.2.ipynb
├── 4.3.ipynb
└── german_credit.csv
└── Section 5
├── 5.1.ipynb
├── 5.2.ipynb
├── 5.3.ipynb
├── fe_data.csv
├── german_credit.csv
└── y.csv
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Packt
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Hands-On-Feature-Engineering-with-Python
2 | Strengthen your machine learning with advanced feature engineering techniques
3 |
4 | This is the code repository for [Hands-On Feature Engineering with Python [Video]](https://www.packtpub.com/big-data-and-business-intelligence/hands-feature-engineering-python-video), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the video course from start to finish.
5 | ## About the Video Course
6 | Feature engineering is the most important aspect of machine learning. You know that every day you put off learning the process, you are hurting your model’s performance. Studies repeatedly prove that feature engineering can be much more powerful than the choice of algorithms. Yet the field of feature engineering can seem overwhelming and confusing.
7 | This course offers you the single best solution. In this course, all of the recommendations have been extensively tested and proven on real-world problems. You’ll find everything included: the recommendations, the code, the data sources, and the rationale. You’ll get an over-the-shoulder, step-by-step approach for every situation, and each segment can stand alone, allowing you to jump immediately to the topics most important to you.
8 | By the end of the course, you’ll have a clear, concise path to feature engineering and will enable you to get improved results by applying feature engineering techniques on your own datasets
9 |
10 | ## Table of Contents:
11 | Section 1 - Introduction to Feature Engineering
12 | Section 2 - Implementing Feature Extraction
13 | Section 3 - Implementing Feature Transformation
14 | Section 4 - Implementing Feature Selection
15 | Section 5 - Putting All Together - Building the Application
16 |
17 |
18 |
19 |
What You Will Learn
20 |
21 |
22 | - Master the insider tips for world-class feature engineering
23 |
- Eliminate frustration and confusion in handling all aspects of features
24 |
- Dramatically reduce the time required to move to the modeling steps of the process
25 |
- Handle missing values with speed and ease
26 |
- Systematically test for feature interaction terms build new features
27 |
- Leverage advanced “target mean encoding” to maximize performance and understanding
28 |
- Handle outliers automatically with much less effort
29 |
30 |
31 | ## Instructions and Navigation
32 | ### Assumed Knowledge
33 | Anyone who wants to build faster, more accurate machine learning models will benefit. This course assumes that you have basic familiarity with Python as well as machine learning concepts. The content covers material for beginners through to experts.
34 |
35 | ### Technical Requirements
36 | This course has the following requirements:
37 | Operating system: Windows or Linux
38 | Browser: Mozilla or Crome
39 | IDE : Pycharm Community version
40 | Jupyter notebook or jupyter lab
41 |
42 |
43 |
44 | ## Related Products
45 | * [Feature Engineering Made Easy](https://prod.packtpub.com/in/big-data-and-business-intelligence/feature-engineering-made-easy)
46 |
47 | * [Machine Learning Algorithms in 7 Days [Video]](https://prod.packtpub.com/in/big-data-and-business-intelligence/machine-learning-algorithms-7-days-video)
48 |
49 | * [Introduction to ML Classification Models using scikit-learn [Video]](https://prod.packtpub.com/in/application-development/introduction-ml-classification-models-using-scikit-learn-video)
50 |
--------------------------------------------------------------------------------
/Section 1/1.4.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 12,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import pandas as pd # data munging\n",
10 | "import seaborn as sns #visualization\n",
11 | "import matplotlib.pyplot as plt #visualization\n",
12 | "from sklearn.ensemble import RandomForestClassifier #for a classification model\n",
13 | "import numpy as np #scientific computing\n",
14 | "from sklearn.model_selection import train_test_split # split the data into training and testing sets"
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "## set working directory"
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": 2,
27 | "metadata": {},
28 | "outputs": [],
29 | "source": [
30 | "import os\n",
31 | "os.chdir('/home/sahibachopra/packt/')"
32 | ]
33 | },
34 | {
35 | "cell_type": "markdown",
36 | "metadata": {},
37 | "source": [
38 | "## load the data"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": 3,
44 | "metadata": {},
45 | "outputs": [],
46 | "source": [
47 | "data = pd.read_csv('german_credit.csv')"
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": 5,
53 | "metadata": {},
54 | "outputs": [
55 | {
56 | "data": {
57 | "text/html": [
58 | "\n",
59 | "\n",
72 | "
\n",
73 | " \n",
74 | " \n",
75 | " | \n",
76 | " Creditability | \n",
77 | " Account Balance | \n",
78 | " Duration of Credit (month) | \n",
79 | " Payment Status of Previous Credit | \n",
80 | " Purpose | \n",
81 | " Credit Amount | \n",
82 | " Value Savings/Stocks | \n",
83 | " Length of current employment | \n",
84 | " Instalment per cent | \n",
85 | " Sex & Marital Status | \n",
86 | " ... | \n",
87 | " Duration in Current address | \n",
88 | " Most valuable available asset | \n",
89 | " Age (years) | \n",
90 | " Concurrent Credits | \n",
91 | " Type of apartment | \n",
92 | " No of Credits at this Bank | \n",
93 | " Occupation | \n",
94 | " No of dependents | \n",
95 | " Telephone | \n",
96 | " Foreign Worker | \n",
97 | "
\n",
98 | " \n",
99 | " \n",
100 | " \n",
101 | " 0 | \n",
102 | " 1 | \n",
103 | " 1 | \n",
104 | " 18 | \n",
105 | " 4 | \n",
106 | " 2 | \n",
107 | " 1049 | \n",
108 | " 1 | \n",
109 | " 2 | \n",
110 | " 4 | \n",
111 | " 2 | \n",
112 | " ... | \n",
113 | " 4 | \n",
114 | " 2 | \n",
115 | " 21 | \n",
116 | " 3 | \n",
117 | " 1 | \n",
118 | " 1 | \n",
119 | " 3 | \n",
120 | " 1 | \n",
121 | " 1 | \n",
122 | " 1 | \n",
123 | "
\n",
124 | " \n",
125 | " 1 | \n",
126 | " 1 | \n",
127 | " 1 | \n",
128 | " 9 | \n",
129 | " 4 | \n",
130 | " 0 | \n",
131 | " 2799 | \n",
132 | " 1 | \n",
133 | " 3 | \n",
134 | " 2 | \n",
135 | " 3 | \n",
136 | " ... | \n",
137 | " 2 | \n",
138 | " 1 | \n",
139 | " 36 | \n",
140 | " 3 | \n",
141 | " 1 | \n",
142 | " 2 | \n",
143 | " 3 | \n",
144 | " 2 | \n",
145 | " 1 | \n",
146 | " 1 | \n",
147 | "
\n",
148 | " \n",
149 | " 2 | \n",
150 | " 1 | \n",
151 | " 2 | \n",
152 | " 12 | \n",
153 | " 2 | \n",
154 | " 9 | \n",
155 | " 841 | \n",
156 | " 2 | \n",
157 | " 4 | \n",
158 | " 2 | \n",
159 | " 2 | \n",
160 | " ... | \n",
161 | " 4 | \n",
162 | " 1 | \n",
163 | " 23 | \n",
164 | " 3 | \n",
165 | " 1 | \n",
166 | " 1 | \n",
167 | " 2 | \n",
168 | " 1 | \n",
169 | " 1 | \n",
170 | " 1 | \n",
171 | "
\n",
172 | " \n",
173 | " 3 | \n",
174 | " 1 | \n",
175 | " 1 | \n",
176 | " 12 | \n",
177 | " 4 | \n",
178 | " 0 | \n",
179 | " 2122 | \n",
180 | " 1 | \n",
181 | " 3 | \n",
182 | " 3 | \n",
183 | " 3 | \n",
184 | " ... | \n",
185 | " 2 | \n",
186 | " 1 | \n",
187 | " 39 | \n",
188 | " 3 | \n",
189 | " 1 | \n",
190 | " 2 | \n",
191 | " 2 | \n",
192 | " 2 | \n",
193 | " 1 | \n",
194 | " 2 | \n",
195 | "
\n",
196 | " \n",
197 | " 4 | \n",
198 | " 1 | \n",
199 | " 1 | \n",
200 | " 12 | \n",
201 | " 4 | \n",
202 | " 0 | \n",
203 | " 2171 | \n",
204 | " 1 | \n",
205 | " 3 | \n",
206 | " 4 | \n",
207 | " 3 | \n",
208 | " ... | \n",
209 | " 4 | \n",
210 | " 2 | \n",
211 | " 38 | \n",
212 | " 1 | \n",
213 | " 2 | \n",
214 | " 2 | \n",
215 | " 2 | \n",
216 | " 1 | \n",
217 | " 1 | \n",
218 | " 2 | \n",
219 | "
\n",
220 | " \n",
221 | "
\n",
222 | "
5 rows × 21 columns
\n",
223 | "
"
224 | ],
225 | "text/plain": [
226 | " Creditability Account Balance Duration of Credit (month) \\\n",
227 | "0 1 1 18 \n",
228 | "1 1 1 9 \n",
229 | "2 1 2 12 \n",
230 | "3 1 1 12 \n",
231 | "4 1 1 12 \n",
232 | "\n",
233 | " Payment Status of Previous Credit Purpose Credit Amount \\\n",
234 | "0 4 2 1049 \n",
235 | "1 4 0 2799 \n",
236 | "2 2 9 841 \n",
237 | "3 4 0 2122 \n",
238 | "4 4 0 2171 \n",
239 | "\n",
240 | " Value Savings/Stocks Length of current employment Instalment per cent \\\n",
241 | "0 1 2 4 \n",
242 | "1 1 3 2 \n",
243 | "2 2 4 2 \n",
244 | "3 1 3 3 \n",
245 | "4 1 3 4 \n",
246 | "\n",
247 | " Sex & Marital Status ... Duration in Current address \\\n",
248 | "0 2 ... 4 \n",
249 | "1 3 ... 2 \n",
250 | "2 2 ... 4 \n",
251 | "3 3 ... 2 \n",
252 | "4 3 ... 4 \n",
253 | "\n",
254 | " Most valuable available asset Age (years) Concurrent Credits \\\n",
255 | "0 2 21 3 \n",
256 | "1 1 36 3 \n",
257 | "2 1 23 3 \n",
258 | "3 1 39 3 \n",
259 | "4 2 38 1 \n",
260 | "\n",
261 | " Type of apartment No of Credits at this Bank Occupation \\\n",
262 | "0 1 1 3 \n",
263 | "1 1 2 3 \n",
264 | "2 1 1 2 \n",
265 | "3 1 2 2 \n",
266 | "4 2 2 2 \n",
267 | "\n",
268 | " No of dependents Telephone Foreign Worker \n",
269 | "0 1 1 1 \n",
270 | "1 2 1 1 \n",
271 | "2 1 1 1 \n",
272 | "3 2 1 2 \n",
273 | "4 1 1 2 \n",
274 | "\n",
275 | "[5 rows x 21 columns]"
276 | ]
277 | },
278 | "execution_count": 5,
279 | "metadata": {},
280 | "output_type": "execute_result"
281 | }
282 | ],
283 | "source": [
284 | "data.head()"
285 | ]
286 | },
287 | {
288 | "cell_type": "markdown",
289 | "metadata": {},
290 | "source": [
291 | "## split the data into predictor variables xVar and response variables yVar"
292 | ]
293 | },
294 | {
295 | "cell_type": "code",
296 | "execution_count": 7,
297 | "metadata": {},
298 | "outputs": [],
299 | "source": [
300 | "yVar = data['Creditability']\n",
301 | "xVar = data.loc[:, data.columns != 'Creditability']"
302 | ]
303 | },
304 | {
305 | "cell_type": "markdown",
306 | "metadata": {},
307 | "source": [
308 | "## split our data into training and testing sets\n",
309 | "- 20% of the data into the testing set "
310 | ]
311 | },
312 | {
313 | "cell_type": "code",
314 | "execution_count": 8,
315 | "metadata": {},
316 | "outputs": [
317 | {
318 | "name": "stdout",
319 | "output_type": "stream",
320 | "text": [
321 | "(800, 20) (800,)\n",
322 | "(200, 20) (200,)\n"
323 | ]
324 | }
325 | ],
326 | "source": [
327 | "X_train, X_test, y_train, y_test = train_test_split(xVar, yVar, test_size=0.2)\n",
328 | "print (X_train.shape, y_train.shape)\n",
329 | "print (X_test.shape, y_test.shape)"
330 | ]
331 | },
332 | {
333 | "cell_type": "markdown",
334 | "metadata": {},
335 | "source": [
336 | "# Build a random forest classifier"
337 | ]
338 | },
339 | {
340 | "cell_type": "code",
341 | "execution_count": 9,
342 | "metadata": {},
343 | "outputs": [
344 | {
345 | "data": {
346 | "text/plain": [
347 | "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
348 | " max_depth=None, max_features='auto', max_leaf_nodes=None,\n",
349 | " min_impurity_decrease=0.0, min_impurity_split=1e-07,\n",
350 | " min_samples_leaf=1, min_samples_split=2,\n",
351 | " min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=2,\n",
352 | " oob_score=False, random_state=0, verbose=0, warm_start=False)"
353 | ]
354 | },
355 | "execution_count": 9,
356 | "metadata": {},
357 | "output_type": "execute_result"
358 | }
359 | ],
360 | "source": [
361 | "clf = RandomForestClassifier(n_jobs=2, random_state=0)\n",
362 | "\n",
363 | "clf.fit(X_train, y_train)\n",
364 | "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
365 | " max_depth=None, max_features='auto', max_leaf_nodes=None,\n",
366 | " min_impurity_split=1e-07, min_samples_leaf=1,\n",
367 | " min_samples_split=2, min_weight_fraction_leaf=0.0,\n",
368 | " n_estimators=10, n_jobs=2, oob_score=False, random_state=0,\n",
369 | " verbose=0, warm_start=False)"
370 | ]
371 | },
372 | {
373 | "cell_type": "markdown",
374 | "metadata": {},
375 | "source": [
376 | "# Predict "
377 | ]
378 | },
379 | {
380 | "cell_type": "code",
381 | "execution_count": 10,
382 | "metadata": {},
383 | "outputs": [],
384 | "source": [
385 | "preds = clf.predict(X_test)"
386 | ]
387 | },
388 | {
389 | "cell_type": "markdown",
390 | "metadata": {},
391 | "source": [
392 | "# Evaluate results\n",
393 | "- 152 / 200 predicted correctly"
394 | ]
395 | },
396 | {
397 | "cell_type": "code",
398 | "execution_count": 11,
399 | "metadata": {},
400 | "outputs": [
401 | {
402 | "data": {
403 | "text/html": [
404 | "\n",
405 | "\n",
418 | "
\n",
419 | " \n",
420 | " \n",
421 | " Predicted Result | \n",
422 | " 0 | \n",
423 | " 1 | \n",
424 | "
\n",
425 | " \n",
426 | " Actual Result | \n",
427 | " | \n",
428 | " | \n",
429 | "
\n",
430 | " \n",
431 | " \n",
432 | " \n",
433 | " 0 | \n",
434 | " 30 | \n",
435 | " 28 | \n",
436 | "
\n",
437 | " \n",
438 | " 1 | \n",
439 | " 20 | \n",
440 | " 122 | \n",
441 | "
\n",
442 | " \n",
443 | "
\n",
444 | "
"
445 | ],
446 | "text/plain": [
447 | "Predicted Result 0 1\n",
448 | "Actual Result \n",
449 | "0 30 28\n",
450 | "1 20 122"
451 | ]
452 | },
453 | "execution_count": 11,
454 | "metadata": {},
455 | "output_type": "execute_result"
456 | }
457 | ],
458 | "source": [
459 | "pd.crosstab(y_test, preds, rownames=['Actual Result'], colnames=['Predicted Result'])"
460 | ]
461 | }
462 | ],
463 | "metadata": {
464 | "kernelspec": {
465 | "display_name": "Python 3",
466 | "language": "python",
467 | "name": "python3"
468 | },
469 | "language_info": {
470 | "codemirror_mode": {
471 | "name": "ipython",
472 | "version": 3
473 | },
474 | "file_extension": ".py",
475 | "mimetype": "text/x-python",
476 | "name": "python",
477 | "nbconvert_exporter": "python",
478 | "pygments_lexer": "ipython3",
479 | "version": "3.6.0"
480 | }
481 | },
482 | "nbformat": 4,
483 | "nbformat_minor": 2
484 | }
485 |
--------------------------------------------------------------------------------
/Section 2/2.1.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 142,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import pandas as pd\n",
10 | "import seaborn as sns\n",
11 | "import matplotlib.pyplot as plt\n",
12 | "from sklearn.ensemble import RandomForestClassifier\n",
13 | "import numpy as np\n",
14 | "from sklearn.model_selection import train_test_split\n",
15 | "from scipy.io import arff\n",
16 | "from sklearn.metrics import roc_auc_score\n",
17 | "from sklearn.metrics import roc_curve, auc\n",
18 | "import scikitplot as skplt\n",
19 | "from sklearn.decomposition import PCA\n",
20 | "from sklearn.feature_selection import SelectKBest\n",
21 | "from sklearn.pipeline import Pipeline, FeatureUnion, make_union\n",
22 | "from sklearn.linear_model import LogisticRegression\n",
23 | "from sklearn.base import BaseEstimator, TransformerMixin\n",
24 | "from sklearn.preprocessing import scale"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": 3,
30 | "metadata": {},
31 | "outputs": [],
32 | "source": [
33 | "data = pd.read_csv('german_credit.csv')"
34 | ]
35 | },
36 | {
37 | "cell_type": "markdown",
38 | "metadata": {},
39 | "source": [
40 | "## Feature extraction is a process of dimensionality reduction\n",
41 | "- Principal Component Analysis (PCA) helps reduce dimensions by creating a new set of variables that are smaller than the original set without losing any information \n",
42 | "- This efficient reduction of the number of variables is achieved by obtaining orthogonal linear combinations of the original variables – the so-called Principal Components (PCs). \n",
43 | "- PCA is useful for the compression of data and to find patterns in high-dimensional data. "
44 | ]
45 | },
46 | {
47 | "cell_type": "markdown",
48 | "metadata": {},
49 | "source": [
50 | "## PCA doesn't improve the performance of Random Forests much because Random Forests check feature importance and builds a model based on the important features\n",
51 | "- let's run a logistic regression model instead on the data \n",
52 | "- then run another logistic regression model on the data after we have reduced the number of dimensions using PCA"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": 88,
58 | "metadata": {},
59 | "outputs": [],
60 | "source": [
61 | "X, y = data.loc[:, data.columns != 'Creditability'], data['Creditability']"
62 | ]
63 | },
64 | {
65 | "cell_type": "code",
66 | "execution_count": 89,
67 | "metadata": {},
68 | "outputs": [
69 | {
70 | "name": "stdout",
71 | "output_type": "stream",
72 | "text": [
73 | "(800, 20) (800,)\n",
74 | "(200, 20) (200,)\n"
75 | ]
76 | }
77 | ],
78 | "source": [
79 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
80 | "print (X_train.shape, y_train.shape)\n",
81 | "print (X_test.shape, y_test.shape)"
82 | ]
83 | },
84 | {
85 | "cell_type": "markdown",
86 | "metadata": {},
87 | "source": [
88 | "## run a logistic regression model on the raw data"
89 | ]
90 | },
91 | {
92 | "cell_type": "code",
93 | "execution_count": 90,
94 | "metadata": {},
95 | "outputs": [
96 | {
97 | "data": {
98 | "text/plain": [
99 | "LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n",
100 | " intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n",
101 | " penalty='l2', random_state=None, solver='liblinear', tol=0.0001,\n",
102 | " verbose=0, warm_start=False)"
103 | ]
104 | },
105 | "execution_count": 90,
106 | "metadata": {},
107 | "output_type": "execute_result"
108 | }
109 | ],
110 | "source": [
111 | "logreg = LogisticRegression()\n",
112 | "logreg.fit(X_train, y_train)"
113 | ]
114 | },
115 | {
116 | "cell_type": "code",
117 | "execution_count": 91,
118 | "metadata": {},
119 | "outputs": [
120 | {
121 | "name": "stdout",
122 | "output_type": "stream",
123 | "text": [
124 | "Accuracy of logistic regression classifier on test set: 0.75\n"
125 | ]
126 | }
127 | ],
128 | "source": [
129 | "y_pred = logreg.predict(X_test)\n",
130 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))"
131 | ]
132 | },
133 | {
134 | "cell_type": "code",
135 | "execution_count": 92,
136 | "metadata": {},
137 | "outputs": [],
138 | "source": [
139 | "## run PCA on the raw data to reduce dimensions"
140 | ]
141 | },
142 | {
143 | "cell_type": "code",
144 | "execution_count": 143,
145 | "metadata": {},
146 | "outputs": [
147 | {
148 | "name": "stderr",
149 | "output_type": "stream",
150 | "text": [
151 | "/home/sahibachopra/miniconda/envs/ai/lib/python3.6/site-packages/sklearn/utils/validation.py:475: DataConversionWarning: Data with input dtype int64 was converted to float64 by the scale function.\n",
152 | " warnings.warn(msg, DataConversionWarning)\n"
153 | ]
154 | },
155 | {
156 | "data": {
157 | "text/plain": [
158 | "array([[ 0.65465367, -1.25456565, -0.24085723, ..., -0.42828957,\n",
159 | " -0.82331789, -0.19601428],\n",
160 | " [ 0.65465367, -1.25456565, -0.9875727 , ..., 2.33486893,\n",
161 | " -0.82331789, -0.19601428],\n",
162 | " [ 0.65465367, -0.45902624, -0.73866754, ..., -0.42828957,\n",
163 | " -0.82331789, -0.19601428],\n",
164 | " ...,\n",
165 | " [-1.52752523, 1.13205258, 0.00804793, ..., -0.42828957,\n",
166 | " 1.21459768, -0.19601428],\n",
167 | " [-1.52752523, -0.45902624, -0.73866754, ..., -0.42828957,\n",
168 | " 1.21459768, -0.19601428],\n",
169 | " [-1.52752523, -1.25456565, 0.75476341, ..., -0.42828957,\n",
170 | " -0.82331789, -0.19601428]])"
171 | ]
172 | },
173 | "execution_count": 143,
174 | "metadata": {},
175 | "output_type": "execute_result"
176 | }
177 | ],
178 | "source": [
179 | "x = data.values #convert the data into a numpy array\n",
180 | "x = scale(x);x"
181 | ]
182 | },
183 | {
184 | "cell_type": "code",
185 | "execution_count": 144,
186 | "metadata": {},
187 | "outputs": [],
188 | "source": [
189 | "covar_matrix = PCA(n_components = 20) #we have 20 features"
190 | ]
191 | },
192 | {
193 | "cell_type": "code",
194 | "execution_count": 145,
195 | "metadata": {},
196 | "outputs": [
197 | {
198 | "data": {
199 | "text/plain": [
200 | "array([12.2, 22.4, 29.5, 35.9, 41.8, 47.5, 53. , 58.2, 63. , 67.4, 71.6,\n",
201 | " 75.5, 79.2, 82.8, 86.1, 89.1, 91.8, 94.3, 96.6, 98.8])"
202 | ]
203 | },
204 | "execution_count": 145,
205 | "metadata": {},
206 | "output_type": "execute_result"
207 | }
208 | ],
209 | "source": [
210 | "covar_matrix.fit(x)\n",
211 | "variance = covar_matrix.explained_variance_ratio_ #calculate variance ratios\n",
212 | "\n",
213 | "var=np.cumsum(np.round(covar_matrix.explained_variance_ratio_, decimals=3)*100)\n",
214 | "var #cumulative sum of variance explained with [n] features"
215 | ]
216 | },
217 | {
218 | "cell_type": "code",
219 | "execution_count": 149,
220 | "metadata": {},
221 | "outputs": [
222 | {
223 | "data": {
224 | "text/plain": [
225 | "[]"
226 | ]
227 | },
228 | "execution_count": 149,
229 | "metadata": {},
230 | "output_type": "execute_result"
231 | },
232 | {
233 | "data": {
234 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEWCAYAAAB8LwAVAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4xLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvAOZPmwAAIABJREFUeJzt3XecVNX9//HXh97rsggsnaVJFwQVFHvDgp2osSVo1KhfE6NJTDSJMcYUE/0ZE6IiVqwREiVKUEBUuvTe27L0pbPt8/vj3jXrZnYZdpmZ3dn38/GYx9y599w5Hy6z85lz7r3nmLsjIiJSVJVEByAiIuWTEoSIiESkBCEiIhEpQYiISERKECIiEpEShIiIRKQEIVKOmNk6MzunjO+x38w6HK+YpPJSgpAKL/xSPRR+MWaa2Wgzq1do+/lmNtXM9pnZdjObYmaXFnmPoWbmZvajKOtsb2b5ZvaX4/3vKSt3r+fuaxIdh1R8ShCSLC5x93pAP2AA8DCAmV0FvA28DKQBzYGfA5cU2f8mYFf4HI1vA7uB68ysZpmjFymHlCAkqbj7ZmAC0MPMDPgj8Ct3f97ds9w9392nuPt3C/YxszrAVcBdQLqZ9Y+iqm8TJKEciiSbsCVyh5mtNLPdZvZsGAtm1tHMPjGznWa2w8xeM7NGRd/czE4ws4Nm1rTQupPCFlB1M+sUtoSywvd5s0j9ncLli8xsSdh62mxmP4z6YEqlpwQhScXMWgMXAV8BXYDWwDtH2e1KYD9BS+Mjgi//kuoYQtAaGQu8VUz5YQQtmd7ANcD5BbsDvwFaAt3C+B4turO7bwUmh/sWuAEY6+45wK+Aj4HGYSzPFBPuC8Dt7l4f6AF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R32F9uZjcT3BV+d4R9JhPM5XEoXHU2weisTwJXuHuOmf0FmO7uL5tZE3ffFSaMScA97r7AzNaFdewI33e/u9cLl68Chrn7zWGiSAEuc/c8M3scWOLur1owuc9MgpGAnwjrfC0caqKquxfEKHLM1FyVZNWXYNiIrsCSEsqdQjDAGgTDOTwZ5ftf7+5fzwhmZiOAk4BZ4dBPtfnvgHzXhEM7VyNILN0Jhow4Fm+7e164fB5wqZn9MHxdC2hD0Fr6qZmlAe958UNOi0RFCUKSStg99BLBSJ47CPr4zYIpIE+J4hd1aZvUBoxx9x8Xiac98ENggLvvDlsDtaKou2iZA0XqutLdlxcps9TMZgAXAx+Z2Xfc/ZNj/HeIfE3nICSpuPs8d+9DOH0r8AlwfngyOVJy+IJg1E+A64Fppax6EnCVmaXC13NQtwUaEHy5Z5lZc4KhyQvsI5hqtkCmmXUzsyoEI5YW5yPg+4VGqe0bPncA1rj70wQD2vUq5b9FBFCCkCRkZs2A3R7MHdDV3UvqYroHuCU8qX0jcG9p6gzreJhgtrAFBNOFtnD3+cBXBKN3vgh8Xmi3UcCEgpPUBHNV/4sgqZU0ZPqvgOrAAjNbFL4GuBZYFLaWugIvl+bfIlJAJ6lFRCQitSBERCQiJQgREYlICUJERCJSghARkYiUIEREJCIlCBERiUgJQkREIvr/IA9j1t/vDq8AAAAASUVORK5CYII=\n",
235 | "text/plain": [
236 | ""
237 | ]
238 | },
239 | "metadata": {
240 | "needs_background": "light"
241 | },
242 | "output_type": "display_data"
243 | }
244 | ],
245 | "source": [
246 | "plt.ylabel('% Variance Explained')\n",
247 | "plt.xlabel('# of Features')\n",
248 | "plt.title('PCA Analysis')\n",
249 | "plt.ylim(0,110)\n",
250 | "plt.style.context('seaborn-whitegrid')\n",
251 | "\n",
252 | "\n",
253 | "plt.plot(var)"
254 | ]
255 | },
256 | {
257 | "cell_type": "code",
258 | "execution_count": 137,
259 | "metadata": {},
260 | "outputs": [
261 | {
262 | "name": "stdout",
263 | "output_type": "stream",
264 | "text": [
265 | "Combined space has 14 features\n"
266 | ]
267 | }
268 | ],
269 | "source": [
270 | "pca = PCA(n_components=13)\n",
271 | "\n",
272 | "# Maybe some original features where good, too?\n",
273 | "selection = SelectKBest(k=1)\n",
274 | "\n",
275 | "# Build estimator from PCA and Univariate selection:\n",
276 | "\n",
277 | "combined_features = FeatureUnion([(\"pca\", pca), (\"univ_select\", selection)])\n",
278 | "\n",
279 | "\n",
280 | "\n",
281 | "# Use combined features to transform dataset:\n",
282 | "X_features = combined_features.fit(X, y).transform(X)\n",
283 | "print(\"Combined space has\", X_features.shape[1], \"features\")"
284 | ]
285 | },
286 | {
287 | "cell_type": "code",
288 | "execution_count": 138,
289 | "metadata": {},
290 | "outputs": [
291 | {
292 | "name": "stdout",
293 | "output_type": "stream",
294 | "text": [
295 | "(800, 14) (800,)\n",
296 | "(200, 14) (200,)\n"
297 | ]
298 | }
299 | ],
300 | "source": [
301 | "X_train, X_test, y_train, y_test = train_test_split(X_features, y, test_size=0.2)\n",
302 | "print (X_train.shape, y_train.shape)\n",
303 | "print (X_test.shape, y_test.shape)"
304 | ]
305 | },
306 | {
307 | "cell_type": "code",
308 | "execution_count": 139,
309 | "metadata": {},
310 | "outputs": [
311 | {
312 | "data": {
313 | "text/plain": [
314 | "LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n",
315 | " intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n",
316 | " penalty='l2', random_state=None, solver='liblinear', tol=0.0001,\n",
317 | " verbose=0, warm_start=False)"
318 | ]
319 | },
320 | "execution_count": 139,
321 | "metadata": {},
322 | "output_type": "execute_result"
323 | }
324 | ],
325 | "source": [
326 | "logreg = LogisticRegression()\n",
327 | "logreg.fit(X_train, y_train)"
328 | ]
329 | },
330 | {
331 | "cell_type": "code",
332 | "execution_count": 140,
333 | "metadata": {},
334 | "outputs": [
335 | {
336 | "name": "stdout",
337 | "output_type": "stream",
338 | "text": [
339 | "Accuracy of logistic regression classifier on test set: 0.80\n"
340 | ]
341 | }
342 | ],
343 | "source": [
344 | "y_pred = logreg.predict(X_test)\n",
345 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))"
346 | ]
347 | }
348 | ],
349 | "metadata": {
350 | "kernelspec": {
351 | "display_name": "Python 3",
352 | "language": "python",
353 | "name": "python3"
354 | },
355 | "language_info": {
356 | "codemirror_mode": {
357 | "name": "ipython",
358 | "version": 3
359 | },
360 | "file_extension": ".py",
361 | "mimetype": "text/x-python",
362 | "name": "python",
363 | "nbconvert_exporter": "python",
364 | "pygments_lexer": "ipython3",
365 | "version": "3.6.0"
366 | }
367 | },
368 | "nbformat": 4,
369 | "nbformat_minor": 2
370 | }
371 |
--------------------------------------------------------------------------------
/Section 2/2.2.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 10,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import pandas as pd\n",
10 | "import seaborn as sns\n",
11 | "import matplotlib.pyplot as plt\n",
12 | "from sklearn.ensemble import RandomForestClassifier\n",
13 | "import numpy as np\n",
14 | "from sklearn.model_selection import train_test_split\n",
15 | "from scipy.io import arff\n",
16 | "from sklearn.metrics import roc_auc_score\n",
17 | "from sklearn.metrics import roc_curve, auc\n",
18 | "import scikitplot as skplt\n",
19 | "from sklearn.decomposition import PCA\n",
20 | "from sklearn.feature_selection import SelectKBest\n",
21 | "from sklearn.pipeline import Pipeline, FeatureUnion, make_union\n",
22 | "from sklearn.linear_model import LogisticRegression\n",
23 | "from sklearn.base import BaseEstimator, TransformerMixin\n",
24 | "from sklearn.preprocessing import scale\n",
25 | "from sklearn.preprocessing import LabelBinarizer # one hot encoding"
26 | ]
27 | },
28 | {
29 | "cell_type": "markdown",
30 | "metadata": {},
31 | "source": [
32 | "## Dealing with categorical features\n",
33 | " - Label encoding\n",
34 | " - One Hot encoding"
35 | ]
36 | },
37 | {
38 | "cell_type": "code",
39 | "execution_count": 2,
40 | "metadata": {},
41 | "outputs": [],
42 | "source": [
43 | "# load the raw data\n",
44 | "\n",
45 | "df = pd.read_csv('german_credit_raw.csv')"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": 3,
51 | "metadata": {},
52 | "outputs": [
53 | {
54 | "data": {
55 | "text/html": [
56 | "\n",
57 | "\n",
70 | "
\n",
71 | " \n",
72 | " \n",
73 | " | \n",
74 | " default | \n",
75 | " account_check_status | \n",
76 | " duration_in_month | \n",
77 | " credit_history | \n",
78 | " purpose | \n",
79 | " credit_amount | \n",
80 | " savings | \n",
81 | " present_emp_since | \n",
82 | " installment_as_income_perc | \n",
83 | " personal_status_sex | \n",
84 | " ... | \n",
85 | " present_res_since | \n",
86 | " property | \n",
87 | " age | \n",
88 | " other_installment_plans | \n",
89 | " housing | \n",
90 | " credits_this_bank | \n",
91 | " job | \n",
92 | " people_under_maintenance | \n",
93 | " telephone | \n",
94 | " foreign_worker | \n",
95 | "
\n",
96 | " \n",
97 | " \n",
98 | " \n",
99 | " 0 | \n",
100 | " 0 | \n",
101 | " < 0 DM | \n",
102 | " 6 | \n",
103 | " critical account/ other credits existing (not ... | \n",
104 | " domestic appliances | \n",
105 | " 1169 | \n",
106 | " unknown/ no savings account | \n",
107 | " .. >= 7 years | \n",
108 | " 4 | \n",
109 | " male : single | \n",
110 | " ... | \n",
111 | " 4 | \n",
112 | " real estate | \n",
113 | " 67 | \n",
114 | " none | \n",
115 | " own | \n",
116 | " 2 | \n",
117 | " skilled employee / official | \n",
118 | " 1 | \n",
119 | " yes, registered under the customers name | \n",
120 | " yes | \n",
121 | "
\n",
122 | " \n",
123 | " 1 | \n",
124 | " 1 | \n",
125 | " 0 <= ... < 200 DM | \n",
126 | " 48 | \n",
127 | " existing credits paid back duly till now | \n",
128 | " domestic appliances | \n",
129 | " 5951 | \n",
130 | " ... < 100 DM | \n",
131 | " 1 <= ... < 4 years | \n",
132 | " 2 | \n",
133 | " female : divorced/separated/married | \n",
134 | " ... | \n",
135 | " 2 | \n",
136 | " real estate | \n",
137 | " 22 | \n",
138 | " none | \n",
139 | " own | \n",
140 | " 1 | \n",
141 | " skilled employee / official | \n",
142 | " 1 | \n",
143 | " none | \n",
144 | " yes | \n",
145 | "
\n",
146 | " \n",
147 | " 2 | \n",
148 | " 0 | \n",
149 | " no checking account | \n",
150 | " 12 | \n",
151 | " critical account/ other credits existing (not ... | \n",
152 | " (vacation - does not exist?) | \n",
153 | " 2096 | \n",
154 | " ... < 100 DM | \n",
155 | " 4 <= ... < 7 years | \n",
156 | " 2 | \n",
157 | " male : single | \n",
158 | " ... | \n",
159 | " 3 | \n",
160 | " real estate | \n",
161 | " 49 | \n",
162 | " none | \n",
163 | " own | \n",
164 | " 1 | \n",
165 | " unskilled - resident | \n",
166 | " 2 | \n",
167 | " none | \n",
168 | " yes | \n",
169 | "
\n",
170 | " \n",
171 | " 3 | \n",
172 | " 0 | \n",
173 | " < 0 DM | \n",
174 | " 42 | \n",
175 | " existing credits paid back duly till now | \n",
176 | " radio/television | \n",
177 | " 7882 | \n",
178 | " ... < 100 DM | \n",
179 | " 4 <= ... < 7 years | \n",
180 | " 2 | \n",
181 | " male : single | \n",
182 | " ... | \n",
183 | " 4 | \n",
184 | " if not A121 : building society savings agreeme... | \n",
185 | " 45 | \n",
186 | " none | \n",
187 | " for free | \n",
188 | " 1 | \n",
189 | " skilled employee / official | \n",
190 | " 2 | \n",
191 | " none | \n",
192 | " yes | \n",
193 | "
\n",
194 | " \n",
195 | " 4 | \n",
196 | " 1 | \n",
197 | " < 0 DM | \n",
198 | " 24 | \n",
199 | " delay in paying off in the past | \n",
200 | " car (new) | \n",
201 | " 4870 | \n",
202 | " ... < 100 DM | \n",
203 | " 1 <= ... < 4 years | \n",
204 | " 3 | \n",
205 | " male : single | \n",
206 | " ... | \n",
207 | " 4 | \n",
208 | " unknown / no property | \n",
209 | " 53 | \n",
210 | " none | \n",
211 | " for free | \n",
212 | " 2 | \n",
213 | " skilled employee / official | \n",
214 | " 2 | \n",
215 | " none | \n",
216 | " yes | \n",
217 | "
\n",
218 | " \n",
219 | "
\n",
220 | "
5 rows × 21 columns
\n",
221 | "
"
222 | ],
223 | "text/plain": [
224 | " default account_check_status duration_in_month \\\n",
225 | "0 0 < 0 DM 6 \n",
226 | "1 1 0 <= ... < 200 DM 48 \n",
227 | "2 0 no checking account 12 \n",
228 | "3 0 < 0 DM 42 \n",
229 | "4 1 < 0 DM 24 \n",
230 | "\n",
231 | " credit_history \\\n",
232 | "0 critical account/ other credits existing (not ... \n",
233 | "1 existing credits paid back duly till now \n",
234 | "2 critical account/ other credits existing (not ... \n",
235 | "3 existing credits paid back duly till now \n",
236 | "4 delay in paying off in the past \n",
237 | "\n",
238 | " purpose credit_amount savings \\\n",
239 | "0 domestic appliances 1169 unknown/ no savings account \n",
240 | "1 domestic appliances 5951 ... < 100 DM \n",
241 | "2 (vacation - does not exist?) 2096 ... < 100 DM \n",
242 | "3 radio/television 7882 ... < 100 DM \n",
243 | "4 car (new) 4870 ... < 100 DM \n",
244 | "\n",
245 | " present_emp_since installment_as_income_perc \\\n",
246 | "0 .. >= 7 years 4 \n",
247 | "1 1 <= ... < 4 years 2 \n",
248 | "2 4 <= ... < 7 years 2 \n",
249 | "3 4 <= ... < 7 years 2 \n",
250 | "4 1 <= ... < 4 years 3 \n",
251 | "\n",
252 | " personal_status_sex ... present_res_since \\\n",
253 | "0 male : single ... 4 \n",
254 | "1 female : divorced/separated/married ... 2 \n",
255 | "2 male : single ... 3 \n",
256 | "3 male : single ... 4 \n",
257 | "4 male : single ... 4 \n",
258 | "\n",
259 | " property age \\\n",
260 | "0 real estate 67 \n",
261 | "1 real estate 22 \n",
262 | "2 real estate 49 \n",
263 | "3 if not A121 : building society savings agreeme... 45 \n",
264 | "4 unknown / no property 53 \n",
265 | "\n",
266 | " other_installment_plans housing credits_this_bank \\\n",
267 | "0 none own 2 \n",
268 | "1 none own 1 \n",
269 | "2 none own 1 \n",
270 | "3 none for free 1 \n",
271 | "4 none for free 2 \n",
272 | "\n",
273 | " job people_under_maintenance \\\n",
274 | "0 skilled employee / official 1 \n",
275 | "1 skilled employee / official 1 \n",
276 | "2 unskilled - resident 2 \n",
277 | "3 skilled employee / official 2 \n",
278 | "4 skilled employee / official 2 \n",
279 | "\n",
280 | " telephone foreign_worker \n",
281 | "0 yes, registered under the customers name yes \n",
282 | "1 none yes \n",
283 | "2 none yes \n",
284 | "3 none yes \n",
285 | "4 none yes \n",
286 | "\n",
287 | "[5 rows x 21 columns]"
288 | ]
289 | },
290 | "execution_count": 3,
291 | "metadata": {},
292 | "output_type": "execute_result"
293 | }
294 | ],
295 | "source": [
296 | "df.head()"
297 | ]
298 | },
299 | {
300 | "cell_type": "markdown",
301 | "metadata": {},
302 | "source": [
303 | "## let's look at the different types of account status"
304 | ]
305 | },
306 | {
307 | "cell_type": "code",
308 | "execution_count": 4,
309 | "metadata": {},
310 | "outputs": [
311 | {
312 | "data": {
313 | "text/plain": [
314 | "account_check_status\n",
315 | "0 <= ... < 200 DM 269\n",
316 | "< 0 DM 274\n",
317 | ">= 200 DM / salary assignments for at least 1 year 63\n",
318 | "no checking account 394\n",
319 | "dtype: int64"
320 | ]
321 | },
322 | "execution_count": 4,
323 | "metadata": {},
324 | "output_type": "execute_result"
325 | }
326 | ],
327 | "source": [
328 | "df.groupby('account_check_status').size()"
329 | ]
330 | },
331 | {
332 | "cell_type": "code",
333 | "execution_count": 6,
334 | "metadata": {},
335 | "outputs": [],
336 | "source": [
337 | "## convert the data type to category\n",
338 | "df['account_check_status'] = df['account_check_status'].astype('category')"
339 | ]
340 | },
341 | {
342 | "cell_type": "markdown",
343 | "metadata": {},
344 | "source": [
345 | "## Label encoding"
346 | ]
347 | },
348 | {
349 | "cell_type": "code",
350 | "execution_count": 7,
351 | "metadata": {},
352 | "outputs": [],
353 | "source": [
354 | "df['account_status_cat'] = df['account_check_status'].cat.codes"
355 | ]
356 | },
357 | {
358 | "cell_type": "code",
359 | "execution_count": 8,
360 | "metadata": {},
361 | "outputs": [
362 | {
363 | "data": {
364 | "text/plain": [
365 | "account_check_status account_status_cat\n",
366 | "0 <= ... < 200 DM 0 269\n",
367 | "< 0 DM 1 274\n",
368 | ">= 200 DM / salary assignments for at least 1 year 2 63\n",
369 | "no checking account 3 394\n",
370 | "dtype: int64"
371 | ]
372 | },
373 | "execution_count": 8,
374 | "metadata": {},
375 | "output_type": "execute_result"
376 | }
377 | ],
378 | "source": [
379 | "df.groupby(['account_check_status', 'account_status_cat']).size()"
380 | ]
381 | },
382 | {
383 | "cell_type": "markdown",
384 | "metadata": {},
385 | "source": [
386 | "## One hot encoding"
387 | ]
388 | },
389 | {
390 | "cell_type": "code",
391 | "execution_count": 11,
392 | "metadata": {},
393 | "outputs": [
394 | {
395 | "data": {
396 | "text/html": [
397 | "\n",
398 | "\n",
411 | "
\n",
412 | " \n",
413 | " \n",
414 | " | \n",
415 | " 0 <= ... < 200 DM | \n",
416 | " < 0 DM | \n",
417 | " >= 200 DM / salary assignments for at least 1 year | \n",
418 | " no checking account | \n",
419 | "
\n",
420 | " \n",
421 | " \n",
422 | " \n",
423 | " 0 | \n",
424 | " 0 | \n",
425 | " 1 | \n",
426 | " 0 | \n",
427 | " 0 | \n",
428 | "
\n",
429 | " \n",
430 | " 1 | \n",
431 | " 1 | \n",
432 | " 0 | \n",
433 | " 0 | \n",
434 | " 0 | \n",
435 | "
\n",
436 | " \n",
437 | " 2 | \n",
438 | " 0 | \n",
439 | " 0 | \n",
440 | " 0 | \n",
441 | " 1 | \n",
442 | "
\n",
443 | " \n",
444 | " 3 | \n",
445 | " 0 | \n",
446 | " 1 | \n",
447 | " 0 | \n",
448 | " 0 | \n",
449 | "
\n",
450 | " \n",
451 | " 4 | \n",
452 | " 0 | \n",
453 | " 1 | \n",
454 | " 0 | \n",
455 | " 0 | \n",
456 | "
\n",
457 | " \n",
458 | "
\n",
459 | "
"
460 | ],
461 | "text/plain": [
462 | " 0 <= ... < 200 DM < 0 DM \\\n",
463 | "0 0 1 \n",
464 | "1 1 0 \n",
465 | "2 0 0 \n",
466 | "3 0 1 \n",
467 | "4 0 1 \n",
468 | "\n",
469 | " >= 200 DM / salary assignments for at least 1 year no checking account \n",
470 | "0 0 0 \n",
471 | "1 0 0 \n",
472 | "2 0 1 \n",
473 | "3 0 0 \n",
474 | "4 0 0 "
475 | ]
476 | },
477 | "execution_count": 11,
478 | "metadata": {},
479 | "output_type": "execute_result"
480 | }
481 | ],
482 | "source": [
483 | "df_one_hot = df.copy()\n",
484 | "\n",
485 | "lb = LabelBinarizer()\n",
486 | "lb_results = lb.fit_transform(df_one_hot['account_check_status'])\n",
487 | "lb_results_df = pd.DataFrame(lb_results, columns=lb.classes_)\n",
488 | "\n",
489 | "lb_results_df.head()"
490 | ]
491 | },
492 | {
493 | "cell_type": "code",
494 | "execution_count": 12,
495 | "metadata": {},
496 | "outputs": [],
497 | "source": [
498 | "## concatenate this data to our data set\n",
499 | "\n",
500 | "final_df = pd.concat([df_one_hot, lb_results_df], axis=1)"
501 | ]
502 | },
503 | {
504 | "cell_type": "code",
505 | "execution_count": 15,
506 | "metadata": {},
507 | "outputs": [
508 | {
509 | "name": "stdout",
510 | "output_type": "stream",
511 | "text": [
512 | "original df dimensions: (1000, 22)\n",
513 | "one hot encoded df dimensions: (1000, 26)\n"
514 | ]
515 | },
516 | {
517 | "data": {
518 | "text/html": [
519 | "\n",
520 | "\n",
533 | "
\n",
534 | " \n",
535 | " \n",
536 | " | \n",
537 | " default | \n",
538 | " account_check_status | \n",
539 | " duration_in_month | \n",
540 | " credit_history | \n",
541 | " purpose | \n",
542 | " credit_amount | \n",
543 | " savings | \n",
544 | " present_emp_since | \n",
545 | " installment_as_income_perc | \n",
546 | " personal_status_sex | \n",
547 | " ... | \n",
548 | " credits_this_bank | \n",
549 | " job | \n",
550 | " people_under_maintenance | \n",
551 | " telephone | \n",
552 | " foreign_worker | \n",
553 | " account_status_cat | \n",
554 | " 0 <= ... < 200 DM | \n",
555 | " < 0 DM | \n",
556 | " >= 200 DM / salary assignments for at least 1 year | \n",
557 | " no checking account | \n",
558 | "
\n",
559 | " \n",
560 | " \n",
561 | " \n",
562 | " 0 | \n",
563 | " 0 | \n",
564 | " < 0 DM | \n",
565 | " 6 | \n",
566 | " critical account/ other credits existing (not ... | \n",
567 | " domestic appliances | \n",
568 | " 1169 | \n",
569 | " unknown/ no savings account | \n",
570 | " .. >= 7 years | \n",
571 | " 4 | \n",
572 | " male : single | \n",
573 | " ... | \n",
574 | " 2 | \n",
575 | " skilled employee / official | \n",
576 | " 1 | \n",
577 | " yes, registered under the customers name | \n",
578 | " yes | \n",
579 | " 1 | \n",
580 | " 0 | \n",
581 | " 1 | \n",
582 | " 0 | \n",
583 | " 0 | \n",
584 | "
\n",
585 | " \n",
586 | " 1 | \n",
587 | " 1 | \n",
588 | " 0 <= ... < 200 DM | \n",
589 | " 48 | \n",
590 | " existing credits paid back duly till now | \n",
591 | " domestic appliances | \n",
592 | " 5951 | \n",
593 | " ... < 100 DM | \n",
594 | " 1 <= ... < 4 years | \n",
595 | " 2 | \n",
596 | " female : divorced/separated/married | \n",
597 | " ... | \n",
598 | " 1 | \n",
599 | " skilled employee / official | \n",
600 | " 1 | \n",
601 | " none | \n",
602 | " yes | \n",
603 | " 0 | \n",
604 | " 1 | \n",
605 | " 0 | \n",
606 | " 0 | \n",
607 | " 0 | \n",
608 | "
\n",
609 | " \n",
610 | " 2 | \n",
611 | " 0 | \n",
612 | " no checking account | \n",
613 | " 12 | \n",
614 | " critical account/ other credits existing (not ... | \n",
615 | " (vacation - does not exist?) | \n",
616 | " 2096 | \n",
617 | " ... < 100 DM | \n",
618 | " 4 <= ... < 7 years | \n",
619 | " 2 | \n",
620 | " male : single | \n",
621 | " ... | \n",
622 | " 1 | \n",
623 | " unskilled - resident | \n",
624 | " 2 | \n",
625 | " none | \n",
626 | " yes | \n",
627 | " 3 | \n",
628 | " 0 | \n",
629 | " 0 | \n",
630 | " 0 | \n",
631 | " 1 | \n",
632 | "
\n",
633 | " \n",
634 | " 3 | \n",
635 | " 0 | \n",
636 | " < 0 DM | \n",
637 | " 42 | \n",
638 | " existing credits paid back duly till now | \n",
639 | " radio/television | \n",
640 | " 7882 | \n",
641 | " ... < 100 DM | \n",
642 | " 4 <= ... < 7 years | \n",
643 | " 2 | \n",
644 | " male : single | \n",
645 | " ... | \n",
646 | " 1 | \n",
647 | " skilled employee / official | \n",
648 | " 2 | \n",
649 | " none | \n",
650 | " yes | \n",
651 | " 1 | \n",
652 | " 0 | \n",
653 | " 1 | \n",
654 | " 0 | \n",
655 | " 0 | \n",
656 | "
\n",
657 | " \n",
658 | " 4 | \n",
659 | " 1 | \n",
660 | " < 0 DM | \n",
661 | " 24 | \n",
662 | " delay in paying off in the past | \n",
663 | " car (new) | \n",
664 | " 4870 | \n",
665 | " ... < 100 DM | \n",
666 | " 1 <= ... < 4 years | \n",
667 | " 3 | \n",
668 | " male : single | \n",
669 | " ... | \n",
670 | " 2 | \n",
671 | " skilled employee / official | \n",
672 | " 2 | \n",
673 | " none | \n",
674 | " yes | \n",
675 | " 1 | \n",
676 | " 0 | \n",
677 | " 1 | \n",
678 | " 0 | \n",
679 | " 0 | \n",
680 | "
\n",
681 | " \n",
682 | "
\n",
683 | "
5 rows × 26 columns
\n",
684 | "
"
685 | ],
686 | "text/plain": [
687 | " default account_check_status duration_in_month \\\n",
688 | "0 0 < 0 DM 6 \n",
689 | "1 1 0 <= ... < 200 DM 48 \n",
690 | "2 0 no checking account 12 \n",
691 | "3 0 < 0 DM 42 \n",
692 | "4 1 < 0 DM 24 \n",
693 | "\n",
694 | " credit_history \\\n",
695 | "0 critical account/ other credits existing (not ... \n",
696 | "1 existing credits paid back duly till now \n",
697 | "2 critical account/ other credits existing (not ... \n",
698 | "3 existing credits paid back duly till now \n",
699 | "4 delay in paying off in the past \n",
700 | "\n",
701 | " purpose credit_amount savings \\\n",
702 | "0 domestic appliances 1169 unknown/ no savings account \n",
703 | "1 domestic appliances 5951 ... < 100 DM \n",
704 | "2 (vacation - does not exist?) 2096 ... < 100 DM \n",
705 | "3 radio/television 7882 ... < 100 DM \n",
706 | "4 car (new) 4870 ... < 100 DM \n",
707 | "\n",
708 | " present_emp_since installment_as_income_perc \\\n",
709 | "0 .. >= 7 years 4 \n",
710 | "1 1 <= ... < 4 years 2 \n",
711 | "2 4 <= ... < 7 years 2 \n",
712 | "3 4 <= ... < 7 years 2 \n",
713 | "4 1 <= ... < 4 years 3 \n",
714 | "\n",
715 | " personal_status_sex ... credits_this_bank \\\n",
716 | "0 male : single ... 2 \n",
717 | "1 female : divorced/separated/married ... 1 \n",
718 | "2 male : single ... 1 \n",
719 | "3 male : single ... 1 \n",
720 | "4 male : single ... 2 \n",
721 | "\n",
722 | " job people_under_maintenance \\\n",
723 | "0 skilled employee / official 1 \n",
724 | "1 skilled employee / official 1 \n",
725 | "2 unskilled - resident 2 \n",
726 | "3 skilled employee / official 2 \n",
727 | "4 skilled employee / official 2 \n",
728 | "\n",
729 | " telephone foreign_worker \\\n",
730 | "0 yes, registered under the customers name yes \n",
731 | "1 none yes \n",
732 | "2 none yes \n",
733 | "3 none yes \n",
734 | "4 none yes \n",
735 | "\n",
736 | " account_status_cat 0 <= ... < 200 DM < 0 DM \\\n",
737 | "0 1 0 1 \n",
738 | "1 0 1 0 \n",
739 | "2 3 0 0 \n",
740 | "3 1 0 1 \n",
741 | "4 1 0 1 \n",
742 | "\n",
743 | " >= 200 DM / salary assignments for at least 1 year no checking account \n",
744 | "0 0 0 \n",
745 | "1 0 0 \n",
746 | "2 0 1 \n",
747 | "3 0 0 \n",
748 | "4 0 0 \n",
749 | "\n",
750 | "[5 rows x 26 columns]"
751 | ]
752 | },
753 | "execution_count": 15,
754 | "metadata": {},
755 | "output_type": "execute_result"
756 | }
757 | ],
758 | "source": [
759 | "print('original df dimensions:', df.shape)\n",
760 | "print('one hot encoded df dimensions:', final_df.shape)\n",
761 | "final_df.head()"
762 | ]
763 | }
764 | ],
765 | "metadata": {
766 | "kernelspec": {
767 | "display_name": "Python 3",
768 | "language": "python",
769 | "name": "python3"
770 | },
771 | "language_info": {
772 | "codemirror_mode": {
773 | "name": "ipython",
774 | "version": 3
775 | },
776 | "file_extension": ".py",
777 | "mimetype": "text/x-python",
778 | "name": "python",
779 | "nbconvert_exporter": "python",
780 | "pygments_lexer": "ipython3",
781 | "version": "3.6.0"
782 | }
783 | },
784 | "nbformat": 4,
785 | "nbformat_minor": 2
786 | }
787 |
--------------------------------------------------------------------------------
/Section 2/2.3.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 8,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import pandas as pd\n",
10 | "import seaborn as sns\n",
11 | "import matplotlib.pyplot as plt\n",
12 | "from sklearn.ensemble import RandomForestClassifier\n",
13 | "import numpy as np\n",
14 | "from sklearn.model_selection import train_test_split\n",
15 | "from scipy.io import arff\n",
16 | "from sklearn.metrics import roc_auc_score\n",
17 | "from sklearn.metrics import roc_curve, auc\n",
18 | "import scikitplot as skplt\n",
19 | "from sklearn.decomposition import PCA\n",
20 | "from sklearn.feature_selection import SelectKBest\n",
21 | "from sklearn.pipeline import Pipeline, FeatureUnion, make_union\n",
22 | "from sklearn.linear_model import LogisticRegression\n",
23 | "from sklearn.base import BaseEstimator, TransformerMixin\n",
24 | "from sklearn.preprocessing import scale\n",
25 | "from sklearn.preprocessing import LabelBinarizer # one hot encoding\n",
26 | "from sklearn.preprocessing import PolynomialFeatures # add polynomial features"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "metadata": {},
32 | "source": [
33 | "## load our cleaned german credit dataset"
34 | ]
35 | },
36 | {
37 | "cell_type": "code",
38 | "execution_count": 4,
39 | "metadata": {},
40 | "outputs": [],
41 | "source": [
42 | "df = pd.read_csv('german_credit.csv')"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": 5,
48 | "metadata": {
49 | "scrolled": false
50 | },
51 | "outputs": [
52 | {
53 | "data": {
54 | "text/plain": [
55 | "Creditability int64\n",
56 | "Account Balance int64\n",
57 | "Duration of Credit (month) int64\n",
58 | "Payment Status of Previous Credit int64\n",
59 | "Purpose int64\n",
60 | "Credit Amount int64\n",
61 | "Value Savings/Stocks int64\n",
62 | "Length of current employment int64\n",
63 | "Instalment per cent int64\n",
64 | "Sex & Marital Status int64\n",
65 | "Guarantors int64\n",
66 | "Duration in Current address int64\n",
67 | "Most valuable available asset int64\n",
68 | "Age (years) int64\n",
69 | "Concurrent Credits int64\n",
70 | "Type of apartment int64\n",
71 | "No of Credits at this Bank int64\n",
72 | "Occupation int64\n",
73 | "No of dependents int64\n",
74 | "Telephone int64\n",
75 | "Foreign Worker int64\n",
76 | "dtype: object"
77 | ]
78 | },
79 | "execution_count": 5,
80 | "metadata": {},
81 | "output_type": "execute_result"
82 | }
83 | ],
84 | "source": [
85 | "df.dtypes"
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": 7,
91 | "metadata": {},
92 | "outputs": [
93 | {
94 | "data": {
95 | "text/html": [
96 | "\n",
97 | "\n",
110 | "
\n",
111 | " \n",
112 | " \n",
113 | " | \n",
114 | " Age (years) | \n",
115 | " Duration of Credit (month) | \n",
116 | " Credit Amount | \n",
117 | "
\n",
118 | " \n",
119 | " \n",
120 | " \n",
121 | " count | \n",
122 | " 1000.00000 | \n",
123 | " 1000.000000 | \n",
124 | " 1000.00000 | \n",
125 | "
\n",
126 | " \n",
127 | " mean | \n",
128 | " 35.54200 | \n",
129 | " 20.903000 | \n",
130 | " 3271.24800 | \n",
131 | "
\n",
132 | " \n",
133 | " std | \n",
134 | " 11.35267 | \n",
135 | " 12.058814 | \n",
136 | " 2822.75176 | \n",
137 | "
\n",
138 | " \n",
139 | " min | \n",
140 | " 19.00000 | \n",
141 | " 4.000000 | \n",
142 | " 250.00000 | \n",
143 | "
\n",
144 | " \n",
145 | " 25% | \n",
146 | " 27.00000 | \n",
147 | " 12.000000 | \n",
148 | " 1365.50000 | \n",
149 | "
\n",
150 | " \n",
151 | " 50% | \n",
152 | " 33.00000 | \n",
153 | " 18.000000 | \n",
154 | " 2319.50000 | \n",
155 | "
\n",
156 | " \n",
157 | " 75% | \n",
158 | " 42.00000 | \n",
159 | " 24.000000 | \n",
160 | " 3972.25000 | \n",
161 | "
\n",
162 | " \n",
163 | " max | \n",
164 | " 75.00000 | \n",
165 | " 72.000000 | \n",
166 | " 18424.00000 | \n",
167 | "
\n",
168 | " \n",
169 | "
\n",
170 | "
"
171 | ],
172 | "text/plain": [
173 | " Age (years) Duration of Credit (month) Credit Amount\n",
174 | "count 1000.00000 1000.000000 1000.00000\n",
175 | "mean 35.54200 20.903000 3271.24800\n",
176 | "std 11.35267 12.058814 2822.75176\n",
177 | "min 19.00000 4.000000 250.00000\n",
178 | "25% 27.00000 12.000000 1365.50000\n",
179 | "50% 33.00000 18.000000 2319.50000\n",
180 | "75% 42.00000 24.000000 3972.25000\n",
181 | "max 75.00000 72.000000 18424.00000"
182 | ]
183 | },
184 | "execution_count": 7,
185 | "metadata": {},
186 | "output_type": "execute_result"
187 | }
188 | ],
189 | "source": [
190 | "## summary statistics of our continuous data\n",
191 | "\n",
192 | "df[['Age (years)', 'Duration of Credit (month)', 'Credit Amount']].describe()"
193 | ]
194 | },
195 | {
196 | "cell_type": "markdown",
197 | "metadata": {},
198 | "source": [
199 | "## merging and expanding numerical data types\n",
200 | " - creating polynomial features"
201 | ]
202 | },
203 | {
204 | "cell_type": "code",
205 | "execution_count": 13,
206 | "metadata": {},
207 | "outputs": [],
208 | "source": [
209 | "pf = PolynomialFeatures(degree=2, interaction_only=False, \n",
210 | " include_bias=False)\n",
211 | "result = pf.fit_transform(df[['Age (years)', 'Duration of Credit (month)', 'Credit Amount']])"
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": 14,
217 | "metadata": {},
218 | "outputs": [
219 | {
220 | "data": {
221 | "text/html": [
222 | "\n",
223 | "\n",
236 | "
\n",
237 | " \n",
238 | " \n",
239 | " | \n",
240 | " 0 | \n",
241 | " 1 | \n",
242 | " 2 | \n",
243 | " 3 | \n",
244 | " 4 | \n",
245 | " 5 | \n",
246 | " 6 | \n",
247 | " 7 | \n",
248 | " 8 | \n",
249 | "
\n",
250 | " \n",
251 | " \n",
252 | " \n",
253 | " 0 | \n",
254 | " 21.0 | \n",
255 | " 18.0 | \n",
256 | " 1049.0 | \n",
257 | " 441.0 | \n",
258 | " 378.0 | \n",
259 | " 22029.0 | \n",
260 | " 324.0 | \n",
261 | " 18882.0 | \n",
262 | " 1100401.0 | \n",
263 | "
\n",
264 | " \n",
265 | " 1 | \n",
266 | " 36.0 | \n",
267 | " 9.0 | \n",
268 | " 2799.0 | \n",
269 | " 1296.0 | \n",
270 | " 324.0 | \n",
271 | " 100764.0 | \n",
272 | " 81.0 | \n",
273 | " 25191.0 | \n",
274 | " 7834401.0 | \n",
275 | "
\n",
276 | " \n",
277 | " 2 | \n",
278 | " 23.0 | \n",
279 | " 12.0 | \n",
280 | " 841.0 | \n",
281 | " 529.0 | \n",
282 | " 276.0 | \n",
283 | " 19343.0 | \n",
284 | " 144.0 | \n",
285 | " 10092.0 | \n",
286 | " 707281.0 | \n",
287 | "
\n",
288 | " \n",
289 | " 3 | \n",
290 | " 39.0 | \n",
291 | " 12.0 | \n",
292 | " 2122.0 | \n",
293 | " 1521.0 | \n",
294 | " 468.0 | \n",
295 | " 82758.0 | \n",
296 | " 144.0 | \n",
297 | " 25464.0 | \n",
298 | " 4502884.0 | \n",
299 | "
\n",
300 | " \n",
301 | " 4 | \n",
302 | " 38.0 | \n",
303 | " 12.0 | \n",
304 | " 2171.0 | \n",
305 | " 1444.0 | \n",
306 | " 456.0 | \n",
307 | " 82498.0 | \n",
308 | " 144.0 | \n",
309 | " 26052.0 | \n",
310 | " 4713241.0 | \n",
311 | "
\n",
312 | " \n",
313 | "
\n",
314 | "
"
315 | ],
316 | "text/plain": [
317 | " 0 1 2 3 4 5 6 7 8\n",
318 | "0 21.0 18.0 1049.0 441.0 378.0 22029.0 324.0 18882.0 1100401.0\n",
319 | "1 36.0 9.0 2799.0 1296.0 324.0 100764.0 81.0 25191.0 7834401.0\n",
320 | "2 23.0 12.0 841.0 529.0 276.0 19343.0 144.0 10092.0 707281.0\n",
321 | "3 39.0 12.0 2122.0 1521.0 468.0 82758.0 144.0 25464.0 4502884.0\n",
322 | "4 38.0 12.0 2171.0 1444.0 456.0 82498.0 144.0 26052.0 4713241.0"
323 | ]
324 | },
325 | "execution_count": 14,
326 | "metadata": {},
327 | "output_type": "execute_result"
328 | }
329 | ],
330 | "source": [
331 | "pd.DataFrame(result).head()"
332 | ]
333 | },
334 | {
335 | "cell_type": "code",
336 | "execution_count": 16,
337 | "metadata": {},
338 | "outputs": [],
339 | "source": [
340 | "result = pd.DataFrame(result)\n",
341 | "result.columns = ['Age (years)', 'Duration of Credit (month)', 'Credit Amount', 'Age^2', 'AgexCreditDuration', \n",
342 | " 'AgexCreditAmount', 'CreditDuration^2', 'CreditDurationxCreditAmount', 'CreditAmount^2' ]"
343 | ]
344 | },
345 | {
346 | "cell_type": "code",
347 | "execution_count": 34,
348 | "metadata": {},
349 | "outputs": [
350 | {
351 | "name": "stdout",
352 | "output_type": "stream",
353 | "text": [
354 | "Accuracy of logistic regression classifier on test set: 0.74\n"
355 | ]
356 | }
357 | ],
358 | "source": [
359 | "X, y = df.loc[:, df.columns != 'Creditability'], df['Creditability']\n",
360 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
361 | "logreg = LogisticRegression()\n",
362 | "logreg.fit(X_train, y_train)\n",
363 | "y_pred = logreg.predict(X_test)\n",
364 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))"
365 | ]
366 | },
367 | {
368 | "cell_type": "code",
369 | "execution_count": 35,
370 | "metadata": {},
371 | "outputs": [],
372 | "source": [
373 | "new_df = df.drop(['Age (years)', 'Duration of Credit (month)', 'Credit Amount'], axis=1)\n",
374 | "new_df = pd.concat([new_df, result[['Age^2', 'AgexCreditDuration', \n",
375 | " 'AgexCreditAmount', 'CreditDuration^2', 'CreditDurationxCreditAmount', 'CreditAmount^2']]], axis=1)"
376 | ]
377 | },
378 | {
379 | "cell_type": "code",
380 | "execution_count": 42,
381 | "metadata": {},
382 | "outputs": [
383 | {
384 | "name": "stdout",
385 | "output_type": "stream",
386 | "text": [
387 | "Accuracy of logistic regression classifier on test set: 0.76\n"
388 | ]
389 | }
390 | ],
391 | "source": [
392 | "X, y = new_df.loc[:, new_df.columns != 'Creditability'], new_df['Creditability']\n",
393 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
394 | "logreg = LogisticRegression()\n",
395 | "logreg.fit(X_train, y_train)\n",
396 | "y_pred = logreg.predict(X_test)\n",
397 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))"
398 | ]
399 | }
400 | ],
401 | "metadata": {
402 | "kernelspec": {
403 | "display_name": "Python 3",
404 | "language": "python",
405 | "name": "python3"
406 | },
407 | "language_info": {
408 | "codemirror_mode": {
409 | "name": "ipython",
410 | "version": 3
411 | },
412 | "file_extension": ".py",
413 | "mimetype": "text/x-python",
414 | "name": "python",
415 | "nbconvert_exporter": "python",
416 | "pygments_lexer": "ipython3",
417 | "version": "3.6.0"
418 | }
419 | },
420 | "nbformat": 4,
421 | "nbformat_minor": 2
422 | }
423 |
--------------------------------------------------------------------------------
/Section 3/3.1.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 6,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import pandas as pd\n",
10 | "import seaborn as sns\n",
11 | "import matplotlib.pyplot as plt\n",
12 | "from sklearn.ensemble import RandomForestClassifier\n",
13 | "import numpy as np\n",
14 | "from sklearn.model_selection import train_test_split\n",
15 | "from scipy.io import arff\n",
16 | "from sklearn.metrics import roc_auc_score\n",
17 | "from sklearn.metrics import roc_curve, auc\n",
18 | "import scikitplot as skplt\n",
19 | "from sklearn.decomposition import PCA\n",
20 | "from sklearn.feature_selection import SelectKBest\n",
21 | "from sklearn.pipeline import Pipeline, FeatureUnion, make_union\n",
22 | "from sklearn.linear_model import LogisticRegression\n",
23 | "from sklearn.base import BaseEstimator, TransformerMixin\n",
24 | "from sklearn.preprocessing import scale\n",
25 | "from sklearn.preprocessing import LabelBinarizer # one hot encoding\n",
26 | "from sklearn.preprocessing import PolynomialFeatures # add polynomial features\n",
27 | "from sklearn.metrics import classification_report # classification report"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {},
33 | "source": [
34 | "## dealing with target labels \n",
35 | "- In this case, our target labels are imbalanced: we have a lot more creditable people than not-creditable people because that's how lending works (risk averse)\n",
36 | "- Let's try to best deal with an imbalanced dataset"
37 | ]
38 | },
39 | {
40 | "cell_type": "markdown",
41 | "metadata": {},
42 | "source": [
43 | "## Method 1:\n",
44 | "- don't use accuracy as a metric\n",
45 | "- other methods:\n",
46 | " - confusion matrix\n",
47 | " - precision\n",
48 | " - recall"
49 | ]
50 | },
51 | {
52 | "cell_type": "code",
53 | "execution_count": 2,
54 | "metadata": {},
55 | "outputs": [],
56 | "source": [
57 | "df = pd.read_csv('german_credit.csv')"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": 3,
63 | "metadata": {},
64 | "outputs": [
65 | {
66 | "name": "stdout",
67 | "output_type": "stream",
68 | "text": [
69 | "Accuracy of logistic regression classifier on test set: 0.72\n"
70 | ]
71 | },
72 | {
73 | "data": {
74 | "text/html": [
75 | "\n",
76 | "\n",
89 | "
\n",
90 | " \n",
91 | " \n",
92 | " Predicted Result | \n",
93 | " 0 | \n",
94 | " 1 | \n",
95 | "
\n",
96 | " \n",
97 | " Actual Result | \n",
98 | " | \n",
99 | " | \n",
100 | "
\n",
101 | " \n",
102 | " \n",
103 | " \n",
104 | " 0 | \n",
105 | " 24 | \n",
106 | " 38 | \n",
107 | "
\n",
108 | " \n",
109 | " 1 | \n",
110 | " 17 | \n",
111 | " 121 | \n",
112 | "
\n",
113 | " \n",
114 | "
\n",
115 | "
"
116 | ],
117 | "text/plain": [
118 | "Predicted Result 0 1\n",
119 | "Actual Result \n",
120 | "0 24 38\n",
121 | "1 17 121"
122 | ]
123 | },
124 | "execution_count": 3,
125 | "metadata": {},
126 | "output_type": "execute_result"
127 | }
128 | ],
129 | "source": [
130 | "X, y = df.loc[:, df.columns != 'Creditability'], df['Creditability']\n",
131 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
132 | "logreg = LogisticRegression()\n",
133 | "logreg.fit(X_train, y_train)\n",
134 | "y_pred = logreg.predict(X_test)\n",
135 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))\n",
136 | "pd.crosstab(y_test, y_pred, rownames=['Actual Result'], colnames=['Predicted Result'])"
137 | ]
138 | },
139 | {
140 | "cell_type": "markdown",
141 | "metadata": {},
142 | "source": [
143 | "## recall = tp / (tp + fn) : ability to find true positives\n",
144 | "## precision = tp / (tp + fp) : ability to correctly label a sample as positive\n"
145 | ]
146 | },
147 | {
148 | "cell_type": "code",
149 | "execution_count": 7,
150 | "metadata": {},
151 | "outputs": [
152 | {
153 | "name": "stdout",
154 | "output_type": "stream",
155 | "text": [
156 | " precision recall f1-score support\n",
157 | "\n",
158 | " 0 0.59 0.39 0.47 62\n",
159 | " 1 0.76 0.88 0.81 138\n",
160 | "\n",
161 | "avg / total 0.71 0.72 0.71 200\n",
162 | "\n"
163 | ]
164 | }
165 | ],
166 | "source": [
167 | "print(classification_report(y_test, y_pred))"
168 | ]
169 | },
170 | {
171 | "cell_type": "markdown",
172 | "metadata": {},
173 | "source": [
174 | "## Method 2:\n",
175 | "- resample data or use models that resample data like Random Forests\n",
176 | "\n",
177 | "## Method 3:\n",
178 | "- collect more data (if possible)"
179 | ]
180 | }
181 | ],
182 | "metadata": {
183 | "kernelspec": {
184 | "display_name": "Python 3",
185 | "language": "python",
186 | "name": "python3"
187 | },
188 | "language_info": {
189 | "codemirror_mode": {
190 | "name": "ipython",
191 | "version": 3
192 | },
193 | "file_extension": ".py",
194 | "mimetype": "text/x-python",
195 | "name": "python",
196 | "nbconvert_exporter": "python",
197 | "pygments_lexer": "ipython3",
198 | "version": "3.6.0"
199 | }
200 | },
201 | "nbformat": 4,
202 | "nbformat_minor": 2
203 | }
204 |
--------------------------------------------------------------------------------
/Section 3/3.3.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 27,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import pandas as pd\n",
10 | "import numpy as np\n",
11 | "from sklearn.model_selection import train_test_split\n",
12 | "from sklearn.linear_model import LogisticRegression"
13 | ]
14 | },
15 | {
16 | "cell_type": "markdown",
17 | "metadata": {},
18 | "source": [
19 | "## imputing missing values\n",
20 | "- Pima Indians diabetes dataset\n",
21 | " - missing values are marked as 0\n",
22 | "- columns with missing values:\n",
23 | " - Glucose\n",
24 | " - Blood pressure\n",
25 | " - Skin Thickness\n",
26 | " - Insulin\n",
27 | " - BMI"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 2,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "df = pd.read_csv('diabetes.csv')"
37 | ]
38 | },
39 | {
40 | "cell_type": "code",
41 | "execution_count": 9,
42 | "metadata": {},
43 | "outputs": [
44 | {
45 | "data": {
46 | "text/html": [
47 | "\n",
48 | "\n",
61 | "
\n",
62 | " \n",
63 | " \n",
64 | " | \n",
65 | " Pregnancies | \n",
66 | " Glucose | \n",
67 | " BloodPressure | \n",
68 | " SkinThickness | \n",
69 | " Insulin | \n",
70 | " BMI | \n",
71 | " DiabetesPedigreeFunction | \n",
72 | " Age | \n",
73 | " Outcome | \n",
74 | "
\n",
75 | " \n",
76 | " \n",
77 | " \n",
78 | " 0 | \n",
79 | " 6 | \n",
80 | " 148 | \n",
81 | " 72 | \n",
82 | " 35 | \n",
83 | " 0 | \n",
84 | " 33.6 | \n",
85 | " 0.627 | \n",
86 | " 50 | \n",
87 | " 1 | \n",
88 | "
\n",
89 | " \n",
90 | " 1 | \n",
91 | " 1 | \n",
92 | " 85 | \n",
93 | " 66 | \n",
94 | " 29 | \n",
95 | " 0 | \n",
96 | " 26.6 | \n",
97 | " 0.351 | \n",
98 | " 31 | \n",
99 | " 0 | \n",
100 | "
\n",
101 | " \n",
102 | " 2 | \n",
103 | " 8 | \n",
104 | " 183 | \n",
105 | " 64 | \n",
106 | " 0 | \n",
107 | " 0 | \n",
108 | " 23.3 | \n",
109 | " 0.672 | \n",
110 | " 32 | \n",
111 | " 1 | \n",
112 | "
\n",
113 | " \n",
114 | " 3 | \n",
115 | " 1 | \n",
116 | " 89 | \n",
117 | " 66 | \n",
118 | " 23 | \n",
119 | " 94 | \n",
120 | " 28.1 | \n",
121 | " 0.167 | \n",
122 | " 21 | \n",
123 | " 0 | \n",
124 | "
\n",
125 | " \n",
126 | " 4 | \n",
127 | " 0 | \n",
128 | " 137 | \n",
129 | " 40 | \n",
130 | " 35 | \n",
131 | " 168 | \n",
132 | " 43.1 | \n",
133 | " 2.288 | \n",
134 | " 33 | \n",
135 | " 1 | \n",
136 | "
\n",
137 | " \n",
138 | "
\n",
139 | "
"
140 | ],
141 | "text/plain": [
142 | " Pregnancies Glucose BloodPressure SkinThickness Insulin BMI \\\n",
143 | "0 6 148 72 35 0 33.6 \n",
144 | "1 1 85 66 29 0 26.6 \n",
145 | "2 8 183 64 0 0 23.3 \n",
146 | "3 1 89 66 23 94 28.1 \n",
147 | "4 0 137 40 35 168 43.1 \n",
148 | "\n",
149 | " DiabetesPedigreeFunction Age Outcome \n",
150 | "0 0.627 50 1 \n",
151 | "1 0.351 31 0 \n",
152 | "2 0.672 32 1 \n",
153 | "3 0.167 21 0 \n",
154 | "4 2.288 33 1 "
155 | ]
156 | },
157 | "execution_count": 9,
158 | "metadata": {},
159 | "output_type": "execute_result"
160 | }
161 | ],
162 | "source": [
163 | "df.head()"
164 | ]
165 | },
166 | {
167 | "cell_type": "code",
168 | "execution_count": 10,
169 | "metadata": {},
170 | "outputs": [
171 | {
172 | "data": {
173 | "text/html": [
174 | "\n",
175 | "\n",
188 | "
\n",
189 | " \n",
190 | " \n",
191 | " | \n",
192 | " Pregnancies | \n",
193 | " Glucose | \n",
194 | " BloodPressure | \n",
195 | " SkinThickness | \n",
196 | " Insulin | \n",
197 | " BMI | \n",
198 | " DiabetesPedigreeFunction | \n",
199 | " Age | \n",
200 | " Outcome | \n",
201 | "
\n",
202 | " \n",
203 | " \n",
204 | " \n",
205 | " count | \n",
206 | " 768.000000 | \n",
207 | " 768.000000 | \n",
208 | " 768.000000 | \n",
209 | " 768.000000 | \n",
210 | " 768.000000 | \n",
211 | " 768.000000 | \n",
212 | " 768.000000 | \n",
213 | " 768.000000 | \n",
214 | " 768.000000 | \n",
215 | "
\n",
216 | " \n",
217 | " mean | \n",
218 | " 3.845052 | \n",
219 | " 120.894531 | \n",
220 | " 69.105469 | \n",
221 | " 20.536458 | \n",
222 | " 79.799479 | \n",
223 | " 31.992578 | \n",
224 | " 0.471876 | \n",
225 | " 33.240885 | \n",
226 | " 0.348958 | \n",
227 | "
\n",
228 | " \n",
229 | " std | \n",
230 | " 3.369578 | \n",
231 | " 31.972618 | \n",
232 | " 19.355807 | \n",
233 | " 15.952218 | \n",
234 | " 115.244002 | \n",
235 | " 7.884160 | \n",
236 | " 0.331329 | \n",
237 | " 11.760232 | \n",
238 | " 0.476951 | \n",
239 | "
\n",
240 | " \n",
241 | " min | \n",
242 | " 0.000000 | \n",
243 | " 0.000000 | \n",
244 | " 0.000000 | \n",
245 | " 0.000000 | \n",
246 | " 0.000000 | \n",
247 | " 0.000000 | \n",
248 | " 0.078000 | \n",
249 | " 21.000000 | \n",
250 | " 0.000000 | \n",
251 | "
\n",
252 | " \n",
253 | " 25% | \n",
254 | " 1.000000 | \n",
255 | " 99.000000 | \n",
256 | " 62.000000 | \n",
257 | " 0.000000 | \n",
258 | " 0.000000 | \n",
259 | " 27.300000 | \n",
260 | " 0.243750 | \n",
261 | " 24.000000 | \n",
262 | " 0.000000 | \n",
263 | "
\n",
264 | " \n",
265 | " 50% | \n",
266 | " 3.000000 | \n",
267 | " 117.000000 | \n",
268 | " 72.000000 | \n",
269 | " 23.000000 | \n",
270 | " 30.500000 | \n",
271 | " 32.000000 | \n",
272 | " 0.372500 | \n",
273 | " 29.000000 | \n",
274 | " 0.000000 | \n",
275 | "
\n",
276 | " \n",
277 | " 75% | \n",
278 | " 6.000000 | \n",
279 | " 140.250000 | \n",
280 | " 80.000000 | \n",
281 | " 32.000000 | \n",
282 | " 127.250000 | \n",
283 | " 36.600000 | \n",
284 | " 0.626250 | \n",
285 | " 41.000000 | \n",
286 | " 1.000000 | \n",
287 | "
\n",
288 | " \n",
289 | " max | \n",
290 | " 17.000000 | \n",
291 | " 199.000000 | \n",
292 | " 122.000000 | \n",
293 | " 99.000000 | \n",
294 | " 846.000000 | \n",
295 | " 67.100000 | \n",
296 | " 2.420000 | \n",
297 | " 81.000000 | \n",
298 | " 1.000000 | \n",
299 | "
\n",
300 | " \n",
301 | "
\n",
302 | "
"
303 | ],
304 | "text/plain": [
305 | " Pregnancies Glucose BloodPressure SkinThickness Insulin \\\n",
306 | "count 768.000000 768.000000 768.000000 768.000000 768.000000 \n",
307 | "mean 3.845052 120.894531 69.105469 20.536458 79.799479 \n",
308 | "std 3.369578 31.972618 19.355807 15.952218 115.244002 \n",
309 | "min 0.000000 0.000000 0.000000 0.000000 0.000000 \n",
310 | "25% 1.000000 99.000000 62.000000 0.000000 0.000000 \n",
311 | "50% 3.000000 117.000000 72.000000 23.000000 30.500000 \n",
312 | "75% 6.000000 140.250000 80.000000 32.000000 127.250000 \n",
313 | "max 17.000000 199.000000 122.000000 99.000000 846.000000 \n",
314 | "\n",
315 | " BMI DiabetesPedigreeFunction Age Outcome \n",
316 | "count 768.000000 768.000000 768.000000 768.000000 \n",
317 | "mean 31.992578 0.471876 33.240885 0.348958 \n",
318 | "std 7.884160 0.331329 11.760232 0.476951 \n",
319 | "min 0.000000 0.078000 21.000000 0.000000 \n",
320 | "25% 27.300000 0.243750 24.000000 0.000000 \n",
321 | "50% 32.000000 0.372500 29.000000 0.000000 \n",
322 | "75% 36.600000 0.626250 41.000000 1.000000 \n",
323 | "max 67.100000 2.420000 81.000000 1.000000 "
324 | ]
325 | },
326 | "execution_count": 10,
327 | "metadata": {},
328 | "output_type": "execute_result"
329 | }
330 | ],
331 | "source": [
332 | "df.describe()"
333 | ]
334 | },
335 | {
336 | "cell_type": "markdown",
337 | "metadata": {},
338 | "source": [
339 | "## baseline ML model"
340 | ]
341 | },
342 | {
343 | "cell_type": "code",
344 | "execution_count": 40,
345 | "metadata": {},
346 | "outputs": [
347 | {
348 | "name": "stdout",
349 | "output_type": "stream",
350 | "text": [
351 | "Accuracy of logistic regression classifier on test set: 0.73\n"
352 | ]
353 | },
354 | {
355 | "data": {
356 | "text/html": [
357 | "\n",
358 | "\n",
371 | "
\n",
372 | " \n",
373 | " \n",
374 | " Predicted Result | \n",
375 | " 0 | \n",
376 | " 1 | \n",
377 | "
\n",
378 | " \n",
379 | " Actual Result | \n",
380 | " | \n",
381 | " | \n",
382 | "
\n",
383 | " \n",
384 | " \n",
385 | " \n",
386 | " 0 | \n",
387 | " 85 | \n",
388 | " 10 | \n",
389 | "
\n",
390 | " \n",
391 | " 1 | \n",
392 | " 32 | \n",
393 | " 27 | \n",
394 | "
\n",
395 | " \n",
396 | "
\n",
397 | "
"
398 | ],
399 | "text/plain": [
400 | "Predicted Result 0 1\n",
401 | "Actual Result \n",
402 | "0 85 10\n",
403 | "1 32 27"
404 | ]
405 | },
406 | "execution_count": 40,
407 | "metadata": {},
408 | "output_type": "execute_result"
409 | }
410 | ],
411 | "source": [
412 | "X, y = df.loc[:, df.columns != 'Outcome'], df['Outcome']\n",
413 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
414 | "logreg = LogisticRegression()\n",
415 | "logreg.fit(X_train, y_train)\n",
416 | "y_pred = logreg.predict(X_test)\n",
417 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))\n",
418 | "pd.crosstab(y_test, y_pred, rownames=['Actual Result'], colnames=['Predicted Result'])"
419 | ]
420 | },
421 | {
422 | "cell_type": "markdown",
423 | "metadata": {},
424 | "source": [
425 | "## impute missing values using the mean of the column"
426 | ]
427 | },
428 | {
429 | "cell_type": "code",
430 | "execution_count": 20,
431 | "metadata": {},
432 | "outputs": [],
433 | "source": [
434 | "missing_columns = df[['Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI']]\n",
435 | "missing_columns = missing_columns.replace(0, np.nan)"
436 | ]
437 | },
438 | {
439 | "cell_type": "code",
440 | "execution_count": 19,
441 | "metadata": {},
442 | "outputs": [
443 | {
444 | "data": {
445 | "text/html": [
446 | "\n",
447 | "\n",
460 | "
\n",
461 | " \n",
462 | " \n",
463 | " | \n",
464 | " Glucose | \n",
465 | " BloodPressure | \n",
466 | " SkinThickness | \n",
467 | " Insulin | \n",
468 | " BMI | \n",
469 | "
\n",
470 | " \n",
471 | " \n",
472 | " \n",
473 | " 0 | \n",
474 | " 148.0 | \n",
475 | " 72.0 | \n",
476 | " 35.0 | \n",
477 | " NaN | \n",
478 | " 33.6 | \n",
479 | "
\n",
480 | " \n",
481 | " 1 | \n",
482 | " 85.0 | \n",
483 | " 66.0 | \n",
484 | " 29.0 | \n",
485 | " NaN | \n",
486 | " 26.6 | \n",
487 | "
\n",
488 | " \n",
489 | " 2 | \n",
490 | " 183.0 | \n",
491 | " 64.0 | \n",
492 | " NaN | \n",
493 | " NaN | \n",
494 | " 23.3 | \n",
495 | "
\n",
496 | " \n",
497 | " 3 | \n",
498 | " 89.0 | \n",
499 | " 66.0 | \n",
500 | " 23.0 | \n",
501 | " 94.0 | \n",
502 | " 28.1 | \n",
503 | "
\n",
504 | " \n",
505 | " 4 | \n",
506 | " 137.0 | \n",
507 | " 40.0 | \n",
508 | " 35.0 | \n",
509 | " 168.0 | \n",
510 | " 43.1 | \n",
511 | "
\n",
512 | " \n",
513 | "
\n",
514 | "
"
515 | ],
516 | "text/plain": [
517 | " Glucose BloodPressure SkinThickness Insulin BMI\n",
518 | "0 148.0 72.0 35.0 NaN 33.6\n",
519 | "1 85.0 66.0 29.0 NaN 26.6\n",
520 | "2 183.0 64.0 NaN NaN 23.3\n",
521 | "3 89.0 66.0 23.0 94.0 28.1\n",
522 | "4 137.0 40.0 35.0 168.0 43.1"
523 | ]
524 | },
525 | "execution_count": 19,
526 | "metadata": {},
527 | "output_type": "execute_result"
528 | }
529 | ],
530 | "source": [
531 | "missing_columns.head()"
532 | ]
533 | },
534 | {
535 | "cell_type": "code",
536 | "execution_count": 21,
537 | "metadata": {},
538 | "outputs": [],
539 | "source": [
540 | "means = df[['Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI']].mean()\n",
541 | "missing_columns = missing_columns.fillna(means)"
542 | ]
543 | },
544 | {
545 | "cell_type": "code",
546 | "execution_count": 23,
547 | "metadata": {},
548 | "outputs": [
549 | {
550 | "data": {
551 | "text/plain": [
552 | "Glucose 120.894531\n",
553 | "BloodPressure 69.105469\n",
554 | "SkinThickness 20.536458\n",
555 | "Insulin 79.799479\n",
556 | "BMI 31.992578\n",
557 | "dtype: float64"
558 | ]
559 | },
560 | "execution_count": 23,
561 | "metadata": {},
562 | "output_type": "execute_result"
563 | }
564 | ],
565 | "source": [
566 | "means"
567 | ]
568 | },
569 | {
570 | "cell_type": "code",
571 | "execution_count": 22,
572 | "metadata": {},
573 | "outputs": [
574 | {
575 | "data": {
576 | "text/html": [
577 | "\n",
578 | "\n",
591 | "
\n",
592 | " \n",
593 | " \n",
594 | " | \n",
595 | " Glucose | \n",
596 | " BloodPressure | \n",
597 | " SkinThickness | \n",
598 | " Insulin | \n",
599 | " BMI | \n",
600 | "
\n",
601 | " \n",
602 | " \n",
603 | " \n",
604 | " 0 | \n",
605 | " 148.0 | \n",
606 | " 72.0 | \n",
607 | " 35.000000 | \n",
608 | " 79.799479 | \n",
609 | " 33.6 | \n",
610 | "
\n",
611 | " \n",
612 | " 1 | \n",
613 | " 85.0 | \n",
614 | " 66.0 | \n",
615 | " 29.000000 | \n",
616 | " 79.799479 | \n",
617 | " 26.6 | \n",
618 | "
\n",
619 | " \n",
620 | " 2 | \n",
621 | " 183.0 | \n",
622 | " 64.0 | \n",
623 | " 20.536458 | \n",
624 | " 79.799479 | \n",
625 | " 23.3 | \n",
626 | "
\n",
627 | " \n",
628 | " 3 | \n",
629 | " 89.0 | \n",
630 | " 66.0 | \n",
631 | " 23.000000 | \n",
632 | " 94.000000 | \n",
633 | " 28.1 | \n",
634 | "
\n",
635 | " \n",
636 | " 4 | \n",
637 | " 137.0 | \n",
638 | " 40.0 | \n",
639 | " 35.000000 | \n",
640 | " 168.000000 | \n",
641 | " 43.1 | \n",
642 | "
\n",
643 | " \n",
644 | "
\n",
645 | "
"
646 | ],
647 | "text/plain": [
648 | " Glucose BloodPressure SkinThickness Insulin BMI\n",
649 | "0 148.0 72.0 35.000000 79.799479 33.6\n",
650 | "1 85.0 66.0 29.000000 79.799479 26.6\n",
651 | "2 183.0 64.0 20.536458 79.799479 23.3\n",
652 | "3 89.0 66.0 23.000000 94.000000 28.1\n",
653 | "4 137.0 40.0 35.000000 168.000000 43.1"
654 | ]
655 | },
656 | "execution_count": 22,
657 | "metadata": {},
658 | "output_type": "execute_result"
659 | }
660 | ],
661 | "source": [
662 | "missing_columns.head()"
663 | ]
664 | },
665 | {
666 | "cell_type": "code",
667 | "execution_count": 29,
668 | "metadata": {},
669 | "outputs": [],
670 | "source": [
671 | "df = df.drop(['Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI'], axis = 1)\n",
672 | "df = pd.concat([df, missing_columns], axis =1)"
673 | ]
674 | },
675 | {
676 | "cell_type": "code",
677 | "execution_count": 35,
678 | "metadata": {},
679 | "outputs": [
680 | {
681 | "name": "stdout",
682 | "output_type": "stream",
683 | "text": [
684 | "Accuracy of logistic regression classifier on test set: 0.77\n"
685 | ]
686 | },
687 | {
688 | "data": {
689 | "text/html": [
690 | "\n",
691 | "\n",
704 | "
\n",
705 | " \n",
706 | " \n",
707 | " Predicted Result | \n",
708 | " 0 | \n",
709 | " 1 | \n",
710 | "
\n",
711 | " \n",
712 | " Actual Result | \n",
713 | " | \n",
714 | " | \n",
715 | "
\n",
716 | " \n",
717 | " \n",
718 | " \n",
719 | " 0 | \n",
720 | " 92 | \n",
721 | " 12 | \n",
722 | "
\n",
723 | " \n",
724 | " 1 | \n",
725 | " 23 | \n",
726 | " 27 | \n",
727 | "
\n",
728 | " \n",
729 | "
\n",
730 | "
"
731 | ],
732 | "text/plain": [
733 | "Predicted Result 0 1\n",
734 | "Actual Result \n",
735 | "0 92 12\n",
736 | "1 23 27"
737 | ]
738 | },
739 | "execution_count": 35,
740 | "metadata": {},
741 | "output_type": "execute_result"
742 | }
743 | ],
744 | "source": [
745 | "X, y = df.loc[:, df.columns != 'Outcome'], df['Outcome']\n",
746 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
747 | "logreg = LogisticRegression()\n",
748 | "logreg.fit(X_train, y_train)\n",
749 | "y_pred = logreg.predict(X_test)\n",
750 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))\n",
751 | "pd.crosstab(y_test, y_pred, rownames=['Actual Result'], colnames=['Predicted Result'])"
752 | ]
753 | },
754 | {
755 | "cell_type": "markdown",
756 | "metadata": {},
757 | "source": [
758 | "## impute missing values using the mode of the column"
759 | ]
760 | },
761 | {
762 | "cell_type": "code",
763 | "execution_count": 41,
764 | "metadata": {},
765 | "outputs": [],
766 | "source": [
767 | "missing_columns = df[['Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI']]\n",
768 | "missing_columns = missing_columns.replace(0, np.nan)\n",
769 | "\n",
770 | "modes = df[['Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI']].mode()\n",
771 | "missing_columns = missing_columns.fillna(modes)"
772 | ]
773 | },
774 | {
775 | "cell_type": "code",
776 | "execution_count": 42,
777 | "metadata": {},
778 | "outputs": [],
779 | "source": [
780 | "df = df.drop(['Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI'], axis = 1)\n",
781 | "df = pd.concat([df, missing_columns], axis =1)"
782 | ]
783 | },
784 | {
785 | "cell_type": "code",
786 | "execution_count": 43,
787 | "metadata": {},
788 | "outputs": [
789 | {
790 | "name": "stdout",
791 | "output_type": "stream",
792 | "text": [
793 | "Accuracy of logistic regression classifier on test set: 0.76\n"
794 | ]
795 | },
796 | {
797 | "data": {
798 | "text/html": [
799 | "\n",
800 | "\n",
813 | "
\n",
814 | " \n",
815 | " \n",
816 | " Predicted Result | \n",
817 | " 0 | \n",
818 | " 1 | \n",
819 | "
\n",
820 | " \n",
821 | " Actual Result | \n",
822 | " | \n",
823 | " | \n",
824 | "
\n",
825 | " \n",
826 | " \n",
827 | " \n",
828 | " 0 | \n",
829 | " 89 | \n",
830 | " 8 | \n",
831 | "
\n",
832 | " \n",
833 | " 1 | \n",
834 | " 29 | \n",
835 | " 28 | \n",
836 | "
\n",
837 | " \n",
838 | "
\n",
839 | "
"
840 | ],
841 | "text/plain": [
842 | "Predicted Result 0 1\n",
843 | "Actual Result \n",
844 | "0 89 8\n",
845 | "1 29 28"
846 | ]
847 | },
848 | "execution_count": 43,
849 | "metadata": {},
850 | "output_type": "execute_result"
851 | }
852 | ],
853 | "source": [
854 | "X, y = df.loc[:, df.columns != 'Outcome'], df['Outcome']\n",
855 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
856 | "logreg = LogisticRegression()\n",
857 | "logreg.fit(X_train, y_train)\n",
858 | "y_pred = logreg.predict(X_test)\n",
859 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))\n",
860 | "pd.crosstab(y_test, y_pred, rownames=['Actual Result'], colnames=['Predicted Result'])"
861 | ]
862 | },
863 | {
864 | "cell_type": "markdown",
865 | "metadata": {},
866 | "source": [
867 | "## impute missing values using the median"
868 | ]
869 | },
870 | {
871 | "cell_type": "code",
872 | "execution_count": 44,
873 | "metadata": {},
874 | "outputs": [],
875 | "source": [
876 | "missing_columns = df[['Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI']]\n",
877 | "missing_columns = missing_columns.replace(0, np.nan)\n",
878 | "\n",
879 | "medians = df[['Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI']].median()\n",
880 | "missing_columns = missing_columns.fillna(medians)"
881 | ]
882 | },
883 | {
884 | "cell_type": "code",
885 | "execution_count": 45,
886 | "metadata": {},
887 | "outputs": [],
888 | "source": [
889 | "df = df.drop(['Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI'], axis = 1)\n",
890 | "df = pd.concat([df, missing_columns], axis =1)"
891 | ]
892 | },
893 | {
894 | "cell_type": "code",
895 | "execution_count": 48,
896 | "metadata": {},
897 | "outputs": [
898 | {
899 | "name": "stdout",
900 | "output_type": "stream",
901 | "text": [
902 | "Accuracy of logistic regression classifier on test set: 0.78\n"
903 | ]
904 | },
905 | {
906 | "data": {
907 | "text/html": [
908 | "\n",
909 | "\n",
922 | "
\n",
923 | " \n",
924 | " \n",
925 | " Predicted Result | \n",
926 | " 0 | \n",
927 | " 1 | \n",
928 | "
\n",
929 | " \n",
930 | " Actual Result | \n",
931 | " | \n",
932 | " | \n",
933 | "
\n",
934 | " \n",
935 | " \n",
936 | " \n",
937 | " 0 | \n",
938 | " 89 | \n",
939 | " 13 | \n",
940 | "
\n",
941 | " \n",
942 | " 1 | \n",
943 | " 21 | \n",
944 | " 31 | \n",
945 | "
\n",
946 | " \n",
947 | "
\n",
948 | "
"
949 | ],
950 | "text/plain": [
951 | "Predicted Result 0 1\n",
952 | "Actual Result \n",
953 | "0 89 13\n",
954 | "1 21 31"
955 | ]
956 | },
957 | "execution_count": 48,
958 | "metadata": {},
959 | "output_type": "execute_result"
960 | }
961 | ],
962 | "source": [
963 | "X, y = df.loc[:, df.columns != 'Outcome'], df['Outcome']\n",
964 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
965 | "logreg = LogisticRegression()\n",
966 | "logreg.fit(X_train, y_train)\n",
967 | "y_pred = logreg.predict(X_test)\n",
968 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))\n",
969 | "pd.crosstab(y_test, y_pred, rownames=['Actual Result'], colnames=['Predicted Result'])"
970 | ]
971 | }
972 | ],
973 | "metadata": {
974 | "kernelspec": {
975 | "display_name": "Python 3",
976 | "language": "python",
977 | "name": "python3"
978 | },
979 | "language_info": {
980 | "codemirror_mode": {
981 | "name": "ipython",
982 | "version": 3
983 | },
984 | "file_extension": ".py",
985 | "mimetype": "text/x-python",
986 | "name": "python",
987 | "nbconvert_exporter": "python",
988 | "pygments_lexer": "ipython3",
989 | "version": "3.6.0"
990 | }
991 | },
992 | "nbformat": 4,
993 | "nbformat_minor": 2
994 | }
995 |
--------------------------------------------------------------------------------
/Section 3/3.4.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [
8 | {
9 | "name": "stderr",
10 | "output_type": "stream",
11 | "text": [
12 | "/home/sahibachopra/miniconda/envs/ai/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88\n",
13 | " return f(*args, **kwds)\n"
14 | ]
15 | }
16 | ],
17 | "source": [
18 | "import pandas as pd\n",
19 | "import numpy as np\n",
20 | "from sklearn.model_selection import train_test_split\n",
21 | "from sklearn.linear_model import LogisticRegression"
22 | ]
23 | },
24 | {
25 | "cell_type": "markdown",
26 | "metadata": {},
27 | "source": [
28 | "## remove outliers using the inter-quartile range"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 2,
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "df = pd.read_csv('diabetes.csv')"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": 3,
43 | "metadata": {},
44 | "outputs": [
45 | {
46 | "data": {
47 | "text/html": [
48 | "\n",
49 | "\n",
62 | "
\n",
63 | " \n",
64 | " \n",
65 | " | \n",
66 | " Pregnancies | \n",
67 | " Glucose | \n",
68 | " BloodPressure | \n",
69 | " SkinThickness | \n",
70 | " Insulin | \n",
71 | " BMI | \n",
72 | " DiabetesPedigreeFunction | \n",
73 | " Age | \n",
74 | " Outcome | \n",
75 | "
\n",
76 | " \n",
77 | " \n",
78 | " \n",
79 | " count | \n",
80 | " 768.000000 | \n",
81 | " 768.000000 | \n",
82 | " 768.000000 | \n",
83 | " 768.000000 | \n",
84 | " 768.000000 | \n",
85 | " 768.000000 | \n",
86 | " 768.000000 | \n",
87 | " 768.000000 | \n",
88 | " 768.000000 | \n",
89 | "
\n",
90 | " \n",
91 | " mean | \n",
92 | " 3.845052 | \n",
93 | " 120.894531 | \n",
94 | " 69.105469 | \n",
95 | " 20.536458 | \n",
96 | " 79.799479 | \n",
97 | " 31.992578 | \n",
98 | " 0.471876 | \n",
99 | " 33.240885 | \n",
100 | " 0.348958 | \n",
101 | "
\n",
102 | " \n",
103 | " std | \n",
104 | " 3.369578 | \n",
105 | " 31.972618 | \n",
106 | " 19.355807 | \n",
107 | " 15.952218 | \n",
108 | " 115.244002 | \n",
109 | " 7.884160 | \n",
110 | " 0.331329 | \n",
111 | " 11.760232 | \n",
112 | " 0.476951 | \n",
113 | "
\n",
114 | " \n",
115 | " min | \n",
116 | " 0.000000 | \n",
117 | " 0.000000 | \n",
118 | " 0.000000 | \n",
119 | " 0.000000 | \n",
120 | " 0.000000 | \n",
121 | " 0.000000 | \n",
122 | " 0.078000 | \n",
123 | " 21.000000 | \n",
124 | " 0.000000 | \n",
125 | "
\n",
126 | " \n",
127 | " 25% | \n",
128 | " 1.000000 | \n",
129 | " 99.000000 | \n",
130 | " 62.000000 | \n",
131 | " 0.000000 | \n",
132 | " 0.000000 | \n",
133 | " 27.300000 | \n",
134 | " 0.243750 | \n",
135 | " 24.000000 | \n",
136 | " 0.000000 | \n",
137 | "
\n",
138 | " \n",
139 | " 50% | \n",
140 | " 3.000000 | \n",
141 | " 117.000000 | \n",
142 | " 72.000000 | \n",
143 | " 23.000000 | \n",
144 | " 30.500000 | \n",
145 | " 32.000000 | \n",
146 | " 0.372500 | \n",
147 | " 29.000000 | \n",
148 | " 0.000000 | \n",
149 | "
\n",
150 | " \n",
151 | " 75% | \n",
152 | " 6.000000 | \n",
153 | " 140.250000 | \n",
154 | " 80.000000 | \n",
155 | " 32.000000 | \n",
156 | " 127.250000 | \n",
157 | " 36.600000 | \n",
158 | " 0.626250 | \n",
159 | " 41.000000 | \n",
160 | " 1.000000 | \n",
161 | "
\n",
162 | " \n",
163 | " max | \n",
164 | " 17.000000 | \n",
165 | " 199.000000 | \n",
166 | " 122.000000 | \n",
167 | " 99.000000 | \n",
168 | " 846.000000 | \n",
169 | " 67.100000 | \n",
170 | " 2.420000 | \n",
171 | " 81.000000 | \n",
172 | " 1.000000 | \n",
173 | "
\n",
174 | " \n",
175 | "
\n",
176 | "
"
177 | ],
178 | "text/plain": [
179 | " Pregnancies Glucose BloodPressure SkinThickness Insulin \\\n",
180 | "count 768.000000 768.000000 768.000000 768.000000 768.000000 \n",
181 | "mean 3.845052 120.894531 69.105469 20.536458 79.799479 \n",
182 | "std 3.369578 31.972618 19.355807 15.952218 115.244002 \n",
183 | "min 0.000000 0.000000 0.000000 0.000000 0.000000 \n",
184 | "25% 1.000000 99.000000 62.000000 0.000000 0.000000 \n",
185 | "50% 3.000000 117.000000 72.000000 23.000000 30.500000 \n",
186 | "75% 6.000000 140.250000 80.000000 32.000000 127.250000 \n",
187 | "max 17.000000 199.000000 122.000000 99.000000 846.000000 \n",
188 | "\n",
189 | " BMI DiabetesPedigreeFunction Age Outcome \n",
190 | "count 768.000000 768.000000 768.000000 768.000000 \n",
191 | "mean 31.992578 0.471876 33.240885 0.348958 \n",
192 | "std 7.884160 0.331329 11.760232 0.476951 \n",
193 | "min 0.000000 0.078000 21.000000 0.000000 \n",
194 | "25% 27.300000 0.243750 24.000000 0.000000 \n",
195 | "50% 32.000000 0.372500 29.000000 0.000000 \n",
196 | "75% 36.600000 0.626250 41.000000 1.000000 \n",
197 | "max 67.100000 2.420000 81.000000 1.000000 "
198 | ]
199 | },
200 | "execution_count": 3,
201 | "metadata": {},
202 | "output_type": "execute_result"
203 | }
204 | ],
205 | "source": [
206 | "df.describe()"
207 | ]
208 | },
209 | {
210 | "cell_type": "markdown",
211 | "metadata": {},
212 | "source": [
213 | "## insulin looks like it might have some outliers \n",
214 | "- mean, quartlies a lot smaller than the max value\n",
215 | " - let's try to remove these outliers to check if they help with model performance"
216 | ]
217 | },
218 | {
219 | "cell_type": "markdown",
220 | "metadata": {},
221 | "source": [
222 | "## calculate the inter-quartile range\n",
223 | "- In descriptive statistics, the interquartile range (IQR), also called the midspread or middle 50%, or technically H-spread, is a measure of statistical dispersion, being equal to the difference between 75th and 25th percentiles, or between upper and lower quartiles, IQR = Q3 − Q1. In other words, the IQR is the first quartile subtracted from the third quartile; these quartiles can be clearly seen on a box plot on the data. (https://en.wikipedia.org/wiki/Interquartile_range)"
224 | ]
225 | },
226 | {
227 | "cell_type": "code",
228 | "execution_count": 7,
229 | "metadata": {},
230 | "outputs": [],
231 | "source": [
232 | "# calculate interquartile range\n",
233 | "q25, q75 = np.percentile(df['Insulin'], 25), np.percentile(df['Insulin'], 75)\n",
234 | "iqr = q75 - q25\n"
235 | ]
236 | },
237 | {
238 | "cell_type": "code",
239 | "execution_count": 8,
240 | "metadata": {},
241 | "outputs": [
242 | {
243 | "data": {
244 | "text/plain": [
245 | "127.25"
246 | ]
247 | },
248 | "execution_count": 8,
249 | "metadata": {},
250 | "output_type": "execute_result"
251 | }
252 | ],
253 | "source": [
254 | "iqr"
255 | ]
256 | },
257 | {
258 | "cell_type": "markdown",
259 | "metadata": {},
260 | "source": [
261 | "## let's remove all observations that have insulin higher than 127.25"
262 | ]
263 | },
264 | {
265 | "cell_type": "code",
266 | "execution_count": 9,
267 | "metadata": {},
268 | "outputs": [],
269 | "source": [
270 | "df2 = df[df['Insulin'] <= 127.25]"
271 | ]
272 | },
273 | {
274 | "cell_type": "code",
275 | "execution_count": 11,
276 | "metadata": {},
277 | "outputs": [
278 | {
279 | "name": "stdout",
280 | "output_type": "stream",
281 | "text": [
282 | "original df size: (768, 9)\n",
283 | "new df size: (576, 9)\n"
284 | ]
285 | }
286 | ],
287 | "source": [
288 | "print('original df size:', df.shape)\n",
289 | "print('new df size:', df2.shape)"
290 | ]
291 | },
292 | {
293 | "cell_type": "markdown",
294 | "metadata": {},
295 | "source": [
296 | "## let's compare models"
297 | ]
298 | },
299 | {
300 | "cell_type": "markdown",
301 | "metadata": {},
302 | "source": [
303 | "## baseline logistic regression model"
304 | ]
305 | },
306 | {
307 | "cell_type": "code",
308 | "execution_count": 22,
309 | "metadata": {},
310 | "outputs": [
311 | {
312 | "name": "stdout",
313 | "output_type": "stream",
314 | "text": [
315 | "Accuracy of logistic regression classifier on test set: 0.75\n"
316 | ]
317 | },
318 | {
319 | "data": {
320 | "text/html": [
321 | "\n",
322 | "\n",
335 | "
\n",
336 | " \n",
337 | " \n",
338 | " Predicted Result | \n",
339 | " 0 | \n",
340 | " 1 | \n",
341 | "
\n",
342 | " \n",
343 | " Actual Result | \n",
344 | " | \n",
345 | " | \n",
346 | "
\n",
347 | " \n",
348 | " \n",
349 | " \n",
350 | " 0 | \n",
351 | " 97 | \n",
352 | " 11 | \n",
353 | "
\n",
354 | " \n",
355 | " 1 | \n",
356 | " 28 | \n",
357 | " 18 | \n",
358 | "
\n",
359 | " \n",
360 | "
\n",
361 | "
"
362 | ],
363 | "text/plain": [
364 | "Predicted Result 0 1\n",
365 | "Actual Result \n",
366 | "0 97 11\n",
367 | "1 28 18"
368 | ]
369 | },
370 | "execution_count": 22,
371 | "metadata": {},
372 | "output_type": "execute_result"
373 | }
374 | ],
375 | "source": [
376 | "X, y = df.loc[:, df.columns != 'Outcome'], df['Outcome']\n",
377 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
378 | "logreg = LogisticRegression()\n",
379 | "logreg.fit(X_train, y_train)\n",
380 | "y_pred = logreg.predict(X_test)\n",
381 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))\n",
382 | "pd.crosstab(y_test, y_pred, rownames=['Actual Result'], colnames=['Predicted Result'])"
383 | ]
384 | },
385 | {
386 | "cell_type": "markdown",
387 | "metadata": {},
388 | "source": [
389 | "## logistic regression on data without insulin outliers"
390 | ]
391 | },
392 | {
393 | "cell_type": "code",
394 | "execution_count": 24,
395 | "metadata": {},
396 | "outputs": [
397 | {
398 | "name": "stdout",
399 | "output_type": "stream",
400 | "text": [
401 | "Accuracy of logistic regression classifier on test set: 0.80\n"
402 | ]
403 | },
404 | {
405 | "data": {
406 | "text/html": [
407 | "\n",
408 | "\n",
421 | "
\n",
422 | " \n",
423 | " \n",
424 | " Predicted Result | \n",
425 | " 0 | \n",
426 | " 1 | \n",
427 | "
\n",
428 | " \n",
429 | " Actual Result | \n",
430 | " | \n",
431 | " | \n",
432 | "
\n",
433 | " \n",
434 | " \n",
435 | " \n",
436 | " 0 | \n",
437 | " 81 | \n",
438 | " 5 | \n",
439 | "
\n",
440 | " \n",
441 | " 1 | \n",
442 | " 18 | \n",
443 | " 12 | \n",
444 | "
\n",
445 | " \n",
446 | "
\n",
447 | "
"
448 | ],
449 | "text/plain": [
450 | "Predicted Result 0 1\n",
451 | "Actual Result \n",
452 | "0 81 5\n",
453 | "1 18 12"
454 | ]
455 | },
456 | "execution_count": 24,
457 | "metadata": {},
458 | "output_type": "execute_result"
459 | }
460 | ],
461 | "source": [
462 | "X, y = df2.loc[:, df2.columns != 'Outcome'], df2['Outcome']\n",
463 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
464 | "logreg = LogisticRegression()\n",
465 | "logreg.fit(X_train, y_train)\n",
466 | "y_pred = logreg.predict(X_test)\n",
467 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))\n",
468 | "pd.crosstab(y_test, y_pred, rownames=['Actual Result'], colnames=['Predicted Result'])"
469 | ]
470 | }
471 | ],
472 | "metadata": {
473 | "kernelspec": {
474 | "display_name": "Python 3",
475 | "language": "python",
476 | "name": "python3"
477 | },
478 | "language_info": {
479 | "codemirror_mode": {
480 | "name": "ipython",
481 | "version": 3
482 | },
483 | "file_extension": ".py",
484 | "mimetype": "text/x-python",
485 | "name": "python",
486 | "nbconvert_exporter": "python",
487 | "pygments_lexer": "ipython3",
488 | "version": "3.6.0"
489 | }
490 | },
491 | "nbformat": 4,
492 | "nbformat_minor": 2
493 | }
494 |
--------------------------------------------------------------------------------
/Section 3/3.5.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [
8 | {
9 | "name": "stderr",
10 | "output_type": "stream",
11 | "text": [
12 | "/home/sahibachopra/miniconda/envs/ai/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88\n",
13 | " return f(*args, **kwds)\n"
14 | ]
15 | }
16 | ],
17 | "source": [
18 | "import pandas as pd\n",
19 | "import numpy as np\n",
20 | "from sklearn.model_selection import train_test_split\n",
21 | "from sklearn.linear_model import LogisticRegression"
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": 2,
27 | "metadata": {},
28 | "outputs": [],
29 | "source": [
30 | "df = pd.read_csv('diabetes.csv')"
31 | ]
32 | },
33 | {
34 | "cell_type": "markdown",
35 | "metadata": {},
36 | "source": [
37 | "## removing outliers using standard deviation\n",
38 | " - Standard deviation is a metric of variance i.e. how much the individual data points are spread out from the mean.\n",
39 | " - less reliable than IQR because the mean and standard deviation are impacted by the outliers\n",
40 | " - data must follow a Gaussian or normal distribution"
41 | ]
42 | },
43 | {
44 | "cell_type": "markdown",
45 | "metadata": {},
46 | "source": [
47 | "## let's remove outliers in the insulin variable\n",
48 | " - remove points that are above (Mean + 2 * SD) and any points below (Mean - 2 * SD)"
49 | ]
50 | },
51 | {
52 | "cell_type": "code",
53 | "execution_count": 4,
54 | "metadata": {},
55 | "outputs": [],
56 | "source": [
57 | "mean = np.mean(df['Insulin'])\n",
58 | "sd = np.std(df['Insulin'])"
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": 5,
64 | "metadata": {},
65 | "outputs": [],
66 | "source": [
67 | "df2 = df[(df['Insulin'] > mean - 2 * sd) & (df['Insulin'] < mean + 2 * sd)]"
68 | ]
69 | },
70 | {
71 | "cell_type": "markdown",
72 | "metadata": {},
73 | "source": [
74 | "## compare models"
75 | ]
76 | },
77 | {
78 | "cell_type": "markdown",
79 | "metadata": {},
80 | "source": [
81 | "## baseline logistic regression model"
82 | ]
83 | },
84 | {
85 | "cell_type": "code",
86 | "execution_count": 9,
87 | "metadata": {},
88 | "outputs": [
89 | {
90 | "name": "stdout",
91 | "output_type": "stream",
92 | "text": [
93 | "Accuracy of logistic regression classifier on test set: 0.75\n"
94 | ]
95 | },
96 | {
97 | "data": {
98 | "text/html": [
99 | "\n",
100 | "\n",
113 | "
\n",
114 | " \n",
115 | " \n",
116 | " Predicted Result | \n",
117 | " 0 | \n",
118 | " 1 | \n",
119 | "
\n",
120 | " \n",
121 | " Actual Result | \n",
122 | " | \n",
123 | " | \n",
124 | "
\n",
125 | " \n",
126 | " \n",
127 | " \n",
128 | " 0 | \n",
129 | " 90 | \n",
130 | " 9 | \n",
131 | "
\n",
132 | " \n",
133 | " 1 | \n",
134 | " 29 | \n",
135 | " 26 | \n",
136 | "
\n",
137 | " \n",
138 | "
\n",
139 | "
"
140 | ],
141 | "text/plain": [
142 | "Predicted Result 0 1\n",
143 | "Actual Result \n",
144 | "0 90 9\n",
145 | "1 29 26"
146 | ]
147 | },
148 | "execution_count": 9,
149 | "metadata": {},
150 | "output_type": "execute_result"
151 | }
152 | ],
153 | "source": [
154 | "X, y = df.loc[:, df.columns != 'Outcome'], df['Outcome']\n",
155 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
156 | "logreg = LogisticRegression()\n",
157 | "logreg.fit(X_train, y_train)\n",
158 | "y_pred = logreg.predict(X_test)\n",
159 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))\n",
160 | "pd.crosstab(y_test, y_pred, rownames=['Actual Result'], colnames=['Predicted Result'])"
161 | ]
162 | },
163 | {
164 | "cell_type": "markdown",
165 | "metadata": {},
166 | "source": [
167 | "## logistic regression model after removing outliers"
168 | ]
169 | },
170 | {
171 | "cell_type": "code",
172 | "execution_count": 13,
173 | "metadata": {},
174 | "outputs": [
175 | {
176 | "name": "stdout",
177 | "output_type": "stream",
178 | "text": [
179 | "Accuracy of logistic regression classifier on test set: 0.80\n"
180 | ]
181 | },
182 | {
183 | "data": {
184 | "text/html": [
185 | "\n",
186 | "\n",
199 | "
\n",
200 | " \n",
201 | " \n",
202 | " Predicted Result | \n",
203 | " 0 | \n",
204 | " 1 | \n",
205 | "
\n",
206 | " \n",
207 | " Actual Result | \n",
208 | " | \n",
209 | " | \n",
210 | "
\n",
211 | " \n",
212 | " \n",
213 | " \n",
214 | " 0 | \n",
215 | " 91 | \n",
216 | " 9 | \n",
217 | "
\n",
218 | " \n",
219 | " 1 | \n",
220 | " 21 | \n",
221 | " 26 | \n",
222 | "
\n",
223 | " \n",
224 | "
\n",
225 | "
"
226 | ],
227 | "text/plain": [
228 | "Predicted Result 0 1\n",
229 | "Actual Result \n",
230 | "0 91 9\n",
231 | "1 21 26"
232 | ]
233 | },
234 | "execution_count": 13,
235 | "metadata": {},
236 | "output_type": "execute_result"
237 | }
238 | ],
239 | "source": [
240 | "X, y = df2.loc[:, df2.columns != 'Outcome'], df2['Outcome']\n",
241 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
242 | "logreg = LogisticRegression()\n",
243 | "logreg.fit(X_train, y_train)\n",
244 | "y_pred = logreg.predict(X_test)\n",
245 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))\n",
246 | "pd.crosstab(y_test, y_pred, rownames=['Actual Result'], colnames=['Predicted Result'])"
247 | ]
248 | },
249 | {
250 | "cell_type": "markdown",
251 | "metadata": {},
252 | "source": [
253 | "## removing outliers using the median absolute deviation\n",
254 | "- Robust Z-Score method\n",
255 | "- source: https://stackoverflow.com/questions/22354094/pythonic-way-of-detecting-outliers-in-one-dimensional-observation-data?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa"
256 | ]
257 | },
258 | {
259 | "cell_type": "code",
260 | "execution_count": 15,
261 | "metadata": {},
262 | "outputs": [],
263 | "source": [
264 | "def mad_based_outlier(points, thresh=3.5):\n",
265 | " if len(points.shape) == 1:\n",
266 | " points = points[:,None]\n",
267 | " median = np.median(points, axis=0)\n",
268 | " diff = np.sum((points - median)**2, axis=-1)\n",
269 | " diff = np.sqrt(diff)\n",
270 | " med_abs_deviation = np.median(diff)\n",
271 | "\n",
272 | " modified_z_score = 0.6745 * diff / med_abs_deviation\n",
273 | "\n",
274 | " return modified_z_score > thresh"
275 | ]
276 | },
277 | {
278 | "cell_type": "code",
279 | "execution_count": 18,
280 | "metadata": {},
281 | "outputs": [],
282 | "source": [
283 | "df2 = df[mad_based_outlier(df['Insulin'])]"
284 | ]
285 | },
286 | {
287 | "cell_type": "code",
288 | "execution_count": 19,
289 | "metadata": {},
290 | "outputs": [
291 | {
292 | "data": {
293 | "text/plain": [
294 | "(101, 9)"
295 | ]
296 | },
297 | "execution_count": 19,
298 | "metadata": {},
299 | "output_type": "execute_result"
300 | }
301 | ],
302 | "source": [
303 | "df2.shape"
304 | ]
305 | },
306 | {
307 | "cell_type": "code",
308 | "execution_count": 20,
309 | "metadata": {},
310 | "outputs": [
311 | {
312 | "data": {
313 | "text/plain": [
314 | "(768, 9)"
315 | ]
316 | },
317 | "execution_count": 20,
318 | "metadata": {},
319 | "output_type": "execute_result"
320 | }
321 | ],
322 | "source": [
323 | "df.shape"
324 | ]
325 | },
326 | {
327 | "cell_type": "code",
328 | "execution_count": 26,
329 | "metadata": {},
330 | "outputs": [
331 | {
332 | "name": "stdout",
333 | "output_type": "stream",
334 | "text": [
335 | "Accuracy of logistic regression classifier on test set: 0.76\n"
336 | ]
337 | },
338 | {
339 | "data": {
340 | "text/html": [
341 | "\n",
342 | "\n",
355 | "
\n",
356 | " \n",
357 | " \n",
358 | " Predicted Result | \n",
359 | " 0 | \n",
360 | " 1 | \n",
361 | "
\n",
362 | " \n",
363 | " Actual Result | \n",
364 | " | \n",
365 | " | \n",
366 | "
\n",
367 | " \n",
368 | " \n",
369 | " \n",
370 | " 0 | \n",
371 | " 6 | \n",
372 | " 1 | \n",
373 | "
\n",
374 | " \n",
375 | " 1 | \n",
376 | " 4 | \n",
377 | " 10 | \n",
378 | "
\n",
379 | " \n",
380 | "
\n",
381 | "
"
382 | ],
383 | "text/plain": [
384 | "Predicted Result 0 1\n",
385 | "Actual Result \n",
386 | "0 6 1\n",
387 | "1 4 10"
388 | ]
389 | },
390 | "execution_count": 26,
391 | "metadata": {},
392 | "output_type": "execute_result"
393 | }
394 | ],
395 | "source": [
396 | "X, y = df2.loc[:, df2.columns != 'Outcome'], df2['Outcome']\n",
397 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
398 | "logreg = LogisticRegression()\n",
399 | "logreg.fit(X_train, y_train)\n",
400 | "y_pred = logreg.predict(X_test)\n",
401 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))\n",
402 | "pd.crosstab(y_test, y_pred, rownames=['Actual Result'], colnames=['Predicted Result'])"
403 | ]
404 | }
405 | ],
406 | "metadata": {
407 | "kernelspec": {
408 | "display_name": "Python 3",
409 | "language": "python",
410 | "name": "python3"
411 | },
412 | "language_info": {
413 | "codemirror_mode": {
414 | "name": "ipython",
415 | "version": 3
416 | },
417 | "file_extension": ".py",
418 | "mimetype": "text/x-python",
419 | "name": "python",
420 | "nbconvert_exporter": "python",
421 | "pygments_lexer": "ipython3",
422 | "version": "3.6.0"
423 | }
424 | },
425 | "nbformat": 4,
426 | "nbformat_minor": 2
427 | }
428 |
--------------------------------------------------------------------------------
/Section 4/4.1.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 3,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import pandas as pd\n",
10 | "import seaborn as sns\n",
11 | "import matplotlib.pyplot as plt\n",
12 | "from sklearn.feature_selection import SelectKBest\n",
13 | "from sklearn.feature_selection import chi2 # chi square filter method"
14 | ]
15 | },
16 | {
17 | "cell_type": "code",
18 | "execution_count": 2,
19 | "metadata": {},
20 | "outputs": [],
21 | "source": [
22 | "data = pd.read_csv('german_credit.csv')"
23 | ]
24 | },
25 | {
26 | "cell_type": "markdown",
27 | "metadata": {},
28 | "source": [
29 | "## Filter methods\n",
30 | "- feature selection independent of the model used\n",
31 | "- based on general methods like correlation\n",
32 | "- find the best subset of variables\n",
33 | "- helps reduce overfitting\n",
34 | "\n",
35 | "## Types of filter methods\n",
36 | "- information gain\n",
37 | "- chi-square test\n",
38 | "- fisher score\n",
39 | "- correlation coefficient\n",
40 | "- variance threshold"
41 | ]
42 | },
43 | {
44 | "cell_type": "markdown",
45 | "metadata": {},
46 | "source": [
47 | "## let's select the top 15 / 20 features with the higher chi squared statistics"
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": 5,
53 | "metadata": {},
54 | "outputs": [],
55 | "source": [
56 | "X, y = data.loc[:, data.columns!='Creditability'], data['Creditability']"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": 6,
62 | "metadata": {},
63 | "outputs": [],
64 | "source": [
65 | "chisq_selector = SelectKBest(chi2, k=15)\n",
66 | "X_kbest = chisq_selector.fit_transform(X, y)"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": 7,
72 | "metadata": {},
73 | "outputs": [
74 | {
75 | "name": "stdout",
76 | "output_type": "stream",
77 | "text": [
78 | "Original number of features: 20\n",
79 | "Reduced number of features: 15\n"
80 | ]
81 | }
82 | ],
83 | "source": [
84 | "print('Original number of features:', X.shape[1])\n",
85 | "print('Reduced number of features:', X_kbest.shape[1])\n"
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": 12,
91 | "metadata": {},
92 | "outputs": [
93 | {
94 | "data": {
95 | "text/plain": [
96 | "(1000, 15)"
97 | ]
98 | },
99 | "execution_count": 12,
100 | "metadata": {},
101 | "output_type": "execute_result"
102 | }
103 | ],
104 | "source": [
105 | "X_kbest.shape"
106 | ]
107 | },
108 | {
109 | "cell_type": "code",
110 | "execution_count": 21,
111 | "metadata": {},
112 | "outputs": [
113 | {
114 | "data": {
115 | "text/plain": [
116 | "array([ True, True, True, True, True, True, True, True, True,\n",
117 | " False, False, True, True, True, False, True, False, False,\n",
118 | " True, True])"
119 | ]
120 | },
121 | "execution_count": 21,
122 | "metadata": {},
123 | "output_type": "execute_result"
124 | }
125 | ],
126 | "source": [
127 | "chisq_selector.get_support()"
128 | ]
129 | },
130 | {
131 | "cell_type": "code",
132 | "execution_count": 22,
133 | "metadata": {},
134 | "outputs": [
135 | {
136 | "data": {
137 | "text/plain": [
138 | "Index(['Account Balance', 'Duration of Credit (month)',\n",
139 | " 'Payment Status of Previous Credit', 'Purpose', 'Credit Amount',\n",
140 | " 'Value Savings/Stocks', 'Length of current employment',\n",
141 | " 'Instalment per cent', 'Sex & Marital Status', 'Guarantors',\n",
142 | " 'Duration in Current address', 'Most valuable available asset',\n",
143 | " 'Age (years)', 'Concurrent Credits', 'Type of apartment',\n",
144 | " 'No of Credits at this Bank', 'Occupation', 'No of dependents',\n",
145 | " 'Telephone', 'Foreign Worker'],\n",
146 | " dtype='object')"
147 | ]
148 | },
149 | "execution_count": 22,
150 | "metadata": {},
151 | "output_type": "execute_result"
152 | }
153 | ],
154 | "source": [
155 | "X.columns"
156 | ]
157 | },
158 | {
159 | "cell_type": "code",
160 | "execution_count": 24,
161 | "metadata": {},
162 | "outputs": [
163 | {
164 | "name": "stdout",
165 | "output_type": "stream",
166 | "text": [
167 | "Index(['Account Balance', 'Duration of Credit (month)',\n",
168 | " 'Payment Status of Previous Credit', 'Purpose', 'Credit Amount',\n",
169 | " 'Value Savings/Stocks', 'Length of current employment',\n",
170 | " 'Instalment per cent', 'Sex & Marital Status',\n",
171 | " 'Most valuable available asset', 'Age (years)', 'Concurrent Credits',\n",
172 | " 'No of Credits at this Bank', 'Telephone', 'Foreign Worker'],\n",
173 | " dtype='object')\n"
174 | ]
175 | }
176 | ],
177 | "source": [
178 | "print(X.columns[chisq_selector.get_support()]) "
179 | ]
180 | },
181 | {
182 | "cell_type": "markdown",
183 | "metadata": {},
184 | "source": [
185 | "## dropped columns:\n",
186 | "1. Guarantors\n",
187 | "2. Duration in Current address\n",
188 | "3. Type of apartment\n",
189 | "4. Occupation\n",
190 | "5. No of dependents"
191 | ]
192 | }
193 | ],
194 | "metadata": {
195 | "kernelspec": {
196 | "display_name": "Python 3",
197 | "language": "python",
198 | "name": "python3"
199 | },
200 | "language_info": {
201 | "codemirror_mode": {
202 | "name": "ipython",
203 | "version": 3
204 | },
205 | "file_extension": ".py",
206 | "mimetype": "text/x-python",
207 | "name": "python",
208 | "nbconvert_exporter": "python",
209 | "pygments_lexer": "ipython3",
210 | "version": "3.6.0"
211 | }
212 | },
213 | "nbformat": 4,
214 | "nbformat_minor": 2
215 | }
216 |
--------------------------------------------------------------------------------
/Section 4/4.2.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 5,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import pandas as pd\n",
10 | "import seaborn as sns\n",
11 | "import matplotlib.pyplot as plt\n",
12 | "from sklearn.feature_selection import SelectKBest\n",
13 | "from sklearn.ensemble import RandomForestClassifier\n",
14 | "from sklearn.feature_selection import RFE"
15 | ]
16 | },
17 | {
18 | "cell_type": "code",
19 | "execution_count": 2,
20 | "metadata": {},
21 | "outputs": [],
22 | "source": [
23 | "data = pd.read_csv('german_credit.csv')"
24 | ]
25 | },
26 | {
27 | "cell_type": "markdown",
28 | "metadata": {},
29 | "source": [
30 | "## Wrapper methods\n",
31 | "- Wrapper methods evaluate subsets of variables which allows, unlike filter approaches, to detect the possible interactions between variables. \n",
32 | "The two main disadvantages of these methods are :\n",
33 | "\n",
34 | "1. The increasing overfitting risk when the number of observations is insufficient.\n",
35 | "2. The significant computation time when the number of variables is large.\n",
36 | "\n",
37 | "source: https://en.wikipedia.org/wiki/Feature_selection#Filter_method\n",
38 | "\n",
39 | "## Types of wrapper methods\n",
40 | "- recursive feature elimination\n",
41 | "- sequential feature selection algorithms\n",
42 | "- genetic algorithms"
43 | ]
44 | },
45 | {
46 | "cell_type": "markdown",
47 | "metadata": {},
48 | "source": [
49 | "Recursive feature elimination recursively removes features, builds a model using the remaining attributes and calculates model accuracy. RFE is able to work out the combination of attributes that contribute to the prediction on the target variable (or class). "
50 | ]
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": 8,
55 | "metadata": {},
56 | "outputs": [
57 | {
58 | "name": "stdout",
59 | "output_type": "stream",
60 | "text": [
61 | "[ True True True True True True True True True False True True\n",
62 | " True True False True True False False False]\n",
63 | "[1 1 1 1 1 1 1 1 1 2 1 1 1 1 4 1 1 5 3 6]\n"
64 | ]
65 | }
66 | ],
67 | "source": [
68 | "X, y = data.loc[:, data.columns!='Creditability'], data['Creditability']\n",
69 | "\n",
70 | "clf = RandomForestClassifier(n_jobs=2, random_state=0)\n",
71 | "# create the RFE model for a random forest classifier\n",
72 | "# and select attributes\n",
73 | "rfe = RFE(clf, 15)\n",
74 | "rfe = rfe.fit(X, y)\n",
75 | "# print summaries for the selection of attributes\n",
76 | "print(rfe.support_)\n",
77 | "print(rfe.ranking_)"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": 17,
83 | "metadata": {},
84 | "outputs": [
85 | {
86 | "data": {
87 | "text/plain": [
88 | "array([ True, True, True, True, True, True, True, True, True,\n",
89 | " False, True, True, True, True, False, True, True, False,\n",
90 | " False, False])"
91 | ]
92 | },
93 | "execution_count": 17,
94 | "metadata": {},
95 | "output_type": "execute_result"
96 | }
97 | ],
98 | "source": [
99 | "rfe.get_support()"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": 19,
105 | "metadata": {},
106 | "outputs": [
107 | {
108 | "name": "stdout",
109 | "output_type": "stream",
110 | "text": [
111 | "Index(['Account Balance', 'Duration of Credit (month)',\n",
112 | " 'Payment Status of Previous Credit', 'Purpose', 'Credit Amount',\n",
113 | " 'Value Savings/Stocks', 'Length of current employment',\n",
114 | " 'Instalment per cent', 'Sex & Marital Status', 'Guarantors',\n",
115 | " 'Duration in Current address', 'Most valuable available asset',\n",
116 | " 'Age (years)', 'Concurrent Credits', 'Type of apartment',\n",
117 | " 'No of Credits at this Bank', 'Occupation', 'No of dependents',\n",
118 | " 'Telephone', 'Foreign Worker'],\n",
119 | " dtype='object')\n"
120 | ]
121 | }
122 | ],
123 | "source": [
124 | "print(X.columns)"
125 | ]
126 | },
127 | {
128 | "cell_type": "code",
129 | "execution_count": 18,
130 | "metadata": {},
131 | "outputs": [
132 | {
133 | "name": "stdout",
134 | "output_type": "stream",
135 | "text": [
136 | "Index(['Account Balance', 'Duration of Credit (month)',\n",
137 | " 'Payment Status of Previous Credit', 'Purpose', 'Credit Amount',\n",
138 | " 'Value Savings/Stocks', 'Length of current employment',\n",
139 | " 'Instalment per cent', 'Sex & Marital Status',\n",
140 | " 'Duration in Current address', 'Most valuable available asset',\n",
141 | " 'Age (years)', 'Concurrent Credits', 'No of Credits at this Bank',\n",
142 | " 'Occupation'],\n",
143 | " dtype='object')\n"
144 | ]
145 | }
146 | ],
147 | "source": [
148 | "print(X.columns[rfe.get_support()]) "
149 | ]
150 | },
151 | {
152 | "cell_type": "markdown",
153 | "metadata": {},
154 | "source": [
155 | "dropped columns:\n",
156 | "1. Guarantors\n",
157 | "2. Type of apartment\n",
158 | "3. No of dependents\n",
159 | "4. Telephone\n",
160 | "5. Foreign Worker"
161 | ]
162 | },
163 | {
164 | "cell_type": "code",
165 | "execution_count": 13,
166 | "metadata": {},
167 | "outputs": [
168 | {
169 | "name": "stdout",
170 | "output_type": "stream",
171 | "text": [
172 | "Features sorted by their rank:\n",
173 | "[(1, 'Account Balance'), (1, 'Age (years)'), (1, 'Concurrent Credits'), (1, 'Credit Amount'), (1, 'Duration in Current address'), (1, 'Duration of Credit (month)'), (1, 'Instalment per cent'), (1, 'Length of current employment'), (1, 'Most valuable available asset'), (1, 'No of Credits at this Bank'), (1, 'Occupation'), (1, 'Payment Status of Previous Credit'), (1, 'Purpose'), (1, 'Sex & Marital Status'), (1, 'Value Savings/Stocks'), (2, 'Guarantors'), (3, 'Telephone'), (4, 'Type of apartment'), (5, 'No of dependents'), (6, 'Foreign Worker')]\n"
174 | ]
175 | }
176 | ],
177 | "source": [
178 | "names = X.columns\n",
179 | "print (\"Features sorted by their rank:\")\n",
180 | "print (sorted(zip(map(lambda x: round(x, 4), rfe.ranking_), names)))"
181 | ]
182 | },
183 | {
184 | "cell_type": "code",
185 | "execution_count": null,
186 | "metadata": {},
187 | "outputs": [],
188 | "source": []
189 | }
190 | ],
191 | "metadata": {
192 | "kernelspec": {
193 | "display_name": "Python 3",
194 | "language": "python",
195 | "name": "python3"
196 | },
197 | "language_info": {
198 | "codemirror_mode": {
199 | "name": "ipython",
200 | "version": 3
201 | },
202 | "file_extension": ".py",
203 | "mimetype": "text/x-python",
204 | "name": "python",
205 | "nbconvert_exporter": "python",
206 | "pygments_lexer": "ipython3",
207 | "version": "3.6.0"
208 | }
209 | },
210 | "nbformat": 4,
211 | "nbformat_minor": 2
212 | }
213 |
--------------------------------------------------------------------------------
/Section 4/4.3.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 7,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import pandas as pd\n",
10 | "from sklearn.ensemble import RandomForestClassifier\n",
11 | "from sklearn import metrics\n",
12 | "import numpy as np"
13 | ]
14 | },
15 | {
16 | "cell_type": "code",
17 | "execution_count": 2,
18 | "metadata": {},
19 | "outputs": [],
20 | "source": [
21 | "data = pd.read_csv('german_credit.csv')"
22 | ]
23 | },
24 | {
25 | "cell_type": "markdown",
26 | "metadata": {},
27 | "source": [
28 | "## Feature importance:\n",
29 | "- use decision tree classifiers to find the relative importance of the features\n",
30 | "- These importance values can be used to inform a feature selection process."
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 6,
36 | "metadata": {},
37 | "outputs": [
38 | {
39 | "name": "stdout",
40 | "output_type": "stream",
41 | "text": [
42 | "[0.12689479 0.09685797 0.05581169 0.061145 0.142556 0.04732479\n",
43 | " 0.05096343 0.04116976 0.0362849 0.01631357 0.04774687 0.03821556\n",
44 | " 0.10748535 0.01597799 0.02453943 0.02408641 0.02888386 0.01614243\n",
45 | " 0.01640286 0.00519733]\n"
46 | ]
47 | }
48 | ],
49 | "source": [
50 | "X, y = data.loc[:, data.columns!='Creditability'], data['Creditability']\n",
51 | "\n",
52 | "model = RandomForestClassifier(n_jobs=2, random_state=0)\n",
53 | "model.fit(X, y)\n",
54 | "# display the relative importance of each attribute\n",
55 | "print(model.feature_importances_)"
56 | ]
57 | },
58 | {
59 | "cell_type": "code",
60 | "execution_count": 11,
61 | "metadata": {},
62 | "outputs": [
63 | {
64 | "data": {
65 | "text/html": [
66 | "\n",
67 | "\n",
80 | "
\n",
81 | " \n",
82 | " \n",
83 | " | \n",
84 | " Gini-importance | \n",
85 | "
\n",
86 | " \n",
87 | " \n",
88 | " \n",
89 | " Credit Amount | \n",
90 | " 0.142556 | \n",
91 | "
\n",
92 | " \n",
93 | " Account Balance | \n",
94 | " 0.126895 | \n",
95 | "
\n",
96 | " \n",
97 | " Age (years) | \n",
98 | " 0.107485 | \n",
99 | "
\n",
100 | " \n",
101 | " Duration of Credit (month) | \n",
102 | " 0.096858 | \n",
103 | "
\n",
104 | " \n",
105 | " Purpose | \n",
106 | " 0.061145 | \n",
107 | "
\n",
108 | " \n",
109 | " Payment Status of Previous Credit | \n",
110 | " 0.055812 | \n",
111 | "
\n",
112 | " \n",
113 | " Length of current employment | \n",
114 | " 0.050963 | \n",
115 | "
\n",
116 | " \n",
117 | " Duration in Current address | \n",
118 | " 0.047747 | \n",
119 | "
\n",
120 | " \n",
121 | " Value Savings/Stocks | \n",
122 | " 0.047325 | \n",
123 | "
\n",
124 | " \n",
125 | " Instalment per cent | \n",
126 | " 0.041170 | \n",
127 | "
\n",
128 | " \n",
129 | " Most valuable available asset | \n",
130 | " 0.038216 | \n",
131 | "
\n",
132 | " \n",
133 | " Sex & Marital Status | \n",
134 | " 0.036285 | \n",
135 | "
\n",
136 | " \n",
137 | " Occupation | \n",
138 | " 0.028884 | \n",
139 | "
\n",
140 | " \n",
141 | " Type of apartment | \n",
142 | " 0.024539 | \n",
143 | "
\n",
144 | " \n",
145 | " No of Credits at this Bank | \n",
146 | " 0.024086 | \n",
147 | "
\n",
148 | " \n",
149 | " Telephone | \n",
150 | " 0.016403 | \n",
151 | "
\n",
152 | " \n",
153 | " Guarantors | \n",
154 | " 0.016314 | \n",
155 | "
\n",
156 | " \n",
157 | " No of dependents | \n",
158 | " 0.016142 | \n",
159 | "
\n",
160 | " \n",
161 | " Concurrent Credits | \n",
162 | " 0.015978 | \n",
163 | "
\n",
164 | " \n",
165 | " Foreign Worker | \n",
166 | " 0.005197 | \n",
167 | "
\n",
168 | " \n",
169 | "
\n",
170 | "
"
171 | ],
172 | "text/plain": [
173 | " Gini-importance\n",
174 | "Credit Amount 0.142556\n",
175 | "Account Balance 0.126895\n",
176 | "Age (years) 0.107485\n",
177 | "Duration of Credit (month) 0.096858\n",
178 | "Purpose 0.061145\n",
179 | "Payment Status of Previous Credit 0.055812\n",
180 | "Length of current employment 0.050963\n",
181 | "Duration in Current address 0.047747\n",
182 | "Value Savings/Stocks 0.047325\n",
183 | "Instalment per cent 0.041170\n",
184 | "Most valuable available asset 0.038216\n",
185 | "Sex & Marital Status 0.036285\n",
186 | "Occupation 0.028884\n",
187 | "Type of apartment 0.024539\n",
188 | "No of Credits at this Bank 0.024086\n",
189 | "Telephone 0.016403\n",
190 | "Guarantors 0.016314\n",
191 | "No of dependents 0.016142\n",
192 | "Concurrent Credits 0.015978\n",
193 | "Foreign Worker 0.005197"
194 | ]
195 | },
196 | "execution_count": 11,
197 | "metadata": {},
198 | "output_type": "execute_result"
199 | }
200 | ],
201 | "source": [
202 | "feats = {} # a dict to hold feature_name: feature_importance\n",
203 | "for feature, importance in zip(X.columns, model.feature_importances_):\n",
204 | " feats[feature] = importance #add the name/value pair \n",
205 | "\n",
206 | "importances = pd.DataFrame.from_dict(feats, orient='index').rename(columns={0: 'Gini-importance'})\n",
207 | "importances.sort_values(by='Gini-importance', ascending=False)"
208 | ]
209 | }
210 | ],
211 | "metadata": {
212 | "kernelspec": {
213 | "display_name": "Python 3",
214 | "language": "python",
215 | "name": "python3"
216 | },
217 | "language_info": {
218 | "codemirror_mode": {
219 | "name": "ipython",
220 | "version": 3
221 | },
222 | "file_extension": ".py",
223 | "mimetype": "text/x-python",
224 | "name": "python",
225 | "nbconvert_exporter": "python",
226 | "pygments_lexer": "ipython3",
227 | "version": "3.6.0"
228 | }
229 | },
230 | "nbformat": 4,
231 | "nbformat_minor": 2
232 | }
233 |
--------------------------------------------------------------------------------
/Section 5/5.1.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [
8 | {
9 | "name": "stderr",
10 | "output_type": "stream",
11 | "text": [
12 | "/home/sahibachopra/miniconda/envs/ai/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88\n",
13 | " return f(*args, **kwds)\n"
14 | ]
15 | }
16 | ],
17 | "source": [
18 | "import pandas as pd\n",
19 | "from sklearn.model_selection import train_test_split\n",
20 | "from sklearn.linear_model import LogisticRegression\n",
21 | "from sklearn.metrics import classification_report # classification report"
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": 2,
27 | "metadata": {},
28 | "outputs": [],
29 | "source": [
30 | "df = pd.read_csv('german_credit.csv')"
31 | ]
32 | },
33 | {
34 | "cell_type": "markdown",
35 | "metadata": {},
36 | "source": [
37 | "## build a baseline logistic regression model"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": 11,
43 | "metadata": {},
44 | "outputs": [
45 | {
46 | "name": "stdout",
47 | "output_type": "stream",
48 | "text": [
49 | "Accuracy of logistic regression classifier on test set: 0.74\n"
50 | ]
51 | },
52 | {
53 | "data": {
54 | "text/html": [
55 | "\n",
56 | "\n",
69 | "
\n",
70 | " \n",
71 | " \n",
72 | " Predicted Result | \n",
73 | " 0 | \n",
74 | " 1 | \n",
75 | "
\n",
76 | " \n",
77 | " Actual Result | \n",
78 | " | \n",
79 | " | \n",
80 | "
\n",
81 | " \n",
82 | " \n",
83 | " \n",
84 | " 0 | \n",
85 | " 29 | \n",
86 | " 34 | \n",
87 | "
\n",
88 | " \n",
89 | " 1 | \n",
90 | " 17 | \n",
91 | " 120 | \n",
92 | "
\n",
93 | " \n",
94 | "
\n",
95 | "
"
96 | ],
97 | "text/plain": [
98 | "Predicted Result 0 1\n",
99 | "Actual Result \n",
100 | "0 29 34\n",
101 | "1 17 120"
102 | ]
103 | },
104 | "execution_count": 11,
105 | "metadata": {},
106 | "output_type": "execute_result"
107 | }
108 | ],
109 | "source": [
110 | "X, y = df.loc[:, df.columns != 'Creditability'], df['Creditability']\n",
111 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
112 | "logreg = LogisticRegression()\n",
113 | "logreg.fit(X_train, y_train)\n",
114 | "y_pred = logreg.predict(X_test)\n",
115 | "print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))\n",
116 | "pd.crosstab(y_test, y_pred, rownames=['Actual Result'], colnames=['Predicted Result'])"
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": 12,
122 | "metadata": {},
123 | "outputs": [
124 | {
125 | "name": "stdout",
126 | "output_type": "stream",
127 | "text": [
128 | " precision recall f1-score support\n",
129 | "\n",
130 | " 0 0.63 0.46 0.53 63\n",
131 | " 1 0.78 0.88 0.82 137\n",
132 | "\n",
133 | "avg / total 0.73 0.74 0.73 200\n",
134 | "\n"
135 | ]
136 | }
137 | ],
138 | "source": [
139 | "print(classification_report(y_test, y_pred))"
140 | ]
141 | }
142 | ],
143 | "metadata": {
144 | "kernelspec": {
145 | "display_name": "Python 3",
146 | "language": "python",
147 | "name": "python3"
148 | },
149 | "language_info": {
150 | "codemirror_mode": {
151 | "name": "ipython",
152 | "version": 3
153 | },
154 | "file_extension": ".py",
155 | "mimetype": "text/x-python",
156 | "name": "python",
157 | "nbconvert_exporter": "python",
158 | "pygments_lexer": "ipython3",
159 | "version": "3.6.0"
160 | }
161 | },
162 | "nbformat": 4,
163 | "nbformat_minor": 2
164 | }
165 |
--------------------------------------------------------------------------------
/Section 5/5.2.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 22,
6 | "metadata": {},
7 | "outputs": [
8 | {
9 | "name": "stderr",
10 | "output_type": "stream",
11 | "text": [
12 | "/home/sahibachopra/miniconda/envs/ai/lib/python3.6/site-packages/sklearn/ensemble/weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.\n",
13 | " from numpy.core.umath_tests import inner1d\n"
14 | ]
15 | }
16 | ],
17 | "source": [
18 | "import pandas as pd\n",
19 | "from sklearn.model_selection import train_test_split\n",
20 | "from sklearn.linear_model import LogisticRegression\n",
21 | "from sklearn.metrics import classification_report\n",
22 | "from sklearn.feature_selection import SelectKBest\n",
23 | "from sklearn.feature_selection import chi2\n",
24 | "from sklearn.preprocessing import scale\n",
25 | "from sklearn.decomposition import PCA\n",
26 | "import numpy as np\n",
27 | "import matplotlib.pyplot as plt\n",
28 | "from sklearn.ensemble import RandomForestClassifier\n",
29 | "from sklearn.feature_selection import RFE"
30 | ]
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "## apply feature engineering to these data points\n",
37 | "- feature importance\n",
38 | "- feature selection: filter method\n",
39 | "- feature extraction / dimensionality reduction: PCA"
40 | ]
41 | },
42 | {
43 | "cell_type": "code",
44 | "execution_count": 3,
45 | "metadata": {},
46 | "outputs": [],
47 | "source": [
48 | "data = pd.read_csv('german_credit.csv')"
49 | ]
50 | },
51 | {
52 | "cell_type": "code",
53 | "execution_count": 47,
54 | "metadata": {},
55 | "outputs": [
56 | {
57 | "data": {
58 | "text/plain": [
59 | "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
60 | " max_depth=None, max_features='auto', max_leaf_nodes=None,\n",
61 | " min_impurity_decrease=0.0, min_impurity_split=None,\n",
62 | " min_samples_leaf=1, min_samples_split=2,\n",
63 | " min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=2,\n",
64 | " oob_score=False, random_state=0, verbose=0, warm_start=False)"
65 | ]
66 | },
67 | "execution_count": 47,
68 | "metadata": {},
69 | "output_type": "execute_result"
70 | }
71 | ],
72 | "source": [
73 | "# feature importance\n",
74 | "X, y = data.loc[:, data.columns!='Creditability'], data['Creditability']\n",
75 | "\n",
76 | "model = RandomForestClassifier(n_jobs=2, random_state=0)\n",
77 | "model.fit(X, y)"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": 48,
83 | "metadata": {},
84 | "outputs": [
85 | {
86 | "data": {
87 | "text/html": [
88 | "\n",
89 | "\n",
102 | "
\n",
103 | " \n",
104 | " \n",
105 | " | \n",
106 | " Gini-importance | \n",
107 | "
\n",
108 | " \n",
109 | " \n",
110 | " \n",
111 | " Credit Amount | \n",
112 | " 0.142556 | \n",
113 | "
\n",
114 | " \n",
115 | " Account Balance | \n",
116 | " 0.126895 | \n",
117 | "
\n",
118 | " \n",
119 | " Age (years) | \n",
120 | " 0.107485 | \n",
121 | "
\n",
122 | " \n",
123 | " Duration of Credit (month) | \n",
124 | " 0.096858 | \n",
125 | "
\n",
126 | " \n",
127 | " Purpose | \n",
128 | " 0.061145 | \n",
129 | "
\n",
130 | " \n",
131 | " Payment Status of Previous Credit | \n",
132 | " 0.055812 | \n",
133 | "
\n",
134 | " \n",
135 | " Length of current employment | \n",
136 | " 0.050963 | \n",
137 | "
\n",
138 | " \n",
139 | " Duration in Current address | \n",
140 | " 0.047747 | \n",
141 | "
\n",
142 | " \n",
143 | " Value Savings/Stocks | \n",
144 | " 0.047325 | \n",
145 | "
\n",
146 | " \n",
147 | " Instalment per cent | \n",
148 | " 0.041170 | \n",
149 | "
\n",
150 | " \n",
151 | " Most valuable available asset | \n",
152 | " 0.038216 | \n",
153 | "
\n",
154 | " \n",
155 | " Sex & Marital Status | \n",
156 | " 0.036285 | \n",
157 | "
\n",
158 | " \n",
159 | " Occupation | \n",
160 | " 0.028884 | \n",
161 | "
\n",
162 | " \n",
163 | " Type of apartment | \n",
164 | " 0.024539 | \n",
165 | "
\n",
166 | " \n",
167 | " No of Credits at this Bank | \n",
168 | " 0.024086 | \n",
169 | "
\n",
170 | " \n",
171 | " Telephone | \n",
172 | " 0.016403 | \n",
173 | "
\n",
174 | " \n",
175 | " Guarantors | \n",
176 | " 0.016314 | \n",
177 | "
\n",
178 | " \n",
179 | " No of dependents | \n",
180 | " 0.016142 | \n",
181 | "
\n",
182 | " \n",
183 | " Concurrent Credits | \n",
184 | " 0.015978 | \n",
185 | "
\n",
186 | " \n",
187 | " Foreign Worker | \n",
188 | " 0.005197 | \n",
189 | "
\n",
190 | " \n",
191 | "
\n",
192 | "
"
193 | ],
194 | "text/plain": [
195 | " Gini-importance\n",
196 | "Credit Amount 0.142556\n",
197 | "Account Balance 0.126895\n",
198 | "Age (years) 0.107485\n",
199 | "Duration of Credit (month) 0.096858\n",
200 | "Purpose 0.061145\n",
201 | "Payment Status of Previous Credit 0.055812\n",
202 | "Length of current employment 0.050963\n",
203 | "Duration in Current address 0.047747\n",
204 | "Value Savings/Stocks 0.047325\n",
205 | "Instalment per cent 0.041170\n",
206 | "Most valuable available asset 0.038216\n",
207 | "Sex & Marital Status 0.036285\n",
208 | "Occupation 0.028884\n",
209 | "Type of apartment 0.024539\n",
210 | "No of Credits at this Bank 0.024086\n",
211 | "Telephone 0.016403\n",
212 | "Guarantors 0.016314\n",
213 | "No of dependents 0.016142\n",
214 | "Concurrent Credits 0.015978\n",
215 | "Foreign Worker 0.005197"
216 | ]
217 | },
218 | "execution_count": 48,
219 | "metadata": {},
220 | "output_type": "execute_result"
221 | }
222 | ],
223 | "source": [
224 | "feats = {} # a dict to hold feature_name: feature_importance\n",
225 | "for feature, importance in zip(X.columns, model.feature_importances_):\n",
226 | " feats[feature] = importance #add the name/value pair \n",
227 | "\n",
228 | "importances = pd.DataFrame.from_dict(feats, orient='index').rename(columns={0: 'Gini-importance'})\n",
229 | "importances.sort_values(by='Gini-importance', ascending=False)"
230 | ]
231 | },
232 | {
233 | "cell_type": "code",
234 | "execution_count": 49,
235 | "metadata": {},
236 | "outputs": [],
237 | "source": [
238 | "X, y = data.loc[:, data.columns!='Creditability'], data['Creditability']"
239 | ]
240 | },
241 | {
242 | "cell_type": "code",
243 | "execution_count": 50,
244 | "metadata": {},
245 | "outputs": [],
246 | "source": [
247 | "# filter method\n",
248 | "chisq_selector = SelectKBest(chi2, k=15)\n",
249 | "X_kbest = chisq_selector.fit_transform(X, y)"
250 | ]
251 | },
252 | {
253 | "cell_type": "code",
254 | "execution_count": 6,
255 | "metadata": {},
256 | "outputs": [],
257 | "source": [
258 | "X_new = X[X.columns[chisq_selector.get_support()]]"
259 | ]
260 | },
261 | {
262 | "cell_type": "code",
263 | "execution_count": 12,
264 | "metadata": {},
265 | "outputs": [
266 | {
267 | "name": "stderr",
268 | "output_type": "stream",
269 | "text": [
270 | "/home/sahibachopra/miniconda/envs/ai/lib/python3.6/site-packages/sklearn/utils/validation.py:475: DataConversionWarning: Data with input dtype int64 was converted to float64 by the scale function.\n",
271 | " warnings.warn(msg, DataConversionWarning)\n"
272 | ]
273 | }
274 | ],
275 | "source": [
276 | "# PCA\n",
277 | "x = X.values \n",
278 | "x = scale(x)"
279 | ]
280 | },
281 | {
282 | "cell_type": "code",
283 | "execution_count": 15,
284 | "metadata": {},
285 | "outputs": [],
286 | "source": [
287 | "covar_matrix = PCA(n_components = 15) #we have 15 features"
288 | ]
289 | },
290 | {
291 | "cell_type": "code",
292 | "execution_count": 18,
293 | "metadata": {},
294 | "outputs": [],
295 | "source": [
296 | "covar_matrix.fit(x)\n",
297 | "variance = covar_matrix.explained_variance_ratio_ #calculate variance ratios\n",
298 | "\n",
299 | "var=np.cumsum(np.round(covar_matrix.explained_variance_ratio_, decimals=3)*100)"
300 | ]
301 | },
302 | {
303 | "cell_type": "code",
304 | "execution_count": 21,
305 | "metadata": {},
306 | "outputs": [
307 | {
308 | "data": {
309 | "text/plain": [
310 | "[]"
311 | ]
312 | },
313 | "execution_count": 21,
314 | "metadata": {},
315 | "output_type": "execute_result"
316 | },
317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L3soaYbgSuCCe1LyW4nvZhC9v4OTA1fK1pQNvwil8LgM+BfwH/Lfa0R4G3iiapCS5Y9DpBqGWW0dzvgNrAYgsuZP+7cPuFwGdhb6kX8ER5fhaRIpqkFhGRiNSDEBGRiBQQIiISkQJCREQiUkCIiEhECggREYlIASEiIhEpIEREJKL/B0MeQT8ji5a0AAAAAElFTkSuQmCC\n",
320 | "text/plain": [
321 | ""
322 | ]
323 | },
324 | "metadata": {
325 | "needs_background": "light"
326 | },
327 | "output_type": "display_data"
328 | }
329 | ],
330 | "source": [
331 | "plt.ylabel('% Variance Explained')\n",
332 | "plt.xlabel('# of Features')\n",
333 | "plt.title('PCA Analysis')\n",
334 | "plt.ylim(0,110)\n",
335 | "plt.style.context('seaborn-whitegrid')\n",
336 | "\n",
337 | "\n",
338 | "plt.plot(var)"
339 | ]
340 | },
341 | {
342 | "cell_type": "code",
343 | "execution_count": 28,
344 | "metadata": {},
345 | "outputs": [],
346 | "source": [
347 | "# feature selection using wrapper method\n",
348 | "\n",
349 | "clf = RandomForestClassifier(n_jobs=2, random_state=0)\n",
350 | "# create the RFE model for the svm classifier \n",
351 | "# and select attributes\n",
352 | "rfe = RFE(clf, 13)\n",
353 | "rfe = rfe.fit(X_new, y)"
354 | ]
355 | },
356 | {
357 | "cell_type": "code",
358 | "execution_count": 29,
359 | "metadata": {},
360 | "outputs": [],
361 | "source": [
362 | "X_new_v2 = X_new[X_new.columns[rfe.get_support()]]"
363 | ]
364 | },
365 | {
366 | "cell_type": "code",
367 | "execution_count": 30,
368 | "metadata": {},
369 | "outputs": [
370 | {
371 | "data": {
372 | "text/plain": [
373 | "(1000, 13)"
374 | ]
375 | },
376 | "execution_count": 30,
377 | "metadata": {},
378 | "output_type": "execute_result"
379 | }
380 | ],
381 | "source": [
382 | "X_new_v2.shape"
383 | ]
384 | },
385 | {
386 | "cell_type": "code",
387 | "execution_count": 31,
388 | "metadata": {},
389 | "outputs": [
390 | {
391 | "name": "stdout",
392 | "output_type": "stream",
393 | "text": [
394 | "Features sorted by their rank:\n",
395 | "[(1, 'Account Balance'), (1, 'Age (years)'), (1, 'Concurrent Credits'), (1, 'Credit Amount'), (1, 'Duration of Credit (month)'), (1, 'Instalment per cent'), (1, 'Length of current employment'), (1, 'Most valuable available asset'), (1, 'No of Credits at this Bank'), (1, 'Payment Status of Previous Credit'), (1, 'Purpose'), (1, 'Sex & Marital Status'), (1, 'Value Savings/Stocks'), (2, 'Telephone'), (3, 'Foreign Worker')]\n"
396 | ]
397 | }
398 | ],
399 | "source": [
400 | "names = X_new.columns\n",
401 | "print (\"Features sorted by their rank:\")\n",
402 | "print (sorted(zip(map(lambda x: round(x, 4), rfe.ranking_), names)))"
403 | ]
404 | },
405 | {
406 | "cell_type": "code",
407 | "execution_count": 52,
408 | "metadata": {},
409 | "outputs": [],
410 | "source": [
411 | "X_new_v2.to_csv('fe_data.csv', index=False)"
412 | ]
413 | },
414 | {
415 | "cell_type": "code",
416 | "execution_count": 58,
417 | "metadata": {},
418 | "outputs": [],
419 | "source": [
420 | "X_new.to_csv('fe_data.csv', index=False)"
421 | ]
422 | },
423 | {
424 | "cell_type": "code",
425 | "execution_count": 57,
426 | "metadata": {},
427 | "outputs": [],
428 | "source": [
429 | "pd.DataFrame(y).to_csv('y.csv', index= False)"
430 | ]
431 | },
432 | {
433 | "cell_type": "code",
434 | "execution_count": 54,
435 | "metadata": {},
436 | "outputs": [
437 | {
438 | "data": {
439 | "text/plain": [
440 | "(1000,)"
441 | ]
442 | },
443 | "execution_count": 54,
444 | "metadata": {},
445 | "output_type": "execute_result"
446 | }
447 | ],
448 | "source": [
449 | "y.shape"
450 | ]
451 | }
452 | ],
453 | "metadata": {
454 | "kernelspec": {
455 | "display_name": "Python 3",
456 | "language": "python",
457 | "name": "python3"
458 | },
459 | "language_info": {
460 | "codemirror_mode": {
461 | "name": "ipython",
462 | "version": 3
463 | },
464 | "file_extension": ".py",
465 | "mimetype": "text/x-python",
466 | "name": "python",
467 | "nbconvert_exporter": "python",
468 | "pygments_lexer": "ipython3",
469 | "version": "3.6.0"
470 | }
471 | },
472 | "nbformat": 4,
473 | "nbformat_minor": 2
474 | }
475 |
--------------------------------------------------------------------------------
/Section 5/5.3.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 44,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import pandas as pd\n",
10 | "from sklearn.model_selection import train_test_split\n",
11 | "from sklearn.linear_model import LogisticRegression\n",
12 | "from sklearn.metrics import classification_report\n",
13 | "import numpy as np\n",
14 | "from sklearn.ensemble import RandomForestClassifier\n",
15 | "import pandas\n",
16 | "from sklearn import model_selection\n",
17 | "from sklearn.linear_model import LogisticRegression\n",
18 | "from sklearn.ensemble import VotingClassifier\n",
19 | "from sklearn.svm import SVC"
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": 58,
25 | "metadata": {},
26 | "outputs": [],
27 | "source": [
28 | "X = pd.read_csv('fe_data.csv')\n",
29 | "y = pd.read_csv('y.csv') "
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": 60,
35 | "metadata": {},
36 | "outputs": [],
37 | "source": [
38 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)"
39 | ]
40 | },
41 | {
42 | "cell_type": "markdown",
43 | "metadata": {},
44 | "source": [
45 | "## ensemble model:\n",
46 | "- Random Forest\n",
47 | "- Linear Regression\n",
48 | "- Support Vector Classifier"
49 | ]
50 | },
51 | {
52 | "cell_type": "code",
53 | "execution_count": 80,
54 | "metadata": {},
55 | "outputs": [
56 | {
57 | "name": "stderr",
58 | "output_type": "stream",
59 | "text": [
60 | "/home/sahibachopra/miniconda/envs/ai/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
61 | " y = column_or_1d(y, warn=True)\n",
62 | "/home/sahibachopra/miniconda/envs/ai/lib/python3.6/site-packages/ipykernel_launcher.py:6: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples,), for example using ravel().\n",
63 | " \n",
64 | "/home/sahibachopra/miniconda/envs/ai/lib/python3.6/site-packages/sklearn/utils/validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
65 | " y = column_or_1d(y, warn=True)\n",
66 | "/home/sahibachopra/miniconda/envs/ai/lib/python3.6/site-packages/sklearn/preprocessing/label.py:95: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
67 | " y = column_or_1d(y, warn=True)\n",
68 | "/home/sahibachopra/miniconda/envs/ai/lib/python3.6/site-packages/sklearn/preprocessing/label.py:128: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
69 | " y = column_or_1d(y, warn=True)\n",
70 | "/home/sahibachopra/miniconda/envs/ai/lib/python3.6/site-packages/sklearn/preprocessing/label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
71 | " if diff:\n"
72 | ]
73 | },
74 | {
75 | "data": {
76 | "text/plain": [
77 | "0.785"
78 | ]
79 | },
80 | "execution_count": 80,
81 | "metadata": {},
82 | "output_type": "execute_result"
83 | }
84 | ],
85 | "source": [
86 | "# baseline model accuracy = 0.74\n",
87 | "model1 = RandomForestClassifier()\n",
88 | "model2= LogisticRegression()\n",
89 | "model3 = SVC()\n",
90 | "\n",
91 | "model = VotingClassifier(estimators=[('rf', model1), ('lr', model2), ('svc', model3)], voting='hard')\n",
92 | "model.fit(X_train,y_train)\n",
93 | "model.score(X_test,y_test)"
94 | ]
95 | }
96 | ],
97 | "metadata": {
98 | "kernelspec": {
99 | "display_name": "Python 3",
100 | "language": "python",
101 | "name": "python3"
102 | },
103 | "language_info": {
104 | "codemirror_mode": {
105 | "name": "ipython",
106 | "version": 3
107 | },
108 | "file_extension": ".py",
109 | "mimetype": "text/x-python",
110 | "name": "python",
111 | "nbconvert_exporter": "python",
112 | "pygments_lexer": "ipython3",
113 | "version": "3.6.0"
114 | }
115 | },
116 | "nbformat": 4,
117 | "nbformat_minor": 2
118 | }
119 |
--------------------------------------------------------------------------------
/Section 5/y.csv:
--------------------------------------------------------------------------------
1 | Creditability
2 | 1
3 | 1
4 | 1
5 | 1
6 | 1
7 | 1
8 | 1
9 | 1
10 | 1
11 | 1
12 | 1
13 | 1
14 | 1
15 | 1
16 | 1
17 | 1
18 | 1
19 | 1
20 | 1
21 | 1
22 | 1
23 | 1
24 | 0
25 | 1
26 | 1
27 | 1
28 | 1
29 | 1
30 | 1
31 | 1
32 | 1
33 | 1
34 | 1
35 | 1
36 | 1
37 | 1
38 | 1
39 | 1
40 | 1
41 | 1
42 | 1
43 | 1
44 | 1
45 | 1
46 | 1
47 | 0
48 | 1
49 | 0
50 | 1
51 | 1
52 | 1
53 | 1
54 | 1
55 | 1
56 | 1
57 | 1
58 | 1
59 | 1
60 | 1
61 | 1
62 | 1
63 | 1
64 | 1
65 | 1
66 | 1
67 | 1
68 | 1
69 | 1
70 | 1
71 | 1
72 | 1
73 | 1
74 | 1
75 | 1
76 | 1
77 | 1
78 | 1
79 | 1
80 | 1
81 | 1
82 | 1
83 | 1
84 | 1
85 | 1
86 | 1
87 | 1
88 | 1
89 | 1
90 | 1
91 | 1
92 | 1
93 | 1
94 | 1
95 | 1
96 | 1
97 | 1
98 | 1
99 | 0
100 | 1
101 | 1
102 | 1
103 | 1
104 | 1
105 | 1
106 | 1
107 | 1
108 | 1
109 | 1
110 | 1
111 | 1
112 | 0
113 | 1
114 | 1
115 | 1
116 | 1
117 | 1
118 | 1
119 | 1
120 | 1
121 | 1
122 | 1
123 | 1
124 | 1
125 | 1
126 | 1
127 | 1
128 | 1
129 | 1
130 | 1
131 | 1
132 | 1
133 | 1
134 | 1
135 | 1
136 | 1
137 | 1
138 | 1
139 | 1
140 | 1
141 | 1
142 | 1
143 | 1
144 | 1
145 | 1
146 | 1
147 | 1
148 | 1
149 | 1
150 | 1
151 | 1
152 | 1
153 | 1
154 | 1
155 | 1
156 | 1
157 | 1
158 | 1
159 | 1
160 | 0
161 | 1
162 | 1
163 | 1
164 | 1
165 | 1
166 | 1
167 | 1
168 | 1
169 | 1
170 | 1
171 | 1
172 | 1
173 | 0
174 | 1
175 | 1
176 | 1
177 | 1
178 | 1
179 | 1
180 | 1
181 | 1
182 | 1
183 | 1
184 | 1
185 | 1
186 | 1
187 | 1
188 | 1
189 | 1
190 | 1
191 | 1
192 | 1
193 | 1
194 | 1
195 | 1
196 | 1
197 | 1
198 | 1
199 | 1
200 | 1
201 | 1
202 | 1
203 | 1
204 | 1
205 | 1
206 | 1
207 | 1
208 | 1
209 | 1
210 | 1
211 | 1
212 | 1
213 | 1
214 | 1
215 | 1
216 | 1
217 | 1
218 | 1
219 | 1
220 | 1
221 | 1
222 | 1
223 | 1
224 | 1
225 | 1
226 | 1
227 | 1
228 | 1
229 | 1
230 | 1
231 | 1
232 | 1
233 | 1
234 | 1
235 | 1
236 | 0
237 | 1
238 | 1
239 | 1
240 | 1
241 | 1
242 | 1
243 | 1
244 | 1
245 | 1
246 | 1
247 | 1
248 | 1
249 | 1
250 | 1
251 | 1
252 | 1
253 | 1
254 | 1
255 | 1
256 | 1
257 | 1
258 | 1
259 | 1
260 | 1
261 | 1
262 | 1
263 | 1
264 | 1
265 | 1
266 | 1
267 | 1
268 | 1
269 | 1
270 | 1
271 | 1
272 | 1
273 | 1
274 | 1
275 | 1
276 | 1
277 | 1
278 | 1
279 | 1
280 | 1
281 | 1
282 | 1
283 | 1
284 | 1
285 | 1
286 | 1
287 | 1
288 | 1
289 | 1
290 | 1
291 | 1
292 | 1
293 | 1
294 | 1
295 | 1
296 | 1
297 | 1
298 | 1
299 | 1
300 | 1
301 | 1
302 | 1
303 | 1
304 | 1
305 | 1
306 | 0
307 | 1
308 | 1
309 | 1
310 | 1
311 | 1
312 | 1
313 | 1
314 | 1
315 | 1
316 | 1
317 | 1
318 | 1
319 | 1
320 | 1
321 | 1
322 | 1
323 | 1
324 | 1
325 | 1
326 | 1
327 | 1
328 | 1
329 | 1
330 | 1
331 | 1
332 | 1
333 | 1
334 | 1
335 | 1
336 | 1
337 | 1
338 | 1
339 | 1
340 | 1
341 | 1
342 | 1
343 | 1
344 | 1
345 | 1
346 | 1
347 | 1
348 | 1
349 | 1
350 | 1
351 | 1
352 | 1
353 | 1
354 | 1
355 | 0
356 | 1
357 | 1
358 | 1
359 | 1
360 | 1
361 | 1
362 | 1
363 | 1
364 | 1
365 | 1
366 | 1
367 | 1
368 | 1
369 | 1
370 | 1
371 | 1
372 | 1
373 | 1
374 | 1
375 | 1
376 | 1
377 | 1
378 | 0
379 | 1
380 | 1
381 | 1
382 | 1
383 | 1
384 | 1
385 | 1
386 | 1
387 | 1
388 | 0
389 | 1
390 | 1
391 | 1
392 | 1
393 | 1
394 | 1
395 | 1
396 | 1
397 | 1
398 | 1
399 | 1
400 | 1
401 | 1
402 | 1
403 | 1
404 | 1
405 | 1
406 | 1
407 | 1
408 | 1
409 | 1
410 | 1
411 | 1
412 | 1
413 | 1
414 | 1
415 | 1
416 | 1
417 | 1
418 | 1
419 | 1
420 | 1
421 | 1
422 | 1
423 | 1
424 | 1
425 | 1
426 | 1
427 | 1
428 | 1
429 | 0
430 | 1
431 | 1
432 | 1
433 | 0
434 | 1
435 | 1
436 | 1
437 | 1
438 | 1
439 | 1
440 | 1
441 | 1
442 | 1
443 | 1
444 | 1
445 | 1
446 | 1
447 | 1
448 | 1
449 | 1
450 | 1
451 | 1
452 | 0
453 | 1
454 | 1
455 | 1
456 | 1
457 | 1
458 | 1
459 | 1
460 | 1
461 | 1
462 | 1
463 | 1
464 | 1
465 | 0
466 | 1
467 | 1
468 | 1
469 | 1
470 | 1
471 | 1
472 | 1
473 | 1
474 | 1
475 | 1
476 | 1
477 | 1
478 | 1
479 | 1
480 | 1
481 | 1
482 | 1
483 | 1
484 | 1
485 | 1
486 | 1
487 | 1
488 | 1
489 | 1
490 | 1
491 | 1
492 | 1
493 | 1
494 | 1
495 | 1
496 | 1
497 | 1
498 | 1
499 | 1
500 | 1
501 | 1
502 | 1
503 | 1
504 | 1
505 | 1
506 | 1
507 | 1
508 | 1
509 | 1
510 | 1
511 | 1
512 | 1
513 | 1
514 | 1
515 | 1
516 | 1
517 | 1
518 | 1
519 | 0
520 | 0
521 | 0
522 | 0
523 | 0
524 | 1
525 | 1
526 | 1
527 | 1
528 | 1
529 | 1
530 | 1
531 | 1
532 | 1
533 | 1
534 | 1
535 | 1
536 | 1
537 | 1
538 | 1
539 | 1
540 | 1
541 | 1
542 | 0
543 | 1
544 | 0
545 | 1
546 | 1
547 | 1
548 | 1
549 | 1
550 | 1
551 | 1
552 | 1
553 | 1
554 | 1
555 | 1
556 | 1
557 | 1
558 | 1
559 | 1
560 | 1
561 | 1
562 | 1
563 | 1
564 | 0
565 | 1
566 | 1
567 | 1
568 | 1
569 | 1
570 | 1
571 | 1
572 | 1
573 | 1
574 | 1
575 | 1
576 | 1
577 | 1
578 | 0
579 | 0
580 | 0
581 | 0
582 | 0
583 | 0
584 | 0
585 | 0
586 | 0
587 | 1
588 | 1
589 | 1
590 | 1
591 | 1
592 | 1
593 | 1
594 | 0
595 | 0
596 | 1
597 | 1
598 | 1
599 | 1
600 | 1
601 | 0
602 | 0
603 | 1
604 | 1
605 | 1
606 | 0
607 | 1
608 | 1
609 | 1
610 | 1
611 | 1
612 | 0
613 | 1
614 | 1
615 | 1
616 | 1
617 | 1
618 | 1
619 | 1
620 | 0
621 | 1
622 | 1
623 | 1
624 | 1
625 | 1
626 | 1
627 | 1
628 | 1
629 | 1
630 | 1
631 | 1
632 | 1
633 | 1
634 | 1
635 | 1
636 | 1
637 | 1
638 | 0
639 | 1
640 | 1
641 | 1
642 | 1
643 | 1
644 | 1
645 | 0
646 | 1
647 | 1
648 | 1
649 | 1
650 | 1
651 | 1
652 | 1
653 | 1
654 | 1
655 | 1
656 | 1
657 | 1
658 | 0
659 | 1
660 | 1
661 | 1
662 | 1
663 | 1
664 | 1
665 | 1
666 | 1
667 | 1
668 | 1
669 | 1
670 | 1
671 | 1
672 | 1
673 | 1
674 | 1
675 | 1
676 | 1
677 | 1
678 | 1
679 | 1
680 | 1
681 | 1
682 | 1
683 | 1
684 | 1
685 | 1
686 | 1
687 | 1
688 | 1
689 | 1
690 | 1
691 | 0
692 | 1
693 | 1
694 | 1
695 | 1
696 | 1
697 | 1
698 | 1
699 | 1
700 | 1
701 | 1
702 | 0
703 | 1
704 | 1
705 | 1
706 | 1
707 | 1
708 | 1
709 | 1
710 | 0
711 | 1
712 | 1
713 | 1
714 | 1
715 | 1
716 | 1
717 | 1
718 | 1
719 | 0
720 | 0
721 | 0
722 | 0
723 | 1
724 | 1
725 | 1
726 | 1
727 | 1
728 | 1
729 | 1
730 | 1
731 | 1
732 | 1
733 | 1
734 | 1
735 | 1
736 | 1
737 | 1
738 | 1
739 | 1
740 | 1
741 | 1
742 | 1
743 | 1
744 | 0
745 | 1
746 | 1
747 | 1
748 | 1
749 | 1
750 | 1
751 | 1
752 | 1
753 | 0
754 | 0
755 | 0
756 | 0
757 | 0
758 | 0
759 | 0
760 | 0
761 | 0
762 | 0
763 | 0
764 | 0
765 | 0
766 | 0
767 | 0
768 | 0
769 | 0
770 | 0
771 | 0
772 | 0
773 | 0
774 | 0
775 | 0
776 | 0
777 | 0
778 | 0
779 | 0
780 | 0
781 | 0
782 | 0
783 | 0
784 | 0
785 | 0
786 | 0
787 | 0
788 | 0
789 | 0
790 | 0
791 | 0
792 | 0
793 | 0
794 | 0
795 | 0
796 | 0
797 | 0
798 | 0
799 | 0
800 | 0
801 | 0
802 | 0
803 | 0
804 | 0
805 | 0
806 | 0
807 | 0
808 | 0
809 | 0
810 | 0
811 | 0
812 | 0
813 | 0
814 | 0
815 | 0
816 | 0
817 | 0
818 | 0
819 | 0
820 | 0
821 | 0
822 | 0
823 | 0
824 | 0
825 | 0
826 | 0
827 | 0
828 | 0
829 | 0
830 | 0
831 | 0
832 | 0
833 | 0
834 | 0
835 | 0
836 | 0
837 | 0
838 | 0
839 | 0
840 | 0
841 | 0
842 | 0
843 | 0
844 | 0
845 | 0
846 | 0
847 | 0
848 | 0
849 | 0
850 | 0
851 | 0
852 | 0
853 | 0
854 | 0
855 | 0
856 | 0
857 | 0
858 | 0
859 | 0
860 | 0
861 | 0
862 | 0
863 | 0
864 | 0
865 | 0
866 | 0
867 | 0
868 | 0
869 | 0
870 | 0
871 | 0
872 | 0
873 | 0
874 | 0
875 | 0
876 | 0
877 | 0
878 | 0
879 | 0
880 | 0
881 | 0
882 | 0
883 | 0
884 | 0
885 | 0
886 | 0
887 | 0
888 | 0
889 | 0
890 | 0
891 | 0
892 | 0
893 | 0
894 | 0
895 | 0
896 | 0
897 | 0
898 | 0
899 | 0
900 | 0
901 | 0
902 | 0
903 | 0
904 | 0
905 | 0
906 | 0
907 | 0
908 | 0
909 | 0
910 | 0
911 | 0
912 | 0
913 | 0
914 | 0
915 | 0
916 | 0
917 | 0
918 | 0
919 | 0
920 | 0
921 | 0
922 | 0
923 | 0
924 | 0
925 | 0
926 | 0
927 | 0
928 | 0
929 | 0
930 | 0
931 | 0
932 | 0
933 | 0
934 | 0
935 | 0
936 | 0
937 | 0
938 | 0
939 | 0
940 | 0
941 | 0
942 | 0
943 | 0
944 | 0
945 | 0
946 | 0
947 | 0
948 | 0
949 | 0
950 | 0
951 | 0
952 | 0
953 | 0
954 | 0
955 | 0
956 | 0
957 | 0
958 | 0
959 | 0
960 | 0
961 | 0
962 | 0
963 | 0
964 | 0
965 | 0
966 | 0
967 | 0
968 | 0
969 | 0
970 | 0
971 | 0
972 | 0
973 | 0
974 | 0
975 | 0
976 | 0
977 | 0
978 | 0
979 | 0
980 | 0
981 | 0
982 | 0
983 | 0
984 | 0
985 | 0
986 | 0
987 | 0
988 | 0
989 | 0
990 | 0
991 | 0
992 | 0
993 | 0
994 | 0
995 | 0
996 | 0
997 | 0
998 | 0
999 | 0
1000 | 0
1001 | 0
1002 |
--------------------------------------------------------------------------------