-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabular_data_analysis.py
More file actions
838 lines (614 loc) · 25.5 KB
/
Tabular_data_analysis.py
File metadata and controls
838 lines (614 loc) · 25.5 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
# Install auto-sklearn
# Reference: https://colab.research.google.com/github/vopani/fortyone/blob/main/notebooks/automl/tabular/Auto-Sklearn.ipynb#scrollTo=l4sGr3c3WDSW
!curl https://raw.githubusercontent.com/automl/auto-sklearn/master/requirements.txt | xargs -n 1 -L 1 pip3 install
!pip3 install auto-sklearn
# Check for updates and get latest version
!pip3 install auto-sklearn --upgrade
# Run this line and restart run time if error occur during autosklearn import
!pip3 install scikit-learn --upgrade
!pip install scikit-learn==0.24.2
# Install Pipeline Profiler
!pip3 install pipelineprofiler
# Commented out IPython magic to ensure Python compatibility.
# %matplotlib inline
import pandas as pd
import numpy as np
import pickle
import autosklearn
import PipelineProfiler
from dateutil.relativedelta import relativedelta
from sklearn import metrics
from sklearn.tree import DecisionTreeClassifier
from sklearn.feature_selection import RFE
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
from autosklearn.regression import AutoSklearnRegressor
from sklearn.metrics import r2_score, confusion_matrix, classification_report, mean_squared_error
from math import sqrt
from matplotlib import pyplot as plt
"""# 1. Read File"""
# Connect to your GDrive
from google.colab import drive
drive.mount('/content/drive')
from pathlib import Path
# Get folder path
data_path = Path("./drive/MyDrive/Colab Notebooks/Fast_Furious_Insured")
# Read csv file into DataFrame
ori_train_df = pd.read_csv(data_path/"train.csv")
"""# 2. Data Preparation
## 2.1 Drop Irrelevant Rows
"""
# Drop rows with condition 0
# Do not need to predict those in good condition
train_df = ori_train_df.drop(ori_train_df[ori_train_df['Condition'] == 0].index)
train_df.head(5)
# Remove invalid value in "Amount"
train_df.drop(train_df.index[(train_df["Amount"] <0)],axis=0,inplace=True)
"""## 2.2 Derive New Attributes"""
# Extract year from the Expiry_date
train_df['Expiry_date'] = pd.to_datetime(train_df['Expiry_date'])
train_df['Expiry_year'] = pd.DatetimeIndex(train_df['Expiry_date']).year
# Create new attribute Year_difference
train_df['Year_difference'] = train_df['Expiry_year'] - 2021
train_df.head(5)
# Convert "Insurance_company" to numeric representation (binary)
cat_vars = ['Insurance_company']
for var in cat_vars:
cat_list = 'var'+'_'+var
cat_list = pd.get_dummies(train_df[var], prefix=var)
train_df1 = train_df.join(cat_list)
train_df = train_df1
data_vars = train_df.columns.values.tolist()
to_keep = [i for i in data_vars if i not in cat_vars]
train_df =train_df[to_keep]
# View columns created
train_df.columns.values
""" ## 2.3 Drop Irrelevant Columns"""
# Drop unused attributes
train_df = train_df.drop(columns=['Image_path', 'Expiry_date', 'Expiry_year', 'Condition'])
train_df.head(5)
"""## 2.4 Treat Missing Values
"""
# Checking for missing values
train_df.isnull().sum()
def impute_missing(df, option):
# Option 1: Fill missing values with values from next row, fill with 0 if there are still missing values
if option == 1:
df = df.fillna(method='bfill', axis=0).fillna(0)
# Option 2: Fill missing values with mean
elif option == 2:
df = df.fillna(df.mean())
# Option 3: Fill missing values with 0
elif option == 3:
df = df.fillna(0)
# Option 4: Remove rows with missing values
elif option == 4:
df = df.dropna(axis=0, how='any')
return df
"""## This point onwards you can perform your specific data prep
1. Place it under your own modeling section (3.1/ 3.2/ 3.3)
2. It should not affect other modeling sections, so create a new df for it
3. Name them using m#_train_df (replace # according to your method, sample shown below)
4. The change attribute type can only be applied when there's no missing values, so run the chosen imputation method before running the line with astype(int)
*This cell to be deleted after done transfering codes*
# 3.0 Data Modeling
### 3.1 Continuous Target
* Decision Tree
* Linear Regression
* Gaussian Naive Bayes
* K-Nearest Neighbors (KNN)
#### Preprocessing the variables
"""
# Current chosen imputation method
m1_train_df = impute_missing(train_df, 4)
# Convert all columns to int type
m1_train_df = m1_train_df.astype(int)
print(m1_train_df.info())
"""#### Feature selection """
# Split the data into X & y
X = m1_train_df.drop('Amount', axis = 1).values
y = m1_train_df['Amount']
y = y.astype(int)
print(X.shape)
print(y.shape)
train_df.head()
# Run a Tree-based estimators
dt = DecisionTreeClassifier(random_state=1, criterion = 'entropy')
dt.fit(X,y)
# Running Feature Importance
fi_col = []
fi = []
for i,column in enumerate(m1_train_df.drop('Amount', axis = 1)):
print('The feature importance for {} is : {}'.format(column, dt.feature_importances_[i]))
fi_col.append(column)
fi.append(dt.feature_importances_[i])
#RFE process
X = m1_train_df.loc[:, m1_train_df.columns != 'Amount']
y = m1_train_df['Amount']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
sel = RFE(DecisionTreeClassifier(),n_features_to_select = 10)
sel.fit(X_train, y_train)
sel.get_support()
features = X_train.columns[sel.get_support()]
print(features)
"""#### Modelling and evaluation """
# Split data into X and y
X = m1_train_df[features].values
y = m1_train_df['Amount']
print(X.shape)
"""Linear regression"""
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
lin_reg = LinearRegression()
lin_reg.fit(X_train, y_train)
LR_y_pred = lin_reg.predict(X_test)
print("The Testing Accuracy is: ", lin_reg.score(X_test, y_test))
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, LR_y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, LR_y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, LR_y_pred)))
"""Decision Tree"""
#X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0,test_size=0.2)
dt = DecisionTreeClassifier()
dt = dt.fit(X_train,y_train)
dt_y_pred = dt.predict(X_test)
print('Accuracy:',metrics.accuracy_score(y_test,dt_y_pred))
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, dt_y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, dt_y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, dt_y_pred)))
"""Gaussion Naive Bayes"""
#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
gnb = GaussianNB()
gnb = gnb.fit(X_train, y_train)
gnb_y_pred = gnb.predict(X_test)
print("The Testing Accuracy is: ", gnb.score(X_test, y_test))
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, gnb_y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, gnb_y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, gnb_y_pred)))
"""K Nearest Neighbours (KNN)"""
#X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
# K is user specified, check the best k:
k_range = range(1,20)
scores = []
for k in k_range:
knn = KNeighborsClassifier(n_neighbors = k)
knn.fit(X_train, y_train)
scores.append(knn.score(X_test, y_test))
plt.figure()
plt.xlabel('k')
plt.ylabel('accuracy')
plt.scatter(k_range, scores)
plt.xticks([0,5,10,15,20]);
#X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0,test_size=0.2)
knn = KNeighborsClassifier(n_neighbors = 3)
knn.fit(X_train, y_train)
knn_y_pred = knn.predict(X_test)
print("The Testing Accuracy is: ",knn.score(X_test, y_test))
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, knn_y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, knn_y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, knn_y_pred)))
"""Saving the model"""
# save model
with open('gnb.pkl', 'wb') as f:
pickle.dump(gnb, f)
# load model
with open('gnb.pkl', 'rb') as f:
loaded_regressor = pickle.load(f)
"""### 3.2 Categorical Target
* Logistic Regression
* Support Vector Machine (SVM)
* Decision Tree
* Gaussian Naive Bayes
* K-Nearest Neighbors (KNN)
#### Preprocessing the variables
"""
# Current chosen imputation method
m2_train_df = impute_missing(train_df, 2)
# Convert all columns to int type
m2_train_df = m2_train_df.astype(int)
print(m2_train_df.info())
# Transform the skewed variables to log
m2_train_df['Cost_of_vehicle'] = m2_train_df['Cost_of_vehicle'].transform([np.log])
m2_train_df['Min_coverage'] = m2_train_df['Min_coverage'].transform([np.log])
m2_train_df['Max_coverage'] = m2_train_df['Max_coverage'].transform([np.log])
# Replace the infinity values to 0 when treating missing values of option 1 & 3
m2_train_df = m2_train_df.replace([np.inf, -np.inf], 0)
# Convert the amount into binary using the median as a break (less than 4048 = 0, more than 4048 = 1)
m2_train_df["Binary_Amount"] = (m2_train_df["Amount"] >= m2_train_df["Amount"].median()).astype(int)
# Drop unused attributes
m2_train_df = m2_train_df.drop(columns=['Amount'])
m2_train_df.head(5)
"""#### Feature selection """
# Split the data into X & y
X = m2_train_df.drop('Binary_Amount', axis = 1).values
y = m2_train_df['Binary_Amount']
y = y.astype(int)
print(X.shape)
print(y.shape)
m2_train_df.head()
# Run a Tree-based estimators
dt = DecisionTreeClassifier(random_state=1, criterion = 'entropy')
dt.fit(X,y)
# Running Feature Importance
fi_col = []
fi = []
for i,column in enumerate(m2_train_df.drop('Binary_Amount', axis = 1)):
print('The feature importance for {} is : {}'.format(column, dt.feature_importances_[i]))
fi_col.append(column)
fi.append(dt.feature_importances_[i])
#RFE process
X = m2_train_df.loc[:, m2_train_df.columns != 'Amount']
y = m2_train_df['Amount']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
sel = RFE(DecisionTreeClassifier(),n_features_to_select = 10)
sel.fit(X_train, y_train)
sel.get_support()
features = X_train.columns[sel.get_support()]
print(features)
"""### Modelling and evaluation"""
# Split data into X and y
X = m2_train_df[features].values
y = m2_train_df['Amount']
print(X.shape)
"""Logistic regression"""
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
log_reg = LogisticRegression(random_state=10, solver = 'lbfgs')
log_reg.fit(X_train, y_train)
log_reg.predict(X_train)
y_pred = log_reg.predict(X_train)
pred_proba = log_reg.predict_proba(X_train)
print("The Testing Accuracy is: ", log_reg.score(X_test, y_test))
print(classification_report(y_train, y_pred))
"""Support Vector Machine (SVM) - RBF kernel"""
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
svm_rbf = svm.SVC(kernel='rbf')
svm_rbf.fit(X_train, y_train)
svm_rbf.predict(X_train)
y_pred = svm_rbf.predict(X_train)
print("The Testing Accuracy is: ", svm_rbf.score(X_test, y_test))
print(classification_report(y_train, y_pred))
"""Decision Tree"""
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1,test_size=0.2)
dt = DecisionTreeClassifier()
dt = dt.fit(X_train,y_train)
dt_y_pred = dt.predict(X_test)
print('Accuracy:',metrics.accuracy_score(y_test,dt_y_pred))
print(classification_report(y_train, y_pred))
"""Gaussian Naive Bayes"""
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
gnb = GaussianNB()
gnb.fit(X_train, y_train)
gnb.predict(X_train)
y_pred = gnb.predict(X_train)
print("The Testing Accuracy is: ", gnb.score(X_test, y_test))
print(classification_report(y_train, y_pred))
"""K Nearest Neigbours (KNN)"""
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
# K is user specified, check the best k:
k_range = range(1,20)
scores = []
for k in k_range:
knn = KNeighborsClassifier(n_neighbors = k)
knn.fit(X_train, y_train)
scores.append(knn.score(X_test, y_test))
plt.figure()
plt.xlabel('k')
plt.ylabel('accuracy')
plt.scatter(k_range, scores)
plt.xticks([0,5,10,15,20]);
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0,test_size=0.2)
knn = KNeighborsClassifier(n_neighbors = 12)
knn.fit(X_train, y_train)
y_predict = knn.predict(X_test)
print("The Testing Accuracy is: ",knn.score(X_test, y_test))
print(classification_report(y_train, y_pred))
"""Saving the model"""
# save model
with open('svm_rbf.pkl', 'wb') as f:
pickle.dump(svm_rbf, f)
# load model
with open('svm_rbf.pkl', 'rb') as f:
loaded_regressor = pickle.load(f)
"""## 3.3 AutoML
Reference: Auto-Sklearn 2.0, https://automl.github.io/auto-sklearn/master/
"""
# Current chosen imputation method
m3_train_df = impute_missing(train_df, 4)
# Convert all columns to int type
m3_train_df = m3_train_df.astype(int)
print(m3_train_df.info())
# Split the data into X & y
X = m3_train_df.drop('Amount', axis = 1).values
y = m3_train_df['Amount']
X = X.astype(int)
y = y.astype(int)
print(X.shape)
print(y.shape)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
sklearn_regression_aml_5min= AutoSklearnRegressor(time_left_for_this_task=60*5)
sklearn_regression_aml_5min.fit(X_train, y_train, dataset_name='Insurance data')
y_pred = sklearn_regression_aml_5min.predict(X_test)
r2 = metrics.r2_score(y_test, y_pred)
mae = metrics.mean_absolute_error(y_test, y_pred)
rmse = metrics.mean_squared_error(y_test, y_pred, squared=False)
print(" R2: ", r2, "\n", "MAE:", mae, "\n", "RMSE: ", rmse)
show_modes_str=sklearn_regression_aml_5min.show_models()
sprint_statistics_str = sklearn_regression_aml_5min.sprint_statistics()
print(show_modes_str)
print(sprint_statistics_str)
profiler_data = PipelineProfiler.import_autosklearn(sklearn_regression_aml_5min)
PipelineProfiler.plot_pipeline_matrix(profiler_data)
# save model
with open('sklearn_regression_aml_5min.pkl', 'wb') as f:
pickle.dump(sklearn_regression_aml_5min, f)
# load model
with open('sklearn_regression_aml_5min.pkl', 'rb') as f:
loaded_regressor = pickle.load(f)
"""---
# **OLD codes to be deleted after transfer**
## 3.1 Data Transformation
Transformation of the variables:
```
Only LOG have output
Only some modelling algorithms improved with the transformed variables through log.
```
"""
#apply transformation to 'Cost_of_vehicle', 'Min_coverage' & 'Max_coverage'
trans1 = train_df['Max_coverage'].transform([np.log, np.exp, np.reciprocal, np.sqrt])
#Seeing which transformation is the best (ONLY *LOG* has output)
trans1.hist(bins=20)
plt.suptitle('Transformed Output')
plt.show()
#change age to the tranformation you have choosen
train_df['Cost_of_vehicle'] = train_df['Cost_of_vehicle'].transform([np.log])
train_df['Min_coverage'] = train_df['Min_coverage'].transform([np.log])
train_df['Max_coverage'] = train_df['Max_coverage'].transform([np.log])
"""Creating dummy variable:"""
# Extract the year from the date and return the year difference (cause it makes more sense?)
train_df['Expiry_date'] = pd.to_datetime(train_df['Expiry_date'])
train_df['Expiry_year'] = pd.DatetimeIndex(train_df['Expiry_date']).year
train_df['Year_difference'] = train_df['Expiry_year'] - 2021
train_df.head(5)
# Drop unused variables
train_df = train_df.drop(columns=['Image_path', 'Expiry_date', 'Expiry_year'])
train_df.head(5)
# Convert "Insurance_company" to numeric representation (binary)
cat_vars = ['Insurance_company']
for var in cat_vars:
cat_list = 'var'+'_'+var
cat_list = pd.get_dummies(train_df[var], prefix=var)
train_df1 = train_df.join(cat_list)
train_df = train_df1
data_vars = train_df.columns.values.tolist()
to_keep = [i for i in data_vars if i not in cat_vars]
new_train_df =train_df[to_keep]
new_train_df.columns.values
new_train_df = new_train_df.astype(int) #change all binary to int type
print(new_train_df.info())
# Split the data into X & y
X = new_train_df.drop('Amount', axis = 1).values
y = new_train_df['Amount']
y = y.astype(int)
print(X.shape)
print(y.shape)
train_df.head()
"""## 3.2 Feature selection """
# Run a Tree-based estimators (i.e. decision trees & random forests)
#dt = RandomForestClassifier(random_state=1, criterion = 'entropy')#, max_depth = 10)
dt = DecisionTreeClassifier(random_state=1, criterion = 'entropy')#, max_depth = 10)
dt.fit(X,y)
# Running Feature Importance
fi_col = []
fi = []
for i,column in enumerate(new_train_df.drop('Amount', axis = 1)):
print('The feature importance for {} is : {}'.format(column, dt.feature_importances_[i]))
fi_col.append(column)
fi.append(dt.feature_importances_[i])
#RFE process
X = new_train_df.loc[:, new_train_df.columns != 'Amount']
y = new_train_df['Amount']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
sel = RFE(DecisionTreeClassifier(),n_features_to_select = 10) #can change the n features, up to you
sel.fit(X_train, y_train)
sel.get_support()
features = X_train.columns[sel.get_support()]
print(features)
"""
---
## **Method 1: Continuous as Target (Amount)**
"""
# Selecting the x and y
X = new_train_df[features].values
y = new_train_df['Amount']
print(X.shape)
# Linear regression
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
lin_reg = LinearRegression()
lin_reg.fit(X_train, y_train)
LR_y_pred = lin_reg.predict(X_test)
print("The Testing Accuracy is: ", lin_reg.score(X_test, y_test))
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, LR_y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, LR_y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, LR_y_pred)))
# Decision tree (accuracy = 0.07279693486590039)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1,test_size=0.2)
dt = DecisionTreeClassifier()
dt = dt.fit(X_train,y_train)
dt_y_pred = dt.predict(X_test)
print('Accuracy:',metrics.accuracy_score(y_test,dt_y_pred))
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, dt_y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, dt_y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, dt_y_pred)))
# Gausian Naive Bayes
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
gnb = GaussianNB()
gnb = gnb.fit(X_train, y_train)
gnb_y_pred = gnb.predict(X_test)
print("The Testing Accuracy is: ", gnb.score(X_test, y_test))
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, gnb_y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, gnb_y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, gnb_y_pred)))
# K nearest neighbours
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
# K is user specified , how do we know which K is the best to use ?
k_range = range(1,20)
scores = []
for k in k_range:
knn = KNeighborsClassifier(n_neighbors = k)
knn.fit(X_train, y_train)
scores.append(knn.score(X_test, y_test))
plt.figure()
plt.xlabel('k')
plt.ylabel('accuracy')
plt.scatter(k_range, scores)
plt.xticks([0,5,10,15,20]);
# I just do up to this part cause we can already see the highest accuracy from this chart is 0.0735 ...
# K NEAREST NEIGHBOURS
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0,test_size=0.2)
knn = KNeighborsClassifier(n_neighbors = 10)
knn.fit(X_train, y_train)
knn_y_pred = knn.predict(X_test)
print("The Testing Accuracy is: ",knn.score(X_test, y_test))
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, knn_y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, knn_y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, knn_y_pred)))
"""---
## **Method 2: Categorical as Target (Binary_Amount)**
## 3.1 Data Transformation
Transformation of the variables:
```
Only LOG have output
Only some modelling algorithms improved with the transformed variables through log.
```
"""
#apply transformation to 'Cost_of_vehicle', 'Min_coverage' & 'Max_coverage'
trans1 = train_df['Max_coverage'].transform([np.log, np.exp, np.reciprocal, np.sqrt])
#Seeing which transformation is the best (ONLY *LOG* has output)
trans1.hist(bins=20)
plt.suptitle('Transformed Output')
plt.show()
#change age to the tranformation you have choosen
train_df['Cost_of_vehicle'] = train_df['Cost_of_vehicle'].transform([np.log])
train_df['Min_coverage'] = train_df['Min_coverage'].transform([np.log])
train_df['Max_coverage'] = train_df['Max_coverage'].transform([np.log])
"""Creating dummy variables:"""
# Extract the year from the date (cause it's makes more sense to just take the year)
train_df['Expiry_date'] = pd.to_datetime(train_df['Expiry_date'])
train_df['Expiry_year'] = pd.DatetimeIndex(train_df['Expiry_date']).year
train_df['Year_difference'] = train_df['Expiry_year'] - 2021
train_df.head(5)
# Convert the amount into binary using the median as a break (less than 4048 = 0, more than 4048 = 1)
train_df["Binary_Amount"] = (train_df["Amount"] >= train_df["Amount"].median()).astype(int)
train_df.head(5)
# Split the data into X & y
X = new_train_df_categorical.drop('Binary_Amount', axis = 1).values
y = new_train_df_categorical['Binary_Amount']
y = y.astype(int)
print(X.shape)
print(y.shape)
train_df_categorical.head()
# Drop unused variables
train_df_categorical = train_df.drop(columns=['Image_path', 'Expiry_date', 'Expiry_year','Amount'])
train_df_categorical.head(5)
# Convert "Insurance_company" to numeric representation (binary)
cat_vars = ['Insurance_company']
for var in cat_vars:
cat_list = 'var'+'_'+var
cat_list = pd.get_dummies(train_df_categorical[var], prefix=var)
train_df_categorical1 = train_df_categorical.join(cat_list)
train_df_categorical = train_df_categorical1
data_vars = train_df_categorical.columns.values.tolist()
to_keep = [i for i in data_vars if i not in cat_vars]
new_train_df_categorical =train_df_categorical[to_keep]
new_train_df_categorical.columns.values
new_train_df_categorical = new_train_df_categorical.astype(int) #change all binary to int type
print(new_train_df_categorical.info())
# Split the data into X & y
X = new_train_df_categorical.drop('Binary_Amount', axis = 1).values
y = new_train_df_categorical['Binary_Amount']
y = y.astype(int)
print(X.shape)
print(y.shape)
train_df_categorical.head()
"""## 3.2 Feature selection """
# Run a Tree-based estimators (i.e. decision trees & random forests)
#dt = RandomForestClassifier(random_state=1, criterion = 'entropy')#, max_depth = 10)
dt = DecisionTreeClassifier(random_state=1, criterion = 'entropy')#, max_depth = 10)
dt.fit(X,y)
# Running Feature Importance
fi_col = []
fi = []
for i,column in enumerate(new_train_df_categorical.drop('Binary_Amount', axis = 1)):
print('The feature importance for {} is : {}'.format(column, dt.feature_importances_[i]))
fi_col.append(column)
fi.append(dt.feature_importances_[i])
#RFE process
X = new_train_df_categorical.loc[:, new_train_df_categorical.columns != 'Binary_Amount']
y = new_train_df_categorical['Binary_Amount']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
sel = RFE(DecisionTreeClassifier(),n_features_to_select = 10)
sel.fit(X_train, y_train)
sel.get_support()
features = X_train.columns[sel.get_support()]
print(features)
X = new_train_df_categorical[features].values
y = new_train_df_categorical['Binary_Amount']
print(X.shape)
"""# 4. Modelling
```
The model accuracy is much better where the highest result is 58.24% (KNN)
```
"""
# LOGISTIC REGRESSION
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
log_reg = LogisticRegression(random_state=10, solver = 'lbfgs')
log_reg.fit(X_train, y_train)
log_reg.predict(X_train)
y_pred = log_reg.predict(X_train)
pred_proba = log_reg.predict_proba(X_train)
print("The Testing Accuracy is: ", log_reg.score(X_test, y_test))
# SVM (kernel='rbf')
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
svm_rbf = svm.SVC(kernel='rbf')
svm_rbf.fit(X_train, y_train)
svm_rbf.predict(X_train)
y_pred = svm_rbf.predict(X_train)
print("The Testing Accuracy is: ", svm_rbf.score(X_test, y_test))
# Decision tree
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1,test_size=0.2)
dt = DecisionTreeClassifier()
dt = dt.fit(X_train,y_train)
dt_y_pred = dt.predict(X_test)
print('Accuracy:',metrics.accuracy_score(y_test,dt_y_pred))
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, dt_y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, dt_y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, dt_y_pred)))
# Gausian Naive Bayes
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
gnb = GaussianNB()
gnb.fit(X_train, y_train)
gnb.predict(X_train)
y_pred = gnb.predict(X_train)
print("The Testing Accuracy is: ", gnb.score(X_test, y_test))
# K nearest neighbours
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
# K is user specified , how do we know which K is the best to use ?
k_range = range(1,20)
scores = []
for k in k_range:
knn = KNeighborsClassifier(n_neighbors = k)
knn.fit(X_train, y_train)
scores.append(knn.score(X_test, y_test))
plt.figure()
plt.xlabel('k')
plt.ylabel('accuracy')
plt.scatter(k_range, scores)
plt.xticks([0,5,10,15,20]);
# K NEAREST NEIGHBOURS
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0,test_size=0.2)
knn = KNeighborsClassifier(n_neighbors = 10)
knn.fit(X_train, y_train)
y_predict = knn.predict(X_test)
print("The Testing Accuracy is: ",knn.score(X_test, y_test))