File size: 34,240 Bytes
0ab7b0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# import seaborn as sns\n",
    "# import matplotlib.pyplot as plt\n",
    "import os\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.feature_selection import SelectFromModel\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "# plt.style.use('seaborn-colorblind')\n",
    "# %matplotlib inline\n",
    "from sklearn.feature_selection import RFE\n",
    "from feature_selection import hybrid\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_breast_cancer\n",
    "data = load_breast_cancer()\n",
    "data = pd.DataFrame(np.c_[data['data'], data['target']],\n",
    "                  columns= np.append(data['feature_names'], ['target']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mean radius</th>\n",
       "      <th>mean texture</th>\n",
       "      <th>mean perimeter</th>\n",
       "      <th>mean area</th>\n",
       "      <th>mean smoothness</th>\n",
       "      <th>mean compactness</th>\n",
       "      <th>mean concavity</th>\n",
       "      <th>mean concave points</th>\n",
       "      <th>mean symmetry</th>\n",
       "      <th>mean fractal dimension</th>\n",
       "      <th>...</th>\n",
       "      <th>worst texture</th>\n",
       "      <th>worst perimeter</th>\n",
       "      <th>worst area</th>\n",
       "      <th>worst smoothness</th>\n",
       "      <th>worst compactness</th>\n",
       "      <th>worst concavity</th>\n",
       "      <th>worst concave points</th>\n",
       "      <th>worst symmetry</th>\n",
       "      <th>worst fractal dimension</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>17.99</td>\n",
       "      <td>10.38</td>\n",
       "      <td>122.80</td>\n",
       "      <td>1001.0</td>\n",
       "      <td>0.11840</td>\n",
       "      <td>0.27760</td>\n",
       "      <td>0.3001</td>\n",
       "      <td>0.14710</td>\n",
       "      <td>0.2419</td>\n",
       "      <td>0.07871</td>\n",
       "      <td>...</td>\n",
       "      <td>17.33</td>\n",
       "      <td>184.60</td>\n",
       "      <td>2019.0</td>\n",
       "      <td>0.1622</td>\n",
       "      <td>0.6656</td>\n",
       "      <td>0.7119</td>\n",
       "      <td>0.2654</td>\n",
       "      <td>0.4601</td>\n",
       "      <td>0.11890</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20.57</td>\n",
       "      <td>17.77</td>\n",
       "      <td>132.90</td>\n",
       "      <td>1326.0</td>\n",
       "      <td>0.08474</td>\n",
       "      <td>0.07864</td>\n",
       "      <td>0.0869</td>\n",
       "      <td>0.07017</td>\n",
       "      <td>0.1812</td>\n",
       "      <td>0.05667</td>\n",
       "      <td>...</td>\n",
       "      <td>23.41</td>\n",
       "      <td>158.80</td>\n",
       "      <td>1956.0</td>\n",
       "      <td>0.1238</td>\n",
       "      <td>0.1866</td>\n",
       "      <td>0.2416</td>\n",
       "      <td>0.1860</td>\n",
       "      <td>0.2750</td>\n",
       "      <td>0.08902</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>19.69</td>\n",
       "      <td>21.25</td>\n",
       "      <td>130.00</td>\n",
       "      <td>1203.0</td>\n",
       "      <td>0.10960</td>\n",
       "      <td>0.15990</td>\n",
       "      <td>0.1974</td>\n",
       "      <td>0.12790</td>\n",
       "      <td>0.2069</td>\n",
       "      <td>0.05999</td>\n",
       "      <td>...</td>\n",
       "      <td>25.53</td>\n",
       "      <td>152.50</td>\n",
       "      <td>1709.0</td>\n",
       "      <td>0.1444</td>\n",
       "      <td>0.4245</td>\n",
       "      <td>0.4504</td>\n",
       "      <td>0.2430</td>\n",
       "      <td>0.3613</td>\n",
       "      <td>0.08758</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11.42</td>\n",
       "      <td>20.38</td>\n",
       "      <td>77.58</td>\n",
       "      <td>386.1</td>\n",
       "      <td>0.14250</td>\n",
       "      <td>0.28390</td>\n",
       "      <td>0.2414</td>\n",
       "      <td>0.10520</td>\n",
       "      <td>0.2597</td>\n",
       "      <td>0.09744</td>\n",
       "      <td>...</td>\n",
       "      <td>26.50</td>\n",
       "      <td>98.87</td>\n",
       "      <td>567.7</td>\n",
       "      <td>0.2098</td>\n",
       "      <td>0.8663</td>\n",
       "      <td>0.6869</td>\n",
       "      <td>0.2575</td>\n",
       "      <td>0.6638</td>\n",
       "      <td>0.17300</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>20.29</td>\n",
       "      <td>14.34</td>\n",
       "      <td>135.10</td>\n",
       "      <td>1297.0</td>\n",
       "      <td>0.10030</td>\n",
       "      <td>0.13280</td>\n",
       "      <td>0.1980</td>\n",
       "      <td>0.10430</td>\n",
       "      <td>0.1809</td>\n",
       "      <td>0.05883</td>\n",
       "      <td>...</td>\n",
       "      <td>16.67</td>\n",
       "      <td>152.20</td>\n",
       "      <td>1575.0</td>\n",
       "      <td>0.1374</td>\n",
       "      <td>0.2050</td>\n",
       "      <td>0.4000</td>\n",
       "      <td>0.1625</td>\n",
       "      <td>0.2364</td>\n",
       "      <td>0.07678</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   mean radius  mean texture  mean perimeter  mean area  mean smoothness  \\\n",
       "0        17.99         10.38          122.80     1001.0          0.11840   \n",
       "1        20.57         17.77          132.90     1326.0          0.08474   \n",
       "2        19.69         21.25          130.00     1203.0          0.10960   \n",
       "3        11.42         20.38           77.58      386.1          0.14250   \n",
       "4        20.29         14.34          135.10     1297.0          0.10030   \n",
       "\n",
       "   mean compactness  mean concavity  mean concave points  mean symmetry  \\\n",
       "0           0.27760          0.3001              0.14710         0.2419   \n",
       "1           0.07864          0.0869              0.07017         0.1812   \n",
       "2           0.15990          0.1974              0.12790         0.2069   \n",
       "3           0.28390          0.2414              0.10520         0.2597   \n",
       "4           0.13280          0.1980              0.10430         0.1809   \n",
       "\n",
       "   mean fractal dimension   ...    worst texture  worst perimeter  worst area  \\\n",
       "0                 0.07871   ...            17.33           184.60      2019.0   \n",
       "1                 0.05667   ...            23.41           158.80      1956.0   \n",
       "2                 0.05999   ...            25.53           152.50      1709.0   \n",
       "3                 0.09744   ...            26.50            98.87       567.7   \n",
       "4                 0.05883   ...            16.67           152.20      1575.0   \n",
       "\n",
       "   worst smoothness  worst compactness  worst concavity  worst concave points  \\\n",
       "0            0.1622             0.6656           0.7119                0.2654   \n",
       "1            0.1238             0.1866           0.2416                0.1860   \n",
       "2            0.1444             0.4245           0.4504                0.2430   \n",
       "3            0.2098             0.8663           0.6869                0.2575   \n",
       "4            0.1374             0.2050           0.4000                0.1625   \n",
       "\n",
       "   worst symmetry  worst fractal dimension  target  \n",
       "0          0.4601                  0.11890     0.0  \n",
       "1          0.2750                  0.08902     0.0  \n",
       "2          0.3613                  0.08758     0.0  \n",
       "3          0.6638                  0.17300     0.0  \n",
       "4          0.2364                  0.07678     0.0  \n",
       "\n",
       "[5 rows x 31 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((455, 30), (114, 30))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(data.drop(labels=['target'], axis=1), \n",
    "                                                    data.target, test_size=0.2,\n",
    "                                                    random_state=0)\n",
    "X_train.shape, X_test.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  Recursive Feature Elimination \n",
    "### with Random Forests Importance\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example 1\n",
    "This method is slightly **different from the guide**, as it use a different stopping criterion: the desired number of features to select is eventually reached."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RFE(estimator=RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
       "            max_depth=None, max_features='auto', max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=1, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, n_estimators=20, n_jobs=None,\n",
       "            oob_score=False, random_state=None, verbose=0,\n",
       "            warm_start=False),\n",
       "  n_features_to_select=10, step=1, verbose=0)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  n_features_to_select decide the stopping criterion\n",
    "# we stop till 10 features remaining\n",
    "\n",
    "sel_ = RFE(RandomForestClassifier(n_estimators=20), n_features_to_select=10)\n",
    "sel_.fit(X_train.fillna(0), y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['mean texture', 'mean perimeter', 'mean area', 'mean concavity',\n",
      "       'mean concave points', 'worst radius', 'worst perimeter', 'worst area',\n",
      "       'worst concave points', 'worst symmetry'],\n",
      "      dtype='object')\n"
     ]
    }
   ],
   "source": [
    "selected_feat = X_train.columns[(sel_.get_support())]\n",
    "print(selected_feat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Example 2\n",
    "recursive feature elimination with RandomForest\n",
    "with the method same as the guide\n",
    "1. Rank the features according to their importance derived from a machine learning algorithm: it can be tree importance, or LASSO / Ridge,  or the linear / logistic regression coefficients.\n",
    "2. Remove one feature -the least important- and build a machine learning algorithm utilizing the remaining features.\n",
    "3. Calculate a performance metric of your choice: roc-auc, mse, rmse, accuracy.\n",
    "4. If the metric decreases by more of an arbitrarily set threshold, then that feature is important and should be kept. Otherwise, we can remove that feature.\n",
    "5. Repeat steps 2-4 until all features have been removed (and therefore evaluated) and the drop in performance assessed.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "testing feature:  mean radius  which is feature  1  out of  30\n",
      "New Test ROC AUC=0.9941251190854239\n",
      "All features Test ROC AUC=0.9968243886948238\n",
      "Drop in ROC AUC=0.0026992696093999236\n",
      "keep:  mean radius\n",
      "\n",
      "testing feature:  mean texture  which is feature  2  out of  30\n",
      "New Test ROC AUC=0.9936487773896475\n",
      "All features Test ROC AUC=0.9968243886948238\n",
      "Drop in ROC AUC=0.0031756113051762958\n",
      "keep:  mean texture\n",
      "\n",
      "testing feature:  mean perimeter  which is feature  3  out of  30\n",
      "New Test ROC AUC=0.9968243886948238\n",
      "All features Test ROC AUC=0.9968243886948238\n",
      "Drop in ROC AUC=0.0\n",
      "remove:  mean perimeter\n",
      "\n",
      "testing feature:  mean area  which is feature  4  out of  30\n",
      "New Test ROC AUC=0.9960304858685297\n",
      "All features Test ROC AUC=0.9968243886948238\n",
      "Drop in ROC AUC=0.0007939028262941017\n",
      "remove:  mean area\n",
      "\n",
      "testing feature:  mean smoothness  which is feature  5  out of  30\n",
      "New Test ROC AUC=0.9965068275643061\n",
      "All features Test ROC AUC=0.9960304858685297\n",
      "Drop in ROC AUC=-0.0004763416957763722\n",
      "remove:  mean smoothness\n",
      "\n",
      "testing feature:  mean compactness  which is feature  6  out of  30\n",
      "New Test ROC AUC=0.9942838996506828\n",
      "All features Test ROC AUC=0.9965068275643061\n",
      "Drop in ROC AUC=0.0022229279136233293\n",
      "keep:  mean compactness\n",
      "\n",
      "testing feature:  mean concavity  which is feature  7  out of  30\n",
      "New Test ROC AUC=0.9957129247380121\n",
      "All features Test ROC AUC=0.9965068275643061\n",
      "Drop in ROC AUC=0.0007939028262939907\n",
      "remove:  mean concavity\n",
      "\n",
      "testing feature:  mean concave points  which is feature  8  out of  30\n",
      "New Test ROC AUC=0.9976182915211178\n",
      "All features Test ROC AUC=0.9957129247380121\n",
      "Drop in ROC AUC=-0.0019053667831057108\n",
      "remove:  mean concave points\n",
      "\n",
      "testing feature:  mean symmetry  which is feature  9  out of  30\n",
      "New Test ROC AUC=0.9953953636074945\n",
      "All features Test ROC AUC=0.9976182915211178\n",
      "Drop in ROC AUC=0.0022229279136233293\n",
      "keep:  mean symmetry\n",
      "\n",
      "testing feature:  mean fractal dimension  which is feature  10  out of  30\n",
      "New Test ROC AUC=0.9949190219117181\n",
      "All features Test ROC AUC=0.9976182915211178\n",
      "Drop in ROC AUC=0.0026992696093997015\n",
      "keep:  mean fractal dimension\n",
      "\n",
      "testing feature:  radius error  which is feature  11  out of  30\n",
      "New Test ROC AUC=0.9952365830422356\n",
      "All features Test ROC AUC=0.9976182915211178\n",
      "Drop in ROC AUC=0.002381708478882194\n",
      "keep:  radius error\n",
      "\n",
      "testing feature:  texture error  which is feature  12  out of  30\n",
      "New Test ROC AUC=0.9952365830422356\n",
      "All features Test ROC AUC=0.9976182915211178\n",
      "Drop in ROC AUC=0.002381708478882194\n",
      "keep:  texture error\n",
      "\n",
      "testing feature:  perimeter error  which is feature  13  out of  30\n",
      "New Test ROC AUC=0.9939663385201651\n",
      "All features Test ROC AUC=0.9976182915211178\n",
      "Drop in ROC AUC=0.003651953000952668\n",
      "keep:  perimeter error\n",
      "\n",
      "testing feature:  area error  which is feature  14  out of  30\n",
      "New Test ROC AUC=0.994919021911718\n",
      "All features Test ROC AUC=0.9976182915211178\n",
      "Drop in ROC AUC=0.0026992696093998125\n",
      "keep:  area error\n",
      "\n",
      "testing feature:  smoothness error  which is feature  15  out of  30\n",
      "New Test ROC AUC=0.995871705303271\n",
      "All features Test ROC AUC=0.9976182915211178\n",
      "Drop in ROC AUC=0.001746586217846846\n",
      "keep:  smoothness error\n",
      "\n",
      "testing feature:  compactness error  which is feature  16  out of  30\n",
      "New Test ROC AUC=0.9958717053032708\n",
      "All features Test ROC AUC=0.9976182915211178\n",
      "Drop in ROC AUC=0.0017465862178469571\n",
      "keep:  compactness error\n",
      "\n",
      "testing feature:  concavity error  which is feature  17  out of  30\n",
      "New Test ROC AUC=0.9961892664337886\n",
      "All features Test ROC AUC=0.9976182915211178\n",
      "Drop in ROC AUC=0.0014290250873292276\n",
      "keep:  concavity error\n",
      "\n",
      "testing feature:  concave points error  which is feature  18  out of  30\n",
      "New Test ROC AUC=0.9961892664337885\n",
      "All features Test ROC AUC=0.9976182915211178\n",
      "Drop in ROC AUC=0.0014290250873293386\n",
      "keep:  concave points error\n",
      "\n",
      "testing feature:  symmetry error  which is feature  19  out of  30\n",
      "New Test ROC AUC=0.9968243886948238\n",
      "All features Test ROC AUC=0.9976182915211178\n",
      "Drop in ROC AUC=0.0007939028262939907\n",
      "remove:  symmetry error\n",
      "\n",
      "testing feature:  fractal dimension error  which is feature  20  out of  30\n",
      "New Test ROC AUC=0.9946014607812005\n",
      "All features Test ROC AUC=0.9968243886948238\n",
      "Drop in ROC AUC=0.0022229279136233293\n",
      "keep:  fractal dimension error\n",
      "\n",
      "testing feature:  worst radius  which is feature  21  out of  30\n",
      "New Test ROC AUC=0.9955541441727532\n",
      "All features Test ROC AUC=0.9968243886948238\n",
      "Drop in ROC AUC=0.001270244522070585\n",
      "keep:  worst radius\n",
      "\n",
      "testing feature:  worst texture  which is feature  22  out of  30\n",
      "New Test ROC AUC=0.9958717053032708\n",
      "All features Test ROC AUC=0.9968243886948238\n",
      "Drop in ROC AUC=0.0009526833915529664\n",
      "remove:  worst texture\n",
      "\n",
      "testing feature:  worst perimeter  which is feature  23  out of  30\n",
      "New Test ROC AUC=0.995871705303271\n",
      "All features Test ROC AUC=0.9958717053032708\n",
      "Drop in ROC AUC=-1.1102230246251565e-16\n",
      "remove:  worst perimeter\n",
      "\n",
      "testing feature:  worst area  which is feature  24  out of  30\n",
      "New Test ROC AUC=0.9938075579549063\n",
      "All features Test ROC AUC=0.995871705303271\n",
      "Drop in ROC AUC=0.0020641473483646866\n",
      "keep:  worst area\n",
      "\n",
      "testing feature:  worst smoothness  which is feature  25  out of  30\n",
      "New Test ROC AUC=0.9939663385201651\n",
      "All features Test ROC AUC=0.995871705303271\n",
      "Drop in ROC AUC=0.0019053667831058219\n",
      "keep:  worst smoothness\n",
      "\n",
      "testing feature:  worst compactness  which is feature  26  out of  30\n",
      "New Test ROC AUC=0.9960304858685296\n",
      "All features Test ROC AUC=0.995871705303271\n",
      "Drop in ROC AUC=-0.0001587805652586427\n",
      "remove:  worst compactness\n",
      "\n",
      "testing feature:  worst concavity  which is feature  27  out of  30\n",
      "New Test ROC AUC=0.9966656081295648\n",
      "All features Test ROC AUC=0.9960304858685296\n",
      "Drop in ROC AUC=-0.0006351222610352369\n",
      "remove:  worst concavity\n",
      "\n",
      "testing feature:  worst concave points  which is feature  28  out of  30\n",
      "New Test ROC AUC=0.9936487773896475\n",
      "All features Test ROC AUC=0.9966656081295648\n",
      "Drop in ROC AUC=0.00301683073991732\n",
      "keep:  worst concave points\n",
      "\n",
      "testing feature:  worst symmetry  which is feature  29  out of  30\n",
      "New Test ROC AUC=0.9976182915211178\n",
      "All features Test ROC AUC=0.9966656081295648\n",
      "Drop in ROC AUC=-0.0009526833915529664\n",
      "remove:  worst symmetry\n",
      "\n",
      "testing feature:  worst fractal dimension  which is feature  30  out of  30\n",
      "New Test ROC AUC=0.9973007303906002\n",
      "All features Test ROC AUC=0.9976182915211178\n",
      "Drop in ROC AUC=0.00031756113051761847\n",
      "remove:  worst fractal dimension\n",
      "DONE!!\n",
      "total features to remove:  12\n",
      "total features to keep:  18\n"
     ]
    }
   ],
   "source": [
    "# tol decide whether we should drop or keep the feature in current round\n",
    "features_to_keep = hybrid.recursive_feature_elimination_rf(X_train=X_train,\n",
    "                                                           y_train=y_train,\n",
    "                                                           X_test=X_test,\n",
    "                                                           y_test=y_test,\n",
    "                                                           tol=0.001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['mean radius',\n",
       " 'mean texture',\n",
       " 'mean compactness',\n",
       " 'mean symmetry',\n",
       " 'mean fractal dimension',\n",
       " 'radius error',\n",
       " 'texture error',\n",
       " 'perimeter error',\n",
       " 'area error',\n",
       " 'smoothness error',\n",
       " 'compactness error',\n",
       " 'concavity error',\n",
       " 'concave points error',\n",
       " 'fractal dimension error',\n",
       " 'worst radius',\n",
       " 'worst area',\n",
       " 'worst smoothness',\n",
       " 'worst concave points']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features_to_keep"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Recursive Feature Addition\n",
    "### with Random Forests Importance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example 1\n",
    "recursive feature addition with RandomForest\n",
    "with the method same as the guide\n",
    "1. Rank the features according to their importance derived from a  machine learning algorithm: it can be tree importance, or LASSO / Ridge,  or the linear / logistic regression coefficients.\n",
    "2. Build a machine learning model with only 1 feature, the most important one, and calculate the model metric for performance.\n",
    "3. Add one feature -the most important- and build a machine learning  algorithm utilizing the added and any feature from previous rounds.\n",
    "4. Calculate a performance metric of your choice: roc-auc, mse, rmse, accuracy.\n",
    "5. If the metric increases by more than an arbitrarily set threshold,  then that feature is important and should be kept. Otherwise, we can  remove that feature.\n",
    "6. Repeat steps 2-5 until all features have been removed (and therefore evaluated) and the drop in performance assessed.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "testing feature:  mean texture  which is feature  1  out of  30\n",
      "New Test ROC AUC=0.9558590028580501\n",
      "All features Test ROC AUC=0.9009209272785013\n",
      "Increase in ROC AUC=0.054938075579548884\n",
      "keep:  mean texture\n",
      "\n",
      "testing feature:  mean perimeter  which is feature  2  out of  30\n",
      "New Test ROC AUC=0.9609399809463322\n",
      "All features Test ROC AUC=0.9558590028580501\n",
      "Increase in ROC AUC=0.005080978088282007\n",
      "keep:  mean perimeter\n",
      "\n",
      "testing feature:  mean area  which is feature  3  out of  30\n",
      "New Test ROC AUC=0.9609399809463322\n",
      "All features Test ROC AUC=0.9609399809463322\n",
      "Increase in ROC AUC=0.0\n",
      "remove:  mean area\n",
      "\n",
      "testing feature:  mean smoothness  which is feature  4  out of  30\n",
      "New Test ROC AUC=0.9684026675134964\n",
      "All features Test ROC AUC=0.9609399809463322\n",
      "Increase in ROC AUC=0.007462686567164201\n",
      "keep:  mean smoothness\n",
      "\n",
      "testing feature:  mean compactness  which is feature  5  out of  30\n",
      "New Test ROC AUC=0.9750714512543665\n",
      "All features Test ROC AUC=0.9684026675134964\n",
      "Increase in ROC AUC=0.006668783740870099\n",
      "keep:  mean compactness\n",
      "\n",
      "testing feature:  mean concavity  which is feature  6  out of  30\n",
      "New Test ROC AUC=0.9933312162591298\n",
      "All features Test ROC AUC=0.9750714512543665\n",
      "Increase in ROC AUC=0.01825976500476334\n",
      "keep:  mean concavity\n",
      "\n",
      "testing feature:  mean concave points  which is feature  7  out of  30\n",
      "New Test ROC AUC=0.9925373134328358\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.0007939028262939907\n",
      "remove:  mean concave points\n",
      "\n",
      "testing feature:  mean symmetry  which is feature  8  out of  30\n",
      "New Test ROC AUC=0.9895204826929185\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.0038107335662113107\n",
      "remove:  mean symmetry\n",
      "\n",
      "testing feature:  mean fractal dimension  which is feature  9  out of  30\n",
      "New Test ROC AUC=0.9892029215624007\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.00412829469672904\n",
      "remove:  mean fractal dimension\n",
      "\n",
      "testing feature:  radius error  which is feature  10  out of  30\n",
      "New Test ROC AUC=0.9895204826929184\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.0038107335662114217\n",
      "remove:  radius error\n",
      "\n",
      "testing feature:  texture error  which is feature  11  out of  30\n",
      "New Test ROC AUC=0.9868212130835186\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.006510003175611234\n",
      "remove:  texture error\n",
      "\n",
      "testing feature:  perimeter error  which is feature  12  out of  30\n",
      "New Test ROC AUC=0.9890441409971419\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.004287075261987905\n",
      "remove:  perimeter error\n",
      "\n",
      "testing feature:  area error  which is feature  13  out of  30\n",
      "New Test ROC AUC=0.989044140997142\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.004287075261987794\n",
      "remove:  area error\n",
      "\n",
      "testing feature:  smoothness error  which is feature  14  out of  30\n",
      "New Test ROC AUC=0.988091457605589\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.00523975865354076\n",
      "remove:  smoothness error\n",
      "\n",
      "testing feature:  compactness error  which is feature  15  out of  30\n",
      "New Test ROC AUC=0.9895204826929184\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.0038107335662114217\n",
      "remove:  compactness error\n",
      "\n",
      "testing feature:  concavity error  which is feature  16  out of  30\n",
      "New Test ROC AUC=0.9911082883455065\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.0022229279136233293\n",
      "remove:  concavity error\n",
      "\n",
      "testing feature:  concave points error  which is feature  17  out of  30\n",
      "New Test ROC AUC=0.9906319466497301\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.0026992696093997015\n",
      "remove:  concave points error\n",
      "\n",
      "testing feature:  symmetry error  which is feature  18  out of  30\n",
      "New Test ROC AUC=0.9876151159098127\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.0057161003493171325\n",
      "remove:  symmetry error\n",
      "\n",
      "testing feature:  fractal dimension error  which is feature  19  out of  30\n",
      "New Test ROC AUC=0.9896792632581772\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.003651953000952557\n",
      "remove:  fractal dimension error\n",
      "\n",
      "testing feature:  worst radius  which is feature  20  out of  30\n",
      "New Test ROC AUC=0.994125119085424\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=0.0007939028262942127\n",
      "remove:  worst radius\n",
      "\n",
      "testing feature:  worst texture  which is feature  21  out of  30\n",
      "New Test ROC AUC=0.9906319466497301\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.0026992696093997015\n",
      "remove:  worst texture\n",
      "\n",
      "testing feature:  worst perimeter  which is feature  22  out of  30\n",
      "New Test ROC AUC=0.9933312162591299\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=1.1102230246251565e-16\n",
      "remove:  worst perimeter\n",
      "\n",
      "testing feature:  worst area  which is feature  23  out of  30\n",
      "New Test ROC AUC=0.9931724356938711\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.0001587805652586427\n",
      "remove:  worst area\n",
      "\n",
      "testing feature:  worst smoothness  which is feature  24  out of  30\n",
      "New Test ROC AUC=0.9933312162591299\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=1.1102230246251565e-16\n",
      "remove:  worst smoothness\n",
      "\n",
      "testing feature:  worst compactness  which is feature  25  out of  30\n",
      "New Test ROC AUC=0.9895204826929184\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=-0.0038107335662114217\n",
      "remove:  worst compactness\n",
      "\n",
      "testing feature:  worst concavity  which is feature  26  out of  30\n",
      "New Test ROC AUC=0.9938075579549063\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=0.0004763416957764832\n",
      "remove:  worst concavity\n",
      "\n",
      "testing feature:  worst concave points  which is feature  27  out of  30\n",
      "New Test ROC AUC=0.9971419498253413\n",
      "All features Test ROC AUC=0.9933312162591298\n",
      "Increase in ROC AUC=0.0038107335662115327\n",
      "keep:  worst concave points\n",
      "\n",
      "testing feature:  worst symmetry  which is feature  28  out of  30\n",
      "New Test ROC AUC=0.9957129247380121\n",
      "All features Test ROC AUC=0.9971419498253413\n",
      "Increase in ROC AUC=-0.0014290250873292276\n",
      "remove:  worst symmetry\n",
      "\n",
      "testing feature:  worst fractal dimension  which is feature  29  out of  30\n",
      "New Test ROC AUC=0.9950778024769769\n",
      "All features Test ROC AUC=0.9971419498253413\n",
      "Increase in ROC AUC=-0.0020641473483644646\n",
      "remove:  worst fractal dimension\n",
      "DONE!!\n",
      "total features to keep:  7\n"
     ]
    }
   ],
   "source": [
    "features_to_keep = hybrid.recursive_feature_addition_rf(X_train=X_train,\n",
    "                                                        y_train=y_train,\n",
    "                                                        X_test=X_test,\n",
    "                                                        y_test=y_test,\n",
    "                                                        tol=0.001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['mean radius',\n",
       " 'mean texture',\n",
       " 'mean perimeter',\n",
       " 'mean smoothness',\n",
       " 'mean compactness',\n",
       " 'mean concavity',\n",
       " 'worst concave points']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features_to_keep"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}