-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVisualization_MidTermCode.py
More file actions
308 lines (192 loc) · 7.98 KB
/
Copy pathVisualization_MidTermCode.py
File metadata and controls
308 lines (192 loc) · 7.98 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
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 23 11:08:16 2021
@author: kamar
"""
from pyspark.sql import SparkSession
from pyspark import SparkContext
from pyspark import SparkConf
from pyspark.sql.functions import avg, col, length
import matplotlib.pyplot as plt
conf = SparkConf().setAppName("Project App").setMaster("local")
sc = SparkContext(conf=conf)
spark = SparkSession(sc)
from pyspark.sql import SQLContext
sql_context = SQLContext(sc)
PATH = r"D:\Kee cllg\spring 2021\CIS 5367 Machine ln\Project\Project submission"
bank_data=sql_context.read.load("%s/bank-full.csv" % PATH,
format='com.databricks.spark.csv',
header='true',
inferSchema='true')
print(bank_data.head(5))
#Output Variable is y. Change column "y" to "Target"
import pyspark.sql.functions as F
bank_data = bank_data.select( '*', F.col('y').alias('Target') ).drop('y')
"""Summary statistics of the dataframe
"""
bank_data.summary().show()
bank_data.summary().show(truncate=False)
bank_data.describe().show()
bank_data.show()
"""Print schema of the bank data"""
bank_data.printSchema()
"""Check the value count for each unique value in the target column
"""
bank_data.select("Target").distinct().show()
deposit = bank_data.rdd.map(lambda x: x[16]).countByValue()
"""Summary statistics for numeric variables
"""
numerical_features=bank_data.select("age","balance","day","duration","campaign","pdays","previous")
numerical_features.describe().show()
print(numerical_features.show(5))
# Checking distribution of all numeric features in the datase
numeric_features = [t[0] for t in bank_data.dtypes if t[1] == 'int']
bank_data.select(numeric_features).describe().toPandas().transpose()
"""Summary statistics for Categorical variables
"""
Categorical_features=bank_data.select("job","marital","education","default","housing","loan","month","poutcome")
Categorical_features.describe().show()
print(Categorical_features.show(5))
"""Pair Plots
"""
import pandas as pd
from pandas.plotting._misc import scatter_matrix
numeric_data = bank_data.select("age","balance","day","duration","campaign","pdays","previous").toPandas()
axs = pd.plotting.scatter_matrix(numeric_data, figsize=(8, 8));
n = len(numeric_data.columns)
for i in range(n):
v = axs[i, 0]
v.yaxis.label.set_rotation(0)
v.yaxis.label.set_ha('right')
v.set_yticks(())
h = axs[n-1, i]
h.xaxis.label.set_rotation(90)
h.set_xticks(())
"""
Correlations between independent variables
"""
from pyspark.sql.functions import when
#Replace yes with 1 and no with 0
bank_data= bank_data.withColumn('deposit', when(bank_data['target']=='yes',1).otherwise(0))
numeric_data = bank_data.select("age","balance","day","duration","campaign","pdays","previous","deposit").toPandas()
import seaborn as sns
import numpy as np
matrix = np.triu(numeric_data.corr())
sns.heatmap(numeric_data.corr(), annot=True, mask=matrix)
"""Show number of customers that have signed up term deposit vs those
that did not OR Number of respondents by the deposit subscription results
"""
numOfSignUps = bank_data.groupBy("deposit").count().show()
numOfSignUps = bank_data.groupBy("target").count().collect()
numOfSignUps_plot = bank_data.rdd.map(lambda x: x[16]).countByValue()
plt.bar(list(numOfSignUps_plot.keys()), numOfSignUps_plot.values(), color='g')
"""Percentage of category of deposit
"""
import pyspark.sql.functions as F
bank_data.groupby('target').agg(
(F.count('target')).alias('count'),
(F.count('target') / bank_data.count()).alias('percentage')
).show()
"""
#Number of respondants by Job Type
"""
job_type = bank_data.groupBy("job").count().show()
plt.figure(figsize=(20,10))
job_type_plot = bank_data.rdd.map(lambda x: x[1]).countByValue()
plt.bar(list(job_type_plot.keys()), job_type_plot.values(), color='g')
plt.xticks(rotation=90)
"""
Number of respondents by marital status
"""
marital_status = bank_data.groupBy("marital").count().show()
plt.figure(figsize=(20,10))
marital_status_plot = bank_data.rdd.map(lambda x: x[2]).countByValue()
plt.bar(list(marital_status_plot.keys()), marital_status_plot.values(), color='green')
"""Number of respondents by level of education
"""
education = bank_data.groupBy("education").count().show()
plt.figure(figsize=(20,10))
education_plot = bank_data.rdd.map(lambda x: x[3]).countByValue()
plt.bar(list(education_plot.keys()), education_plot.values(), color='green')
"""
Number of respondents by the type of contact
"""
contact = bank_data.groupBy("contact").count().show()
plt.figure(figsize=(20,10))
contact_plot = bank_data.rdd.map(lambda x: x[8]).countByValue()
plt.bar(list(contact_plot .keys()), contact_plot .values(), color='green')
"""
Number of respondents by the outcome of the previous campaign
"""
poutcome = bank_data.groupBy("poutcome").count().show()
plt.figure(figsize=(20,10))
poutcome_plot = bank_data.rdd.map(lambda x: x[15]).countByValue()
plt.bar(list(poutcome_plot .keys()), poutcome_plot .values(), color='g')
"""Number of respondents by month
"""
month = bank_data.groupBy("month").count().show()
plt.figure(figsize=(20,10))
month_plot = bank_data.rdd.map(lambda x: x[10]).countByValue()
plt.bar(list(month_plot .keys()), month_plot .values(), color='green')
# Percentage by month
import pyspark.sql.functions as F
bank_data.groupby('month').agg(
(F.count('target')).alias('count'),
(F.count('target') / bank_data.count()).alias('percentage')
).show()
"""
Age Categories
"""
age = bank_data.groupBy("age").count().show()
plt.figure(figsize=(20,10))
age_plot = bank_data.rdd.map(lambda x: x[0]).countByValue()
plt.bar(list(age_plot .keys()), age_plot .values(), color='green')
""" Campaign """
campaign = bank_data.groupBy("campaign").count().show()
plt.figure(figsize=(20,10))
campaign_plot = bank_data.rdd.map(lambda x: x[12]).countByValue()
plt.bar(list(campaign_plot .keys()), campaign_plot .values(), color='green')
#Replace yes with 1 and no with 0
from pyspark.sql import functions as F
from pyspark.sql.functions import when
bank_data= bank_data.withColumn('deposit', when(bank_data['target']=='yes',1).otherwise(0))
bank_data = bank_data.drop('target')
bank_data.show()
"""Bivariate Analysis
"""
# Job versus deposit
bank_data.crosstab('job', 'deposit').show()
import pandas as pd
job = bank_data.crosstab('job', 'deposit').show()
# deposit versus marital status
marital_deposit=bank_data.crosstab('marital', 'deposit').show()
# Crosstabing categorical variables with the label
import pyspark.sql.functions as f
categorical_features = [t[0] for t in bank_data.dtypes if t[1] != 'int']
for f in categorical_features:
bank_data.crosstab(f,'Deposit').show()
# count of marital status versus the deposit
bank_data.groupBy("marital","deposit").count().show()
"""
Age Categories Versus deposit
"""
age = bank_data.groupBy("age","deposit").count().show()
plt.figure(figsize=(20,10))
age_plot = bank_data.rdd.map(lambda x: x[0]).countByValue()
plt.bar(list(age_plot .keys()), age_plot .values(), color='green')
""" Compaign Versus Target
"""
compaign = bank_data.groupBy("campaign","deposit").count().show()
plt.figure(figsize=(20,10))
compaign_plot = bank_data.rdd.map(lambda x: x[11]).countByValue()
plt.bar(list(compaign_plot .keys()), compaign_plot .values(), color='green')
#Down sampling
major_df = bank_data.filter(col("deposit") == 0)
minor_df = bank_data.filter(col("deposit") == 1)
ratio = int(major_df.count()/minor_df.count())
print("ratio: {}".format((ratio),2))
sampled_majority_df = major_df.sample(False, 1/ratio)
combined_df_2 = sampled_majority_df.unionAll(minor_df)
combined_df_2.show()
combined_df_2.count()
combined_df_2.groupby('deposit').count().show()