-
Notifications
You must be signed in to change notification settings - Fork 11
/
Classification_Using _Pyspark.py
1548 lines (1229 loc) · 56.7 KB
/
Classification_Using _Pyspark.py
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
#Classification Using Pyspark
#Pyspark Initializasing
# to make pyspark importable as a regular library
import findspark
findspark.init()
import pyspark
from pyspark import SparkContext
sc = SparkContext.getOrCreate()
#initializasing SparkSession for creating Spark DataFrame
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
#Load Libraries
# Data Frame spark profiling
from pyspark.sql.types import IntegerType, StringType, DoubleType, ShortType, DecimalType
import pyspark.sql.functions as func
from pyspark.sql.functions import isnull
from pyspark.sql.functions import isnan, when, count, col, round
from pyspark.sql.functions import mean
from pyspark.sql.types import Row
import matplotlib.pyplot as plt
from pyspark.sql.functions import udf
# Pandas DF operation
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from numpy import array
# Modeling + Evaluation
from pyspark.ml.feature import VectorAssembler, VectorIndexer, OneHotEncoder, StringIndexer
from pyspark.sql.functions import when
from pyspark.sql import functions as F
from pyspark.sql.functions import avg
from pyspark.ml import Pipeline
from pyspark.ml.classification import LogisticRegression, RandomForestClassifier, GBTClassifier
from pyspark.ml.classification import DecisionTreeClassifier
from pyspark.ml.evaluation import BinaryClassificationEvaluator, MulticlassClassificationEvaluator
from pyspark.mllib.evaluation import BinaryClassificationMetrics
from pyspark.ml.tuning import CrossValidator, ParamGridBuilder
from sklearn.metrics import roc_curve, auc
from sklearn.metrics import log_loss
from pyspark.sql import Window
from pyspark.sql.functions import rank,sum,col
from pyspark.ml.linalg import Vectors
from pyspark.ml.feature import VectorSlicer
window = Window.rowsBetween(Window.unboundedPreceding,Window.unboundedFollowing)
#Load Data to Spark DataFrame
#Initializing File Type and path for data train
file_type = 'text'
path=r'train.csv'
delimeter=','
def load_data(file_type):
"""input type of file "text" or "parquet" and Return pyspark dataframe"""
if file_type =="text": # use text as file type input
df = spark.read.option("header", "true") \
.option("delimeter",delimeter)\
.option("inferSchema", "true") \
.csv(path) #path file that you want import
else:
df= spark.read.parquet("example.parquet") #path file that you want import
return df
#call function load_data
df = load_data(file_type)
#Initializing File Type and path for data test
file_type = 'text'
path=r'test.csv'
delimeter=','
#call function load_data
test_data = load_data(file_type)
#Check data
#check type of data train and data test
type(df)
type(test_data)
#show 5 observation in data train
df.show(5)
#show 5 observation in data test
test_data.show(5)
#Print Schema and count number of columns from data train
len(df.columns), df.printSchema()
#Print Schema and count number of columns from data test
len(test_data.columns), test_data.printSchema()
#rename Target to 'label in data train
df = df.withColumnRenamed('QuoteConversion_Flag','label')
#rename Id number ('QuoteNumber') to 'Id' in data train
df = df.withColumnRenamed('QuoteNumber','Id')
#rename Id number ('QuoteNumber') to 'Id' in data test
test_data = test_data.withColumnRenamed('QuoteNumber','Id')
#drop column Original_Quote_Date from data train
df_final=df.drop('Original_Quote_Date')
#count number of observation in data train
df_final.count()
#drop column Original_Quote_Date from data test
test_data=test_data.drop('Original_Quote_Date')
#calculate percentage of target and save in dataframe called target_percent
target_percent=df_final.groupBy('label').count().sort(col("count").desc())\
.withColumn('total',sum(col('count')).over(window))\
.withColumn('Percent',col('count')*100/col('total'))
#show dataframe terget_percent to check the proportion
target_percent.show()
#Define categorical and nummerical variable in df_final (data train)
#Categorical and numerical variable
#just will select string data type
cat_cols = [item[0] for item in df_final.dtypes if item[1].startswith('string')]
print("cat_cols:", cat_cols)
#just will select integer or double data type
num_cols = [item[0] for item in df_final.dtypes if item[1].startswith('int') | item[1].startswith('double')]
print("num_cols:", num_cols)
#Select column 'Id' from num_cols
num_id=num_cols.pop(0)
print("num_id:", num_id)
#save column 'Id' in num_id variable
num_id=[num_id]
#print num_id
print(num_id)
#Remove column 'label' from numerical columns group
num_cols.remove('label') #label is removed because it's the target to validate the model
#print num_cols variable
print("num_cols:", num_cols)
#count number of numerical and categorical columns in data train
len(num_cols), len(cat_cols)
#Define categorical and nummerical variable in test_data (data test)
#Categorical and numerical variable
#just will select string data type
cat_cols_test = [item[0] for item in test_data.dtypes if item[1].startswith('string')]
print("cat_cols_test:", cat_cols_test)
#just will select integer or double data type
num_cols_test = [item[0] for item in test_data.dtypes if item[1].startswith('int') | item[1].startswith('double')]
print("num_cols_test:", num_cols_test)
#Select 'Id' from num_cols_test and save in variable called 'num_id_test'
num_id_test=num_cols_test.pop(0)
print("num_id_test:", num_id_test)
#save num_id_test to list called 'num_id_test'
num_id_test=[num_id_test]
print(num_id_test)
print(num_cols_test)
#count observation in data test
test_data.count()
#count number of numerical and categorical columns in data test
len(num_cols_test), len(cat_cols_test)
#Sample data
#define ratio that want to sample
ratio=0.1 #will take 10% from data
#take 10% sample from data train with replacing false and seed 42 and save in df_sample
df_sample=df_final.sample(False, ratio, 42)
#count observation from df_sample
df_sample.count()
#take 10% sample from data test with replacing false and seed 42 and save in test_sample
test_sample=test_data.sample(False, ratio, 42)
#count observation from test_sample
test_sample.count()
#Check Missing Value in data train
#Check Missing Value in Pyspark Dataframe
def count_nulls(c):
"""Input pyspark dataframe and return list of columns with missing value and it's total value"""
null_counts = [] #make an empty list to hold our results
for col in c.dtypes: #iterate through the column data types we saw above, e.g. ('C0', 'bigint')
cname = col[0] #splits out the column name, e.g. 'C0'
ctype = col[1] #splits out the column type, e.g. 'bigint'
nulls = c.where( c[cname].isNull()).count() #check count of null in column name
result = tuple([cname, nulls]) #new tuple, (column name, null count)
null_counts.append(result) #put the new tuple in our result list
null_counts=[(x,y) for (x,y) in null_counts if y!=0] #view just columns that have missing values
return null_counts
#Call function count_nulls and apply it to data train (df_final)
null_counts = count_nulls(df_final)
null_counts
#From null_counts, we just take information of columns name and save in list "list_cols_miss", like in the script below:
list_cols_miss=[x[0] for x in null_counts]
list_cols_miss
#Create dataframe which just has list_cols_miss
df_miss= df_final.select(*list_cols_miss)
df_miss.dtypes
#Define categorical columns and numerical columns which have missing value.
### for categorical columns
catcolums_miss=[item[0] for item in df_miss.dtypes if item[1].startswith('string')] #will select name of column with string data type
print("catcolums_miss:", catcolums_miss)
### for numerical columns
numcolumns_miss = [item[0] for item in df_miss.dtypes if item[1].startswith('int') | item[1].startswith('double')] #will select name of column with integer or double data type
print("numcolumns_miss:", numcolumns_miss)
#Drop missing value
df_Nomiss=df_final.na.drop()
#fill missing value in categorical variable with most frequent
for x in catcolums_miss:
mode=df_Nomiss.groupBy(x).count().sort(col("count").desc()).collect()[0][0] #group by based on categories and count each categories and sort descending then take the first value in column
print(x, mode) #print name of columns and it's most categories
df_final = df_final.na.fill({x:mode}) #fill missing value in each columns with most frequent
#fill missing value in numerical variable with average
for i in numcolumns_miss:
meanvalue = df_final.select(round(mean(i))).collect()[0][0] #calculate average in each numerical column
print(i, meanvalue) #print name of columns and it's average value
df_final=df_final.na.fill({i:meanvalue}) #fill missing value in each columns with it's average value
#Check Missing value after filling
null_counts = count_nulls(df_final)
null_counts
#Check Missing Value in data test
#We will cleansing missing values in pyspark dataframe.
#Call function to count missing values in test_data
null_test= count_nulls(test_data)
null_test
#take just name of columns that have missing values
list_miss_test=[x[0] for x in null_test]
list_miss_test
#Create dataframe which just has list_cols_miss
test_miss= test_data.select(*list_miss_test)
#view data types in df_miss
test_miss.dtypes
#Define categorical columns and numerical columns which have missing value.
### for categorical columns
catcolums_miss_test=[item[0] for item in test_miss.dtypes if item[1].startswith('string')] #will select name of column with string data type
print("catcolums_miss_test:", catcolums_miss_test)
### for numerical columns
numcolumns_miss_test = [item[0] for item in test_miss.dtypes if item[1].startswith('int') | item[1].startswith('double')] #will select name of column with integer or double data type
print("numcolumns_miss_test:", numcolumns_miss_test)
#Drop missing value
test_Nomiss=test_data.na.drop()
#fill missing value in categorical variable with most frequent
for x in catcolums_miss_test:
mode=test_Nomiss.groupBy(x).count().sort(col("count").desc()).collect()[0][0] #group by based on categories and count each categories and sort descending then take the first value in column
print(x, mode) #print name of columns and it's most categories
test_data = test_data.na.fill({x:mode}) #fill missing value in each columns with most frequent
#fill missing value in numerical variable with average
for i in numcolumns_miss_test:
meanvalue_test = test_data.select(round(mean(i))).collect()[0][0] #calculate average in each numerical column
print(i, meanvalue_test) #print name of columns and it's average value
test_data=test_data.na.fill({i:meanvalue_test}) #fill missing value in each columns with it's average value
#Check Missing value after filling
%time null_test = count_nulls(test_data)
null_test
#Compare categorical columns in df_final and test_data
#Function to check categorical columns in both data train and data test
def check_category2(a1,a2,y):
"""input are two dataframe you want to compare categorical variables and the colomn category name"""
print('column:',y)
#distinct1=a1.select([y]).distinct().count() #count distinct column in dataframe1
#distinct2=a2.select([y]).distinct().count() #count distinct column in dataframe2
#if distinct1 == distinct2:
var1=a1.select([y]).distinct() #define distinct category in column in dataframe1
var2=a2.select([y]).distinct() #define distinct category in column in dataframe2
diff2=var2.subtract(var1).collect() #define the different category in dataframe2, return is list
diff2=[r[y] for r in diff2] #just take the values
diff1=var1.subtract(var2).collect() #define the different category in dataframe1, return is list
diff1=[r[y] for r in diff1] #just take the values
if diff1 == diff2:
print('diff2:', diff2)
print('diff1:', diff1)
print('Columns match!!')
else:
if len(diff1)!=0 and len(diff2)==len(diff1):
print('diff2:', diff2)
print('diff1:', diff1)
a2=a2.replace(diff2, diff1, y) #replace the different category in dataframe2 with category in dataframe1
print('Columns match now!!')
else:
if len(diff2)!=len(diff1) and len(diff2)!=0:
print('diff2:', diff2)
print('diff1:', diff1)
dominant1=a1.groupBy(y).count().sort(col("count").desc()).collect()[0][0]
dominant2=a2.groupBy(y).count().sort(col("count").desc()).collect()[0][0] #define category dominant in dataframe2
print('dominant2:', dominant2)
print('dominant1:', dominant1)
a2=a2.replace(diff2, dominant1, y) #replace different category in dataframe2 with dominant category
print('Columns match now!!')
else:
print('diff1:', diff1)
print('diff2:', diff2)
return a2
#call function to check catgories in data train and test, whether same or not, if not, the different categories will be replaced.
for y in cat_cols_test:
test_data=check_category2(df_final,test_data,y)
#EDA
#Check distribution in each variables
#Pyspark dataframe has limitation in visualization. Then to create visualization we have to convert pyspark dataframe to pandas dataframe.
# convert spark dataframe to pandas for visualization
df_pd=df_final.toPandas()
#Barchart for categorical variable
plt.figure(figsize=(20,10))
plt.subplot(221)
sns.countplot(x='label', data=df_pd, order=df_pd['label'].value_counts().index)
plt.title('TARGET', fontsize=15)
plt.subplot(222)
sns.countplot(y='Field6', data=df_pd, order=df_pd['Field6'].value_counts().index)
plt.title('Field6', fontsize=15)
plt.subplot(223)
sns.countplot(x='Field12', data=df_pd, order=df_pd['Field12'].value_counts().index)
plt.title('Field12', fontsize=15)
plt.show()
#Barchart for categorical variable
plt.figure(figsize=(20,10))
plt.subplot(221)
sns.countplot(y='CoverageField8', data=df_pd, order=df_pd['CoverageField8'].value_counts().index)
plt.title('CoverageField8', fontsize=15)
plt.subplot(222)
sns.countplot(y='CoverageField9', data=df_pd, order=df_pd['CoverageField9'].value_counts().index)
plt.title('CoverageField9', fontsize=15)
plt.subplot(223)
sns.countplot(y='SalesField7', data=df_pd, order=df_pd['SalesField7'].value_counts().index)
plt.title('SalesField7', fontsize=15)
plt.show()
#Categorical vs Target visualization
pd.crosstab(df_pd['Field6'], df_pd['label'], normalize='index').plot.bar(rot=0, stacked=True,
color=['green', 'red'], figsize=(4,4), title="Field6 VS label")
plt.legend(loc='upper right', bbox_to_anchor=(1.2, 1))
pd.crosstab(df_pd['Field12'], df_pd['label'], normalize='index').plot.bar(rot=0, stacked=True,
color=['green', 'red'], figsize=(4,4), title="Field12 VS label")
plt.legend(loc='upper right', bbox_to_anchor=(1.2, 1))
plt.show()
#Numerical Variables
#We have 260 numerical variables, and we will plot just some variables.
#density plot Field7
#plt.figure(figsize=(24,5))
sns.distplot(df_pd['Field7'])
plt.show()
#Numerical vs Target visualization
#show distribution 'Field7' vs 'label'
#plt.figure(figsize=(20,8))
sns.kdeplot(df_pd[df_pd["label"]==0]["Field7"], label="0", color="green")
sns.kdeplot(df_pd[df_pd["label"]==1]["Field7"], label="1", color="red")
plt.title("Field7 VS label")
plt.show()
#Check outlier in numerical variable
df_pd[["Field7"]].boxplot(sym='g-*', grid=True)
plt.show()
#Insignificant Categories in Data train
#Define the threshold for insignificant categories
threshold=98
threshold2=0.7
#function to replace insignificant categories in data train
def replace_cat2(f,cols):
"""input are dataframe and categorical variables, replace insignificant categories (percentage <=0.7) with largest number
of catgories and output is new dataframe """
df_percent=f.groupBy(cols).count().sort(col("count").desc())\
.withColumn('total',sum(col('count')).over(window))\
.withColumn('Percent',col('count')*100/col('total')) #calculate the percentage-save in Percent columns from each categories
dominant_cat=df_percent.select(df_percent['Percent']).collect()[0][0] #calculate the highest percentage of category
count_dist=f.select([cols]).distinct().count() #calculate distinct values in that columns
if count_dist > 2 and dominant_cat <= threshold :
print('column:', cols)
cols_names.append(cols) #combine with previous list
replacement=f.groupBy(cols).count().sort(col("count").desc()).collect()[0][0] #define dominant category
print("replacement:",replacement)
replacing.append(replacement) #combine with previous list
insign_cat=df_percent.filter(df_percent['Percent']< threshold2).select(df_percent[cols]).collect() #calculate insignificant categories
insign_cat=[r[cols] for r in insign_cat] #just take the values
category.append(insign_cat) #combine with previous list
print("insign_cat:",insign_cat)
f=f.replace(insign_cat,replacement, cols) #replace insignificant categories with dominant categories
return f
#call function replacing insignificant categories in data train
replacing=[]
cols_names=[]
category=[]
for cols in cat_cols:
df_final=replace_cat2(df_final,cols)
#check length in list cols_names, category and replacing
len(cols_names), len(category), len(replacing)
#Create dataframe of replaced categories
g=spark.createDataFrame(list(zip(cols_names, replacing, category)),['cols_names', 'replacing', 'category'])
g.show(9)
#Replacing Insignificant Categories in data test
#We already have a dataframe containing any categories that need to be replaced,
#we got it when the process of replacing the insignificant categories in the data train, the data frame is called g.
#Based on those information, insignificant categories on data test will be replaced.
cols_names_list=g.select('cols_names').collect() #select just cols_names from dataframe g
cols_names_list=[r['cols_names'] for r in cols_names_list] #take just the values
#function to replace insignificant categories in data test
for z in cols_names_list:
print('cols_names:',z)
replacement_cat=g.filter(g['cols_names']== z).select(g['replacing']).collect()[0][0] #select values of replacing columns accoring to z in cols_names
print('replacement_cat:', replacement_cat)
insignificant_cat=g.filter(g['cols_names']== z).select(g['category']).collect()[0][0] #select values of category columns accoring to z in cols_names
print('insignificant_cat:',insignificant_cat)
test_data=test_data.replace(insignificant_cat,replacement_cat, z) #replace insignificant cat with replacement value
#Handle of outlier in data train
#Calculate Upper&Lower side in pandas dataframe
df_describe=df_pd.describe()
df_describe
#Calculate Upper&Lower side in pyspark dataframe
#create quantile dataframe
def quantile(e):
"""Input is dataframe and return new dataframe with value of quantile from numerical columns"""
percentiles = [0.25, 0.5, 0.75]
quant=spark.createDataFrame(zip(percentiles, *e.approxQuantile(num_cols, percentiles, 0.0)),
['percentile']+num_cols) #calculate quantile from pyspark dataframe, 0.0 is relativeError,
#The relative target precision to achieve (>= 0). If set to zero,
#the exact quantiles are computed, which could be very expensive
#and aggregate the result with percentiles variable,
#then create pyspark dataframe
return quant
#call quantile function
%time quantile=quantile(df_sample)
#function to calculate uppler side
def upper_value(b,c):
"""Input is quantile dataframe and name of numerical column and Retrun upper value from the column"""
q1 = b.select(c).collect()[0][0] #select value of q1 from the column
q2 = b.select(c).collect()[1][0] #select value of q2 from the column
q3 = b.select(c).collect()[2][0] #select value of q3 from the column
IQR=q3-q1 #calculate the value of IQR
upper= q3 + (IQR*1.5) #calculate the value of upper side
return upper
#function to calculate lower side
def lower_value(b,c):
"""Input is quantile dataframe and name of numerical column and Retrun lower value from the column"""
q1 = b.select(c).collect()[0][0] #select value of q1 from the column
q2 = b.select(c).collect()[1][0] #select value of q2 from the column
q3 = b.select(c).collect()[2][0] #select value of q3 from the column
IQR=q3-q1 #calculate the value of IQR
lower= q1 - (IQR*1.5) #calculate the value of lower side
return lower
#function for replacing outlier by upper side
def replce_outlier_up2(d,col, value):
"""Input is name of numerical column and it's upper side value"""
d=d.withColumn(col, F.when(d[col] > value , value).otherwise(d[col]))
return d
#function for replacing outlier with lower side
def replce_outlier_low2(d,col, value):
"""Input is name of numerical column and it's lower side value"""
d=d.withColumn(col, F.when(d[col] < value , value).otherwise(d[col]))
return d
#call function to calculate lower side and replace value under lower side with value lower side at all numerical variables
for i in num_cols:
lower=lower_value(quantile,i)
df_final=replce_outlier_low2(df_final, i, lower)
#call function to calculate upper side and replace value above upper side with value upper side at all numerical variables
for x in num_cols:
upper=upper_value(quantile,x)
df_final=replce_outlier_up2(df_final, x, upper)
#Handle of outlier in data test
#create quantile dataframe
def quantile(e):
"""Input is dataframe and return new dataframe with value of quantile from numerical columns"""
percentiles = [0.25, 0.5, 0.75]
quant=spark.createDataFrame(zip(percentiles, *e.approxQuantile(num_cols_test, percentiles, 0.0)),
['percentile']+num_cols_test) #calculate quantile from pyspark dataframe, 0.0 is relativeError,
#The relative target precision to achieve (>= 0). If set to zero,
#the exact quantiles are computed, which could be very expensive
#and aggregate the result with percentiles variable,
#then create pyspark dataframe
return quant
#call funtion quantile
quantile=quantile(test_sample)
#call function to calculate lower side and replace value under lower side with value lower side at all numerical variables
for i in num_cols_test:
lower=lower_value(quantile,i)
test_data=replce_outlier_low2(test_data, i, lower)
#call function to calculate upper side and replace value above upper side with value upper side at all numerical variables
for x in num_cols_test:
upper=upper_value(quantile,x)
test_data=replce_outlier_up2(test_data, x, upper)
#Feature Engineering
#function to check distinct categories in data train and data test
def check_distinct(a1,a2):
"""input are two dataframe that you want to compare categorical variables and the output is
total distinct categories in both dataframe"""
total1=0
total2=0
for y in cat_cols:
distinct1=a1.select([y]).distinct().count() #count distinct column in dataframe1
distinct2=a2.select([y]).distinct().count() #count distinct column in dataframe2
var1=a1.select([y]).distinct().collect() #define distinct category in column in dataframe1
var1=[r[y] for r in var1]
var2=a2.select([y]).distinct().collect()
var2=[r[y] for r in var2]
total1=total1+distinct1
total2=total2+distinct2
return total1, total2
#function to execute feature engineering
def feature_engineering(a1):
"""Function for feature engineering (StringIndexer and OneHotEncoder process)"""
cat_columns_string_vec = []
for c in cat_cols:
cat_columns_string= c+"_vec"
cat_columns_string_vec.append(cat_columns_string)
stringIndexer = [StringIndexer(inputCol=x, outputCol=x+"_Index")
for x in cat_cols]
#use oneHotEncoder to convert categorical variable to binary
encoder = [OneHotEncoder(inputCol=x+"_Index", outputCol=y)
for x,y in zip(cat_cols, cat_columns_string_vec)]
#create list of stringIndexer and encoder with 2 dimension
tmp = [[i,j] for i,j in zip(stringIndexer, encoder)]
tmp = [i for sublist in tmp for i in sublist]
cols_assember=num_id + num_cols + cat_columns_string_vec
assembler=VectorAssembler(inputCols=cols_assember, outputCol='features')
tmp += [assembler]
pipeline=Pipeline(stages=tmp)
df_final_feat=pipeline.fit(a1).transform(a1)
return df_final_feat
#fucntion to call fucntion feature_engineering and check_distinct
def Main_feature_engineering(df,df2):
"""Function for calling check_distinct and feature_engineering. Then Join data train and data test if distinct categories
between data train and data test not same then do feature engineering, If distinct same will do feature engineering data train
and data test separately"""
dist_total1, dist_total2=check_distinct(df,df2)
if dist_total1!=dist_total2:
Label_df=df.select('Id', 'label')
df_final2=df.drop('label')
all_df =df_final2.union(df2)
all_df_feat=feature_engineering(all_df)
id_train=df.select('Id').collect()
id_train=[r['Id'] for r in id_train]
id_test=df2.select('Id').collect()
id_test=[r['Id'] for r in id_test]
a=all_df_feat.filter(all_df['Id'].isin(id_train))
b=all_df_feat.filter(all_df['Id'].isin(id_test))
a=a.join(Label_df, 'Id')
else:
a=feature_engineering(df)
b=feature_engineering(df2)
return a,b
#call function feature engineering
%time data2, test2=Main_feature_engineering(df_final, test_data)
#view result of feature engineering in data train
data2.select('Id', 'features').show(5)
#view result of feature engineering in data test
test2.select('Id', 'features').show(5)
#Split Data train to train and test
#Split df_final to train and test, train 70% and test 30%. Define seed 24 so the random data that we split will not change.
#we can define seed with any value
data_train, data_test=data2.randomSplit([0.7,0.3], 24)
#Modelling & Evaluation
#Logistic Regression
#Create logistic regression model to data train
lr=LogisticRegression(featuresCol='features', labelCol='label')
lr_model = lr.fit(data_train)
#Transform model to data test
lr_result = lr_model.transform(data_test)
#view id, label, prediction and probability from result of modelling
lr_result.select('Id', 'label', 'prediction', 'probability').show(5)
#Logistic Regression Evaluation
#Evaluate model by checking accuracy and AUC value
lr_eval = BinaryClassificationEvaluator(rawPredictionCol="probability", labelCol="label")
lr_eval2= MulticlassClassificationEvaluator(predictionCol="prediction", labelCol="label")
lr_AUC = lr_eval.evaluate(lr_result)
lr_ACC = lr_eval2.evaluate(lr_result, {lr_eval2.metricName:"accuracy"})
print("Logistic Regression Performance Measure")
print("Accuracy = %0.2f" % lr_ACC)
print("AUC = %.2f" % lr_AUC)
#ROC Grafik
#Create ROC grafik from lr_result
PredAndLabels = lr_result.select("probability", "label")
PredAndLabels_collect = PredAndLabels.collect()
PredAndLabels_list = [(float(i[0][0]), 1.0-float(i[1])) for i in PredAndLabels_collect]
PredAndLabels = sc.parallelize(PredAndLabels_list)
metrics = BinaryClassificationMetrics(PredAndLabels)
# Area under ROC
print("Logistic Regression Area Under ROC")
print("Area under ROC = %.2f" % metrics.areaUnderROC)
# Visualization
FPR = dict() # FPR: False Positive Rate
tpr = dict() # TPR: True Positive Rate
roc_auc = dict()
y_test = [i[1] for i in PredAndLabels_list]
y_score = [i[0] for i in PredAndLabels_list]
fpr, tpr, _ = roc_curve(y_test, y_score)
roc_auc = auc(fpr, tpr)
plt.figure(figsize=(5,4))
plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], 'k--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve - Logistic Regression')
plt.legend(loc="lower right")
plt.show()
#confusion Matrix
cm_lr_result = lr_result.crosstab("prediction", "label")
cm_lr_result = cm_lr_result.toPandas()
cm_lr_result
#calculate Accuracy, Sensitivity, Specificity, Precision
TP = cm_lr_result["1"][0]
FP = cm_lr_result["0"][0]
TN = cm_lr_result["0"][1]
FN = cm_lr_result["1"][1]
Accuracy = (TP+TN)/(TP+FP+TN+FN)
Sensitivity = TP/(TP+FN)
Specificity = TN/(TN+FP)
Precision = TP/(TP+FP)
print ("Accuracy = %0.2f" %Accuracy )
print ("Sensitivity = %0.2f" %Sensitivity )
print ("Specificity = %0.2f" %Specificity )
print ("Precision = %0.2f" %Precision )
#Calculate Gini Coefficient from AUC
AUC = lr_AUC
Gini = (2 * AUC - 1)
print("AUC=%.2f" % AUC)
print("GINI ~=%.2f" % Gini)
#Calculate Log Loss in pandas dataframe
#Create Dataframe to Calculate Log Loss
y_test= data_test.select('label')
lr_proba=lr_result.select('probability')
#Convert lr_probaspark dataframe to numpy array
lr_proba= np.array(lr_result.select('probability').collect())
#Convert numpy array 3 dimentional to 2 dimentional
lr_proba=lr_proba.reshape(-1, lr_proba.shape[-1])
#Convert y_test dataframe to pandas dataframe
y_test=y_test.toPandas()
#Convert y_test pandas dataframe to pandas series
y_test=pd.Series(y_test['label'].values)
#Calculate log loss from logistic regression
LogLoss = log_loss(y_test, lr_proba)
print("Log Loss Linear Regression:%.4f" % LogLoss)
#Logistic Regression With Hyper-Parameter Tuning
#define logistic regression model
lr_hyper=LogisticRegression(featuresCol='features', labelCol='label')
#Hyper-Parameter Tuning
paramGrid_lr = ParamGridBuilder() \
.addGrid(lr_hyper.regParam, [0.1, 0.01]) \
.addGrid(lr_hyper.elasticNetParam, [0.8, 0.7]) \
.build()
crossval_lr = CrossValidator(estimator=lr_hyper,
estimatorParamMaps=paramGrid_lr,
evaluator=BinaryClassificationEvaluator(),
numFolds=3)
#fit model to data train
lr_model_hyper = crossval_lr.fit(data_train)
#Transform model to data test
lr_result_hyper = lr_model_hyper.transform(data_test)
#view id, label, prediction and probability from result of modelling
lr_result_hyper.select('Id', 'label', 'prediction', 'probability').show(5)
#Logistic Regression With Hyper-Parameter Tuning Evaluation
#Evaluate model by checking accuracy and AUC value
lr_hyper_eval = BinaryClassificationEvaluator(rawPredictionCol="probability", labelCol="label")
lr_hyper_eval2= MulticlassClassificationEvaluator(predictionCol="prediction", labelCol="label")
lr_hyper_AUC = lr_hyper_eval.evaluate(lr_result_hyper)
lr_hyper_ACC = lr_hyper_eval2.evaluate(lr_result_hyper, {lr_hyper_eval2.metricName:"accuracy"})
print("Logistic Regression Performance Measure")
print("Accuracy = %0.2f" % lr_hyper_ACC)
print("AUC = %.2f" % lr_hyper_AUC)
#ROC Grafik
PredAndLabels = lr_result_hyper.select("probability", "label")
PredAndLabels_collect = PredAndLabels.collect()
PredAndLabels_list = [(float(i[0][0]), 1.0-float(i[1])) for i in PredAndLabels_collect]
PredAndLabels = sc.parallelize(PredAndLabels_list)
metrics = BinaryClassificationMetrics(PredAndLabels)
# Area under ROC
print("Logistic Regression Area Under ROC")
print("Area under ROC = %.2f" % metrics.areaUnderROC)
# Visualization
FPR = dict() # FPR: False Positive Rate
tpr = dict() # TPR: True Positive Rate
roc_auc = dict()
y_test = [i[1] for i in PredAndLabels_list]
y_score = [i[0] for i in PredAndLabels_list]
fpr, tpr, _ = roc_curve(y_test, y_score)
roc_auc = auc(fpr, tpr)
plt.figure(figsize=(5,4))
plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], 'k--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve - Logistic Regression')
plt.legend(loc="lower right")
plt.show()
#confusion matrix
cm_lr_result_hyper = lr_result_hyper.crosstab("prediction", "label")
cm_lr_result_hyper = cm_lr_result_hyper.toPandas()
cm_lr_result_hyper
#calculate Accuracy, Sensitivity, Specificity, Precision
TP = cm_lr_result_hyper["1"][0]
FP = cm_lr_result_hyper["0"][0]
TN = cm_lr_result_hyper["0"][1]
FN = cm_lr_result_hyper["1"][1]
Accuracy = (TP+TN)/(TP+FP+TN+FN)
Sensitivity = TP/(TP+FN)
Specificity = TN/(TN+FP)
Precision = TP/(TP+FP)
print ("Accuracy = %0.2f" %Accuracy )
print ("Sensitivity = %0.2f" %Sensitivity )
print ("Specificity = %0.2f" %Specificity )
print ("Precision = %0.2f" %Precision )
#Calculate Gini Coefisient from AUC
AUC = lr_hyper_AUC
Gini_lr_hyper = (2 * AUC - 1)
print("AUC=%.2f" % AUC)
print("GINI ~=%.2f" % Gini_lr_hyper)
#Calculate Log Loss in pandas dataframe
#Create Dataframe to Calculate Log Loss
y_test= titanic_test.select('label')
lr_hyper_proba=lr_result_hyper.select('probability')
#Convert lr_probaspark dataframe to numpy array
lr_hyper_proba= np.array(lr_hyper_proba.select('probability').collect())
#Convert numpy array 3 dimentional to 2 dimentional
lr_hyper_proba=lr_hyper_proba.reshape(-1, lr_hyper_proba.shape[-1])
#Convert y_test dataframe to pandas dataframe
y_test=y_test.toPandas()
#Convert y_test pandas dataframe to pandas series
y_test=pd.Series(y_test['label'].values)
#Calculate log loss from logistic regression hyper parameter
LogLoss = log_loss(y_test, lr_hyper_proba)
print("Log Loss Linear Regression:%.4f" % LogLoss)
#Decision Tree
#Create decision tree model to data train
dt=DecisionTreeClassifier(featuresCol = 'features', labelCol = 'label', maxDepth = 3)
dt_model = dt.fit(data_train)
##Transform model to data test
dt_result = dt_model.transform(data_test)
#view id, label, prediction and probability from result of modelling
dt_result.select('Id', 'label', 'prediction', 'probability').show(5)
# Decision Tree Evaluation
#Evaluate model by calculating accuracy and area under curve (AUC)
dt_eval = BinaryClassificationEvaluator(rawPredictionCol="probability", labelCol="label")
dt_eval2= MulticlassClassificationEvaluator(predictionCol="prediction", labelCol="label")
dt_AUC = dt_eval.evaluate(dt_result)
dt_ACC = dt_eval2.evaluate(dt_result, {dt_eval2.metricName:"accuracy"})
print("Decision Tree Performance Measure")
print("Accuracy = %0.2f" % dt_ACC)
print("AUC = %.2f" % dt_AUC)
#ROC Grafik
PredAndLabels = dt_result.select("probability", "label")
PredAndLabels_collect = PredAndLabels.collect()
PredAndLabels_list = [(float(i[0][0]), 1.0-float(i[1])) for i in PredAndLabels_collect]
PredAndLabels = sc.parallelize(PredAndLabels_list)
metrics = BinaryClassificationMetrics(PredAndLabels)
# Area under ROC
print("Decision Tree Area Under ROC")
print("Area under ROC = %.2f" % metrics.areaUnderROC)
# Visualization
FPR = dict() # FPR: False Positive Rate
tpr = dict() # TPR: True Positive Rate
roc_auc = dict()
y_test = [i[1] for i in PredAndLabels_list]
y_score = [i[0] for i in PredAndLabels_list]
fpr, tpr, _ = roc_curve(y_test, y_score)
roc_auc = auc(fpr, tpr)
plt.figure(figsize=(5,4))
plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], 'k--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve - Decision Tree')
plt.legend(loc="lower right")
plt.show()
#confusion matrix
cm_dt_result = dt_result.crosstab("prediction", "label")
cm_dt_result = cm_dt_result.toPandas()
cm_dt_result
#calculate accuracy, sensitivity, specificity and precision
TP = cm_dt_result["1"][0]
FP = cm_dt_result["0"][0]
TN = cm_dt_result["0"][1]
FN = cm_dt_result["1"][1]
Accuracy = (TP+TN)/(TP+FP+TN+FN)
Sensitivity = TP/(TP+FN)
Specificity = TN/(TN+FP)
Precision = TP/(TP+FP)
print ("Accuracy = %0.2f" %Accuracy )
print ("Sensitivity = %0.2f" %Sensitivity )
print ("Specificity = %0.2f" %Specificity )
print ("Precision = %0.2f" %Precision )
#Calculate Gini Coeffiecient from AUC
AUC = dt_AUC
Gini_dt = (2 * AUC - 1)
print("AUC=%.2f" % AUC)
print("GINI ~=%.2f" % Gini_dt)
#Calculate Log Loss in pandas dataframe
#Create Dataframe to Calculate Log Loss
y_test= data_test.select('label')
dt_proba=dt_result.select('probability')
##Convert lr_probaspark dataframe to numpy array
dt_proba= np.array(dt_proba.select('probability').collect())
#Convert numpy array 3 dimentional to 2 dimentional
dt_proba=dt_proba.reshape(-1, dt_proba.shape[-1])
#Convert y_test dataframe to pandas dataframe
y_test=y_test.toPandas()
#Convert y_test pandas dataframe to pandas series
y_test=pd.Series(y_test['label'].values)
#Calculate log loss from Decision Tree
LogLoss = log_loss(y_test, dt_proba)
print("Log Loss Decision Tree:%.4f" % LogLoss)
#Decision Tree With Hyper-Parameter Tuning
#define decision tree model
dt_hyper=DecisionTreeClassifier(featuresCol = 'features', labelCol = 'label', impurity='gini')
#Hyper-Parameter Tuning
paramGrid_dt = ParamGridBuilder() \
.addGrid(dt_hyper.maxDepth, [5, 7]) \
.addGrid(dt_hyper.maxBins, [10,20]) \
.build()
crossval_dt = CrossValidator(estimator=dt_hyper,
estimatorParamMaps=paramGrid_dt,
evaluator=BinaryClassificationEvaluator(),
numFolds=5)
#fit model to data train
dt_model_hyper = crossval_dt.fit(data_train)
#transform model to data test
dt_result_hyper = dt_model_hyper.transform(data_test)
#view id, label, prediction and probability from result of modelling
dt_result_hyper.select('Id', 'label', 'prediction', 'probability').show(5)
#Decision Tree With Hyper-Parameter Tuning Evaluation
#Evaluate model by calculating accuracy and area under curve (AUC)
dt_hyper_eval = BinaryClassificationEvaluator(rawPredictionCol="probability", labelCol="label")
dt_hyper_eval2= MulticlassClassificationEvaluator(predictionCol="prediction", labelCol="label")
dt_hyper_AUC = dt_hyper_eval.evaluate(dt_result_hyper)
dt_hyper_ACC = dt_hyper_eval2.evaluate(dt_result_hyper, {dt_hyper_eval2.metricName:"accuracy"})
print("Decision Tree Performance Measure")
print("Accuracy = %0.2f" % dt_hyper_ACC)
print("AUC = %.2f" % dt_hyper_AUC)
#ROC Grafik
PredAndLabels = dt_result_hyper.select("probability", "label")
PredAndLabels_collect = PredAndLabels.collect()
PredAndLabels_list = [(float(i[0][0]), 1.0-float(i[1])) for i in PredAndLabels_collect]
PredAndLabels = sc.parallelize(PredAndLabels_list)
metrics = BinaryClassificationMetrics(PredAndLabels)
# Area under ROC
print("Decision Tree Area Under ROC")
print("Area under ROC = %.2f" % metrics.areaUnderROC)