-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMarketing_Data_Exploration.py
313 lines (146 loc) · 5.32 KB
/
Marketing_Data_Exploration.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import matplotlib.pyplot as plt
plt.style.use('classic')
get_ipython().run_line_magic('matplotlib', 'inline')
import numpy as np
import pandas as pd
import seaborn as sns
sns.set()
# In[2]:
marketing = pd.read_csv('marketing_training.csv')
# In[3]:
marketing.head()
# In[4]:
marketing.shape
# In[5]:
print('Number of training examples: {0}'.format(marketing.shape[0]))
print('Number of features for each example: {0}'.format(marketing.shape[1]))
# In[6]:
marketing.info()
# In[7]:
#List of columns
pd.DataFrame(data = {'Feature Label': marketing.columns})
# In[ ]:
# # Visualizing Missing values
# In[9]:
import missingno as msno
# In[10]:
msno.matrix(marketing)
# We can see that there is pattern of missing values in custAge, schooling, day_of_week
# In[11]:
msno.bar(marketing)
# There are three columns that have missing values: custAge, schooling, day_of_week
# # Data Exploration
# In[14]:
# for generation of interactive data visualization
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
init_notebook_mode(connected=True)
labels = ["no", "yes"]
values = marketing["responded"].value_counts().values
trace = go.Pie(labels = labels, values = values)
layout = go.Layout(title = 'Distribution of Target Variable - Responded')
fig = go.Figure(data = [trace], layout = layout)
iplot(fig)
# As we can see, the target variable is highly skewed, with only 11.3% of responded said yes. This is something that we will have to keep in mind when evaluating the results of our predictions later on.
# We should also get some information on the categorical variables in this dataset. We can do this by subsetting the categorical feature columns.
# In[15]:
marketing.select_dtypes(include=["object_"]).columns
# In[16]:
import numpy as np
df1 = marketing.replace(np.nan, 'missing', regex=True)
# In[15]:
list(df1.select_dtypes(include=["object_"]).columns)
# In[17]:
for i in list(df1.select_dtypes(include=["object_"]).columns):
df1[i] =df1[i].astype('category')
# In[18]:
df1.select_dtypes(exclude=["number","bool_","object_"])
# In[19]:
list(df1.select_dtypes(include=["category"]).columns)[0]
# In[20]:
df1['custAge'] = df1['custAge'].astype('str')
# In[21]:
df1['custAge'][df1.custAge == 'missing'] = 0
# In[22]:
df1['custAge'] = df1['custAge'].astype('float')
# In[23]:
df1['custAge'][df1.custAge == 0] = df1.custAge.mean()
# In[24]:
df1[list(df1.select_dtypes(include=["category"]).columns)[1]].unique()
# In[24]:
for i in df1.select_dtypes(include=["category"]).columns:
print("Categories in column " + str(i) + str(df1[i].unique()))
print('')
# Below is a visualization of the distribution within each categorical feature. The legend on the right shows the labels provided each category.
# In[26]:
cat_columns =list(df1.select_dtypes(include=["category"]).columns)
cat_counts = df1[list(df1.select_dtypes(include=["category"]).columns)].apply(pd.value_counts)
trace = []
for i in range(cat_counts.shape[0]):
trace_temp = go.Bar(
x= np.asarray(cat_columns),
y= cat_counts.values[i],
name = cat_counts.index[i]
)
trace.append(trace_temp)
layout = go.Layout(
barmode = 'stack',
title = 'Distribution of Categorical Features'
)
fig = go.Figure(data = trace, layout = layout)
iplot(fig)
# In[27]:
misc_columns = list(df1.select_dtypes(include=["float_"]).columns)
misc_columns
# In[28]:
list(df1.select_dtypes(include=["float_"]).columns)[:-2]
# In[29]:
#create boxplots for 'ind' columns
ind_columns = list(df1.select_dtypes(include=["float_"]).columns)[:-2]
trace1 = []
for i in range(len(ind_columns)):
trace_temp = go.Box(
y= df1[ind_columns[i]],
name = ind_columns[i]
)
trace1.append(trace_temp)
layout1 = go.Layout(
title = 'Distribution of "numerical" Features'
)
fig1 = go.Figure(data = trace1, layout = layout1)
iplot(fig1)
# We can see from the distributions above that while some features have relatively small ranges while others have relatively larger ranges, it may be useful to consider normalization in order to improve the performance of our chosen model when it comes time to optimize our predictions.
#
#
# In[43]:
## Analyzing target variable
# In[44]:
with sns.axes_style('white'):
g = sns.factorplot("responded", data=df1, aspect=2,
kind="count", color='steelblue')
g.set_xticklabels(step=1)
# It is clear that the dataset is imbalanced
# In[47]:
for i in list(df1.select_dtypes(include=["category"]).columns):
with sns.axes_style('white'):
g = sns.factorplot(i, data=df1, aspect=4.0, kind='count',
hue='responded')
g.set_xticklabels(step=1)
# The dataset imbalance so there not a strong finding.But the people who said yes seems to have following traits:
# 1. They are in high profile jobs
# 2. Attained high education
# 3. Do not have loan
# In[30]:
ax = sns.boxplot(x="responded", y="custAge", data=df1)
# In[31]:
list(df1.select_dtypes(include=["float_"]).columns)[:-2]
# In[32]:
ax = sns.boxplot(x="responded", y='emp.var.rate', data=df1)
# In[55]:
ax = sns.boxplot(x="responded", y='cons.price.idx', data=df1)
# In[56]:
ax = sns.boxplot(x="responded", y='euribor3m', data=df1)
# In[ ]: