-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVM_Py.py
More file actions
2165 lines (2165 loc) · 305 KB
/
SVM_Py.py
File metadata and controls
2165 lines (2165 loc) · 305 KB
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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"cells": [
{
"cell_type": "markdown",
"id": "71320d9a",
"metadata": {},
"source": [
"# <font color= black>Support Vector Machine (SVM)</font>"
]
},
{
"cell_type": "markdown",
"id": "edf49eb3",
"metadata": {},
"source": [
"**<font color = blue>1. When to use SVM?</font>**"
]
},
{
"cell_type": "markdown",
"id": "f2f470e1",
"metadata": {},
"source": [
"The major limitation of K-NN (K nearest neighbour) is that it is difficult to categorize data when it is dense. \n",
"For instance, if the co-ordinates are closely spaced to each other, the machine finds it very difficult to categroize it\n",
"into one class.\n",
"This limitation of K-NN is then overcomed using SVM (i.e. Space Vector Machine) algorithm.\n",
"SVM is used for both classification & regression problems.\n",
"Primarily, SVM is used for binary & multi-class Classification.\n",
"\n",
"<font color = red>**SVM is suited when we have more number of columns than the number of rows.**</font>\n",
"Such a data is called high-dimensional data. It is the data where number of variables are high as compared to the number\n",
"of observations.\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "5c7ca110",
"metadata": {},
"source": [
"<font color = blue>**2. Linearly and Non-linearly separable data**</font>"
]
},
{
"cell_type": "markdown",
"id": "63891ce2",
"metadata": {},
"source": [
"Linearly separable data is the data where you can partition the data into classes simply by putting a line between them.\n",
"\n",
"However, there are scenarios where one cannot separate the data by simply putting a linear line. Part of the reason might\n",
"be due to it's non-linear nature, such data is called as Non-linearly separable data.\n",
"One example of a Non-linearly separable data is data in concentric patches.\n",
"\n",
"SVM works on both Linearly & Non-linearly separable data."
]
},
{
"cell_type": "markdown",
"id": "1de6ca8e",
"metadata": {},
"source": [
"**<font color = blue>3. What does one mean by Support Vector?</font>**"
]
},
{
"cell_type": "markdown",
"id": "4521ad1d",
"metadata": {},
"source": [
"**<font color = red>The datapoint of a class which is at the extreme ends of the class as well as the one which is closest to another class \n",
"is called as the Support Vector of that class.</font>**\n",
"Once we find the Support vectors we draw a hyperplane passing through both these co-ordinates and then draw a margin throughthe centre of this hyperplane, which we define as maximum margin.\n",
"\n",
"So in the case of a densely populated data, even if the data points are closely packed we could easily differentiate with\n",
"the help of this maximum margin. This makes the classification a lot simpler.\n"
]
},
{
"cell_type": "markdown",
"id": "f37af80f",
"metadata": {},
"source": [
"<img src = \"SVM_image.png\">"
]
},
{
"cell_type": "markdown",
"id": "ac1b09f7",
"metadata": {},
"source": [
"If we look at the above figure,it tells us clearly how support vectors can be used to create hyperplane & maximum\n",
"margin which can further classify the data better.\n",
"The dimensions of the hyperplane depends upon the features present in the dataset, which means if there are 2 features,\n",
"then the hyperplane will be a straight line, if there are 3 features then the hyperplane will be a 2 dimensional plane.\n",
"\n",
"We always create a hyperplane that has maximum margin, which means maximum distance between the data points."
]
},
{
"cell_type": "markdown",
"id": "7cc3f38e",
"metadata": {},
"source": [
"**<font color = blue>4. Hyperparameter</font>**"
]
},
{
"cell_type": "markdown",
"id": "9f655725",
"metadata": {},
"source": [
"In Machine Learning it is difficult to tune the algorithm based on different datasets. To make it impossible, we tune\n",
"the model using a hyperparameter, which is basically a user defined variable which can be used to tune the model as per\n",
"the different datasets, so that we achieve the desired accuracy for that model.\n",
"\n",
"In SVM, we have 3 hyperparameters alpha, beta & gamma."
]
},
{
"cell_type": "markdown",
"id": "fff74578",
"metadata": {},
"source": [
"------------------------------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "86248157",
"metadata": {},
"source": [
"# Code"
]
},
{
"cell_type": "markdown",
"id": "4af978c6",
"metadata": {},
"source": [
"### **<font color = blue>1. Importing the Libraries</font>**"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "618bc7d3",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"id": "92310d76",
"metadata": {},
"source": [
"### **<font color = blue>2. Reading the training & testing data</font>**"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "a92931d1",
"metadata": {},
"outputs": [],
"source": [
"train_data = pd.read_csv(r'risk_analytics_train.csv',index_col=0,header=0)\n",
"test_data = pd.read_csv(r'risk_analytics_test.csv',index_col=0,header=0)"
]
},
{
"cell_type": "markdown",
"id": "e14539e1",
"metadata": {},
"source": [
"### **<font color = blue>3. Preprocessing</font>**"
]
},
{
"cell_type": "markdown",
"id": "14d3b0c8",
"metadata": {},
"source": [
"#### **<font color = green>3.1 Checking the dimensions</font>**"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "60b29221",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(614, 12)\n",
"(367, 11)\n"
]
}
],
"source": [
"print(train_data.shape)\n",
"print(test_data.shape)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "b080ab9e",
"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>Gender</th>\n",
" <th>Married</th>\n",
" <th>Dependents</th>\n",
" <th>Education</th>\n",
" <th>Self_Employed</th>\n",
" <th>ApplicantIncome</th>\n",
" <th>CoapplicantIncome</th>\n",
" <th>LoanAmount</th>\n",
" <th>Loan_Amount_Term</th>\n",
" <th>Credit_History</th>\n",
" <th>Property_Area</th>\n",
" <th>Loan_Status</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Loan_ID</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>LP001002</th>\n",
" <td>Male</td>\n",
" <td>No</td>\n",
" <td>0.0</td>\n",
" <td>Graduate</td>\n",
" <td>No</td>\n",
" <td>5849</td>\n",
" <td>0.0</td>\n",
" <td>NaN</td>\n",
" <td>360.0</td>\n",
" <td>1.0</td>\n",
" <td>Urban</td>\n",
" <td>Y</td>\n",
" </tr>\n",
" <tr>\n",
" <th>LP001003</th>\n",
" <td>Male</td>\n",
" <td>Yes</td>\n",
" <td>1.0</td>\n",
" <td>Graduate</td>\n",
" <td>No</td>\n",
" <td>4583</td>\n",
" <td>1508.0</td>\n",
" <td>128.0</td>\n",
" <td>360.0</td>\n",
" <td>1.0</td>\n",
" <td>Rural</td>\n",
" <td>N</td>\n",
" </tr>\n",
" <tr>\n",
" <th>LP001005</th>\n",
" <td>Male</td>\n",
" <td>Yes</td>\n",
" <td>0.0</td>\n",
" <td>Graduate</td>\n",
" <td>Yes</td>\n",
" <td>3000</td>\n",
" <td>0.0</td>\n",
" <td>66.0</td>\n",
" <td>360.0</td>\n",
" <td>1.0</td>\n",
" <td>Urban</td>\n",
" <td>Y</td>\n",
" </tr>\n",
" <tr>\n",
" <th>LP001006</th>\n",
" <td>Male</td>\n",
" <td>Yes</td>\n",
" <td>0.0</td>\n",
" <td>Not Graduate</td>\n",
" <td>No</td>\n",
" <td>2583</td>\n",
" <td>2358.0</td>\n",
" <td>120.0</td>\n",
" <td>360.0</td>\n",
" <td>1.0</td>\n",
" <td>Urban</td>\n",
" <td>Y</td>\n",
" </tr>\n",
" <tr>\n",
" <th>LP001008</th>\n",
" <td>Male</td>\n",
" <td>No</td>\n",
" <td>0.0</td>\n",
" <td>Graduate</td>\n",
" <td>No</td>\n",
" <td>6000</td>\n",
" <td>0.0</td>\n",
" <td>141.0</td>\n",
" <td>360.0</td>\n",
" <td>1.0</td>\n",
" <td>Urban</td>\n",
" <td>Y</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Gender Married Dependents Education Self_Employed \\\n",
"Loan_ID \n",
"LP001002 Male No 0.0 Graduate No \n",
"LP001003 Male Yes 1.0 Graduate No \n",
"LP001005 Male Yes 0.0 Graduate Yes \n",
"LP001006 Male Yes 0.0 Not Graduate No \n",
"LP001008 Male No 0.0 Graduate No \n",
"\n",
" ApplicantIncome CoapplicantIncome LoanAmount Loan_Amount_Term \\\n",
"Loan_ID \n",
"LP001002 5849 0.0 NaN 360.0 \n",
"LP001003 4583 1508.0 128.0 360.0 \n",
"LP001005 3000 0.0 66.0 360.0 \n",
"LP001006 2583 2358.0 120.0 360.0 \n",
"LP001008 6000 0.0 141.0 360.0 \n",
"\n",
" Credit_History Property_Area Loan_Status \n",
"Loan_ID \n",
"LP001002 1.0 Urban Y \n",
"LP001003 1.0 Rural N \n",
"LP001005 1.0 Urban Y \n",
"LP001006 1.0 Urban Y \n",
"LP001008 1.0 Urban Y "
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Preview the train & test data\n",
"\n",
"train_data.head()"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "4e7aaaa0",
"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>Gender</th>\n",
" <th>Married</th>\n",
" <th>Dependents</th>\n",
" <th>Education</th>\n",
" <th>Self_Employed</th>\n",
" <th>ApplicantIncome</th>\n",
" <th>CoapplicantIncome</th>\n",
" <th>LoanAmount</th>\n",
" <th>Loan_Amount_Term</th>\n",
" <th>Credit_History</th>\n",
" <th>Property_Area</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Loan_ID</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>LP001015</th>\n",
" <td>Male</td>\n",
" <td>Yes</td>\n",
" <td>0.0</td>\n",
" <td>Graduate</td>\n",
" <td>No</td>\n",
" <td>5720</td>\n",
" <td>0</td>\n",
" <td>110.0</td>\n",
" <td>360.0</td>\n",
" <td>1.0</td>\n",
" <td>Urban</td>\n",
" </tr>\n",
" <tr>\n",
" <th>LP001022</th>\n",
" <td>Male</td>\n",
" <td>Yes</td>\n",
" <td>1.0</td>\n",
" <td>Graduate</td>\n",
" <td>No</td>\n",
" <td>3076</td>\n",
" <td>1500</td>\n",
" <td>126.0</td>\n",
" <td>360.0</td>\n",
" <td>1.0</td>\n",
" <td>Urban</td>\n",
" </tr>\n",
" <tr>\n",
" <th>LP001031</th>\n",
" <td>Male</td>\n",
" <td>Yes</td>\n",
" <td>2.0</td>\n",
" <td>Graduate</td>\n",
" <td>No</td>\n",
" <td>5000</td>\n",
" <td>1800</td>\n",
" <td>208.0</td>\n",
" <td>360.0</td>\n",
" <td>1.0</td>\n",
" <td>Urban</td>\n",
" </tr>\n",
" <tr>\n",
" <th>LP001035</th>\n",
" <td>Male</td>\n",
" <td>Yes</td>\n",
" <td>2.0</td>\n",
" <td>Graduate</td>\n",
" <td>No</td>\n",
" <td>2340</td>\n",
" <td>2546</td>\n",
" <td>100.0</td>\n",
" <td>360.0</td>\n",
" <td>NaN</td>\n",
" <td>Urban</td>\n",
" </tr>\n",
" <tr>\n",
" <th>LP001051</th>\n",
" <td>Male</td>\n",
" <td>No</td>\n",
" <td>0.0</td>\n",
" <td>Not Graduate</td>\n",
" <td>No</td>\n",
" <td>3276</td>\n",
" <td>0</td>\n",
" <td>78.0</td>\n",
" <td>360.0</td>\n",
" <td>1.0</td>\n",
" <td>Urban</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Gender Married Dependents Education Self_Employed \\\n",
"Loan_ID \n",
"LP001015 Male Yes 0.0 Graduate No \n",
"LP001022 Male Yes 1.0 Graduate No \n",
"LP001031 Male Yes 2.0 Graduate No \n",
"LP001035 Male Yes 2.0 Graduate No \n",
"LP001051 Male No 0.0 Not Graduate No \n",
"\n",
" ApplicantIncome CoapplicantIncome LoanAmount Loan_Amount_Term \\\n",
"Loan_ID \n",
"LP001015 5720 0 110.0 360.0 \n",
"LP001022 3076 1500 126.0 360.0 \n",
"LP001031 5000 1800 208.0 360.0 \n",
"LP001035 2340 2546 100.0 360.0 \n",
"LP001051 3276 0 78.0 360.0 \n",
"\n",
" Credit_History Property_Area \n",
"Loan_ID \n",
"LP001015 1.0 Urban \n",
"LP001022 1.0 Urban \n",
"LP001031 1.0 Urban \n",
"LP001035 NaN Urban \n",
"LP001051 1.0 Urban "
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test_data.head()"
]
},
{
"cell_type": "markdown",
"id": "82c774c7",
"metadata": {},
"source": [
"#### **<font color = green>3.2 Null value & special characters treatment</font>**"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "73000468",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Gender 13\n",
"Married 3\n",
"Dependents 15\n",
"Education 0\n",
"Self_Employed 32\n",
"ApplicantIncome 0\n",
"CoapplicantIncome 0\n",
"LoanAmount 22\n",
"Loan_Amount_Term 14\n",
"Credit_History 50\n",
"Property_Area 0\n",
"Loan_Status 0\n",
"dtype: int64"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Check for null values\n",
"train_data.isnull().sum()"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "4104fcfd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Gender 11\n",
"Married 0\n",
"Dependents 10\n",
"Education 0\n",
"Self_Employed 23\n",
"ApplicantIncome 0\n",
"CoapplicantIncome 0\n",
"LoanAmount 5\n",
"Loan_Amount_Term 6\n",
"Credit_History 29\n",
"Property_Area 0\n",
"dtype: int64"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test_data.isnull().sum()"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "838b2de4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gender 0\n",
"Married 0\n",
"Dependents 0\n",
"Education 0\n",
"Self_Employed 0\n",
"ApplicantIncome 0\n",
"CoapplicantIncome 0\n",
"LoanAmount 22\n",
"Loan_Amount_Term 0\n",
"Credit_History 50\n",
"Property_Area 0\n",
"Loan_Status 0\n",
"dtype: int64\n",
"Gender 0\n",
"Married 0\n",
"Dependents 0\n",
"Education 0\n",
"Self_Employed 0\n",
"ApplicantIncome 0\n",
"CoapplicantIncome 0\n",
"LoanAmount 5\n",
"Loan_Amount_Term 0\n",
"Credit_History 29\n",
"Property_Area 0\n",
"dtype: int64\n"
]
}
],
"source": [
"# Treating the null values\n",
"# Inputing Categorical missing data with \"mode\" value\n",
"\n",
"colname1 = [\"Gender\",\"Married\",\"Dependents\",\"Self_Employed\",\"Loan_Amount_Term\"]\n",
"\n",
"for x in colname1:\n",
" train_data[x].fillna(train_data[x].mode()[0],inplace=True)\n",
" test_data[x].fillna(test_data[x].mode()[0],inplace=True)\n",
" \n",
"print(train_data.isnull().sum())\n",
"print(test_data.isnull().sum())"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "5286092a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gender 0\n",
"Married 0\n",
"Dependents 0\n",
"Education 0\n",
"Self_Employed 0\n",
"ApplicantIncome 0\n",
"CoapplicantIncome 0\n",
"LoanAmount 0\n",
"Loan_Amount_Term 0\n",
"Credit_History 50\n",
"Property_Area 0\n",
"Loan_Status 0\n",
"dtype: int64\n",
"Gender 0\n",
"Married 0\n",
"Dependents 0\n",
"Education 0\n",
"Self_Employed 0\n",
"ApplicantIncome 0\n",
"CoapplicantIncome 0\n",
"LoanAmount 0\n",
"Loan_Amount_Term 0\n",
"Credit_History 29\n",
"Property_Area 0\n",
"dtype: int64\n"
]
}
],
"source": [
"# Inputing the numeric values with \"mean\" value\n",
"\n",
"train_data[\"LoanAmount\"].fillna(round(train_data[\"LoanAmount\"].mean(),0),inplace = True)\n",
"test_data[\"LoanAmount\"].fillna(round(test_data[\"LoanAmount\"].mean(),0),inplace = True)\n",
"\n",
"print(train_data.isnull().sum())\n",
"print(test_data.isnull().sum())"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "a0c0084f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gender 0\n",
"Married 0\n",
"Dependents 0\n",
"Education 0\n",
"Self_Employed 0\n",
"ApplicantIncome 0\n",
"CoapplicantIncome 0\n",
"LoanAmount 0\n",
"Loan_Amount_Term 0\n",
"Credit_History 0\n",
"Property_Area 0\n",
"Loan_Status 0\n",
"dtype: int64\n",
"Gender 0\n",
"Married 0\n",
"Dependents 0\n",
"Education 0\n",
"Self_Employed 0\n",
"ApplicantIncome 0\n",
"CoapplicantIncome 0\n",
"LoanAmount 0\n",
"Loan_Amount_Term 0\n",
"Credit_History 0\n",
"Property_Area 0\n",
"dtype: int64\n"
]
}
],
"source": [
"# So from above we got to know that we are still yet to replace the null values from credit history\n",
"\n",
"train_data[\"Credit_History\"].fillna(value=0,inplace = True)\n",
"test_data[\"Credit_History\"].fillna(value=0,inplace = True)\n",
"\n",
"print(train_data.isnull().sum())\n",
"print(test_data.isnull().sum())"
]
},
{
"cell_type": "markdown",
"id": "ab10b96b",
"metadata": {},
"source": [
"#### **<font color = green>3.3 Label Encoding</font>**"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "2503a1d7",
"metadata": {},
"outputs": [],
"source": [
"#Converting all the categorical data into numerical for train_data\n",
"\n",
"from sklearn.preprocessing import LabelEncoder\n",
"\n",
"colname = [\"Gender\",\"Married\",\"Education\",\"Self_Employed\",\"Dependents\",\"Self_Employed\",\"Property_Area\",\"Loan_Status\"]\n",
"\n",
"le = LabelEncoder()\n",
"\n",
"for x in colname:\n",
" train_data[x]=le.fit_transform(train_data[x])"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "8858dee9",
"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>Gender</th>\n",
" <th>Married</th>\n",
" <th>Dependents</th>\n",
" <th>Education</th>\n",
" <th>Self_Employed</th>\n",
" <th>ApplicantIncome</th>\n",
" <th>CoapplicantIncome</th>\n",
" <th>LoanAmount</th>\n",
" <th>Loan_Amount_Term</th>\n",
" <th>Credit_History</th>\n",
" <th>Property_Area</th>\n",
" <th>Loan_Status</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Loan_ID</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>LP001002</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>5849</td>\n",
" <td>0.0</td>\n",
" <td>146.0</td>\n",
" <td>360.0</td>\n",
" <td>1.0</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>LP001003</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>4583</td>\n",
" <td>1508.0</td>\n",
" <td>128.0</td>\n",
" <td>360.0</td>\n",
" <td>1.0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>LP001005</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>3000</td>\n",
" <td>0.0</td>\n",
" <td>66.0</td>\n",
" <td>360.0</td>\n",
" <td>1.0</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>LP001006</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>2583</td>\n",
" <td>2358.0</td>\n",
" <td>120.0</td>\n",
" <td>360.0</td>\n",
" <td>1.0</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>LP001008</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>6000</td>\n",
" <td>0.0</td>\n",
" <td>141.0</td>\n",
" <td>360.0</td>\n",
" <td>1.0</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Gender Married Dependents Education Self_Employed \\\n",
"Loan_ID \n",
"LP001002 1 0 0 0 0 \n",
"LP001003 1 1 1 0 0 \n",
"LP001005 1 1 0 0 1 \n",
"LP001006 1 1 0 1 0 \n",
"LP001008 1 0 0 0 0 \n",
"\n",
" ApplicantIncome CoapplicantIncome LoanAmount Loan_Amount_Term \\\n",
"Loan_ID \n",
"LP001002 5849 0.0 146.0 360.0 \n",
"LP001003 4583 1508.0 128.0 360.0 \n",
"LP001005 3000 0.0 66.0 360.0 \n",
"LP001006 2583 2358.0 120.0 360.0 \n",
"LP001008 6000 0.0 141.0 360.0 \n",
"\n",
" Credit_History Property_Area Loan_Status \n",
"Loan_ID \n",
"LP001002 1.0 2 1 \n",
"LP001003 1.0 0 0 \n",
"LP001005 1.0 2 1 \n",
"LP001006 1.0 2 1 \n",
"LP001008 1.0 2 1 "
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_data.head()"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "af9073f1",
"metadata": {},
"outputs": [],
"source": [
"#Converting all the categorical data into numerical for test_data\n",
"\n",
"from sklearn.preprocessing import LabelEncoder\n",
"\n",
"colname = [\"Gender\",\"Married\",\"Education\",\"Self_Employed\",\"Property_Area\"]\n",
"\n",
"le = LabelEncoder()\n",
"\n",
"for x in colname:\n",
" test_data[x]=le.fit_transform(test_data[x])"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "3dd8506b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",