-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgenerateplots.py
562 lines (460 loc) · 22.3 KB
/
generateplots.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
import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import datetime as dt
pd.options.mode.chained_assignment = None
################################################################
# Import CSV Data into Pandas DataFrames #
################################################################
training_df = pd.read_csv("data/train.csv")
store_df = pd.read_csv("data/store.csv")
# print(training_df.info())
# print(store_df.info())
################################################################
# Process Data #
################################################################
def is_nan(val):
return val != val
def less_than_ten(val):
if int(val) < 10:
return "0" + val
else:
return val
############################################
# training_df #
############################################
# Create "Year", "Month" & "DayOfMonth" columns
training_df["Year"] = training_df["Date"].apply(lambda x: dt.datetime.strptime(x, "%Y-%m-%d").year)
training_df["Month"] = training_df["Date"].apply(lambda x: dt.datetime.strptime(x, "%Y-%m-%d").month)
training_df["DayOfMonth"] = training_df["Date"].apply(lambda x: dt.datetime.strptime(x, "%Y-%m-%d").day)
# Create "YearMonth" column
training_df["YearMonth"] = training_df["Date"].apply(lambda x: str(dt.datetime.strptime(x, "%Y-%m-%d").year) + "-" + less_than_ten(str(dt.datetime.strptime(x, "%Y-%m-%d").month)))
# "StateHoliday" has values "0" & 0
training_df["StateHoliday"].loc[training_df["StateHoliday"] == 0] = "0"
# Create "StateHolidayBinary" column
training_df["StateHolidayBinary"] = training_df["StateHoliday"].map({0: 0, "0": 0, "a": 1, "b": 1, "c": 1})
############################################
# training_df_open #
############################################
# Create DataFrame for only open days
training_df_open = training_df[training_df["Open"] != 0]
# Drop "Open" column
training_df_open.drop(["Open"], axis=1, inplace=True)
############################################
# store_df #
############################################
# Add "AvgSales" & "AvgCustomers" columns to store_df
avg_sales_customers = training_df_open.groupby("Store")[["Sales", "Customers"]].mean()
avg_sales_customers_df = DataFrame({"Store": avg_sales_customers.index, "AvgSales": avg_sales_customers["Sales"], "AvgCustomers": avg_sales_customers["Customers"]}, columns=["Store", "AvgSales", "AvgCustomers"])
store_df = pd.merge(avg_sales_customers_df, store_df, on="Store")
# Add "AvgSalesPerCustomer" column to store_df
store_tot_sales = training_df.groupby([training_df["Store"]])["Sales"].sum()
store_tot_custs = training_df.groupby([training_df["Store"]])["Customers"].sum()
store_salespercust = store_tot_sales / store_tot_custs
store_df = pd.merge(store_df, store_salespercust.reset_index(name="AvgSalesPerCustomer"), on="Store")
# Fill NaN values in store_df for "CompetitionDistance" = 0 (since no record exists where "CD" = NaN & "COS[Y/M]" = !NaN)
store_df["CompetitionDistance"][is_nan(store_df["CompetitionDistance"])] = 0
################################################################
# Plot Data #
################################################################
############################################
# "Open" Data Field #
############################################
# Generate plot for No. of Open or Closed Stores (by Day Of Week)
fig, (axis1) = plt.subplots(1, 1, figsize=(15, 8))
sns.countplot(x="Open", hue="DayOfWeek", data=training_df, palette="husl", ax=axis1)
fig.tight_layout()
fig.savefig("plots/No. of Open or Closed Stores (by Day Of Week).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted No. of Open or Closed Stores (by Day Of Week)")
############################################
# "Date" Data Field & Derivatives #
############################################
# Generate plots for Avg. Sales & Percentage Change (by Year-Month)
average_sales = training_df.groupby("YearMonth")["Sales"].mean()
pct_change_sales = training_df.groupby("YearMonth")["Sales"].sum().pct_change()
fig, (axis1, axis2) = plt.subplots(2, 1, sharex=True, figsize=(15, 16))
ax1 = average_sales.plot(legend=True, ax=axis1, marker="o", title="Average Sales")
ax1.set_xticks(range(len(average_sales)))
ax1.set_xticklabels(average_sales.index.tolist(), rotation=90)
ax2 = pct_change_sales.plot(legend=True, ax=axis2, marker="o", rot=90, colormap="summer", title="Sales Percent Change")
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Percentage Change (by Year-Month).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Percentage Change (by Year-Month)")
# Generate plots for Avg. Sales & Customers (by Year)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="Year", y="Sales", data=training_df, ax=axis1, ci=None)
sns.barplot(x="Year", y="Customers", data=training_df, ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by Year).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by Year)")
# Generate plots for Avg. Sales & Customers (by Month)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="Month", y="Sales", data=training_df, ax=axis1, ci=None)
sns.barplot(x="Month", y="Customers", data=training_df, ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by Month).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by Month)")
# Generate plots for Avg. Sales & Customers (by Day of Month)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="DayOfMonth", y="Sales", data=training_df, ax=axis1, ci=None)
sns.barplot(x="DayOfMonth", y="Customers", data=training_df, ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by Day Of Month).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by Day Of Month)")
############################################
# "DayOfWeek" Data Field #
############################################
# Generate plots for Avg. Sales & Customers (by Day of Week)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="DayOfWeek", y="Sales", data=training_df, order=[1, 2, 3, 4, 5, 6, 7], ax=axis1, ci=None)
sns.barplot(x="DayOfWeek", y="Customers", data=training_df, order=[1, 2, 3, 4, 5, 6, 7], ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by Day of Week).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by Day of Week)")
# Generate plots for Avg. Sales & Customers (by Day of Week for Open Stores)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="DayOfWeek", y="Sales", data=training_df_open, order=[1, 2, 3, 4, 5, 6, 7], ax=axis1, ci=None)
sns.barplot(x="DayOfWeek", y="Customers", data=training_df_open, order=[1, 2, 3, 4, 5, 6, 7], ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by Day of Week for Open Stores).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by Day of Week for Open Stores)")
############################################
# "Promo" Data Field #
############################################
# Generate plots for Avg. Sales & Customers (by Promo)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="Promo", y="Sales", data=training_df, ax=axis1, ci=None)
sns.barplot(x="Promo", y="Customers", data=training_df, ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by Promo).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by Promo)")
# Generate plots for Avg. Sales & Customers (by Promo for Open Stores)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="Promo", y="Sales", data=training_df_open, ax=axis1, ci=None)
sns.barplot(x="Promo", y="Customers", data=training_df_open, ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by Promo for Open Stores).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by Promo for Open Stores)")
############################################
# "StateHoliday" Data Field & Derivatives #
############################################
# Generate plot for No. of State Holidays
fig, (axis1) = plt.subplots(1, 1, figsize=(15, 8))
sns.countplot(x="StateHoliday", data=training_df)
fig.tight_layout()
fig.savefig("plots/No. of State Holidays.png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted No. of State Holidays")
# Generate plots for Avg. Sales & Customers (by State Holiday Binary)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="StateHolidayBinary", y="Sales", data=training_df, ax=axis1, ci=None)
sns.barplot(x="StateHolidayBinary", y="Customers", data=training_df, ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by State Holiday Binary).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by State Holiday Binary)")
# Generate plots for Avg. Sales & Customers (by State Holiday Binary for Open Stores)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="StateHolidayBinary", y="Sales", data=training_df_open, ax=axis1, ci=None)
sns.barplot(x="StateHolidayBinary", y="Customers", data=training_df_open, ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by State Holiday Binary for Open Stores).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by State Holiday Binary for Open Stores)")
# Generate plots for Avg. Sales & Customers (by State Holiday)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="StateHoliday", y="Sales", data=training_df, ax=axis1, ci=None)
mask = (training_df["StateHoliday"] != "0") & (training_df["Sales"] > 0)
sns.barplot(x="StateHoliday", y="Sales", data=training_df[mask], ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by State Holiday).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by State Holiday)")
# Generate plots for Avg. Sales & Customers (by State Holiday for Open Stores)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="StateHoliday", y="Sales", data=training_df_open, ax=axis1, ci=None)
mask = (training_df_open["StateHoliday"] != "0") & (training_df_open["Sales"] > 0)
sns.barplot(x="StateHoliday", y="Sales", data=training_df_open[mask], ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by State Holiday for Open Stores).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by State Holiday for Open Stores)")
############################################
# "SchoolHoliday" Data Field #
############################################
# Generate plot for No. of School Holidays
fig, (axis1) = plt.subplots(1, 1, figsize=(15, 8))
sns.countplot(x="SchoolHoliday", data=training_df)
fig.tight_layout()
fig.savefig("plots/No. of School Holidays.png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted No. of School Holidays")
# Generate plots for Avg. Sales & Customers (by School Holiday)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="SchoolHoliday", y="Sales", data=training_df, ax=axis1, ci=None)
sns.barplot(x="SchoolHoliday", y="Customers", data=training_df, ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by School Holiday).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by School Holiday)")
# Generate plots for Avg. Sales & Customers (by School Holiday for Open Stores)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="SchoolHoliday", y="Sales", data=training_df_open, ax=axis1, ci=None)
sns.barplot(x="SchoolHoliday", y="Customers", data=training_df_open, ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by School Holiday for Open Stores).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by School Holiday for Open Stores)")
############################################
# "Sales" Data Field #
############################################
# Generate plot for Frequency of Sales Values
fig, (axis1) = plt.subplots(1, 1, figsize=(15, 8))
training_df["Sales"].plot(kind="hist", bins=70, xlim=(0, 20000), ax=axis1)
fig.tight_layout()
fig.savefig("plots/Frequency of Sales Values.png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Frequency of Sales Values")
# Generate plot for Frequency of Sales Values (for Open Stores)
fig, (axis1) = plt.subplots(1, 1, figsize=(15, 8))
training_df_open["Sales"].plot(kind="hist", bins=70, xlim=(0, 20000), ax=axis1)
fig.tight_layout()
fig.savefig("plots/Frequency of Sales Values (for Open Stores).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Frequency of Sales Values (for Open Stores)")
############################################
# "Customers" Data Field #
############################################
# Generate plot for Frequency of Customers Values
fig, (axis1) = plt.subplots(1, 1, figsize=(15, 8))
training_df["Customers"].plot(kind="hist", bins=70, xlim=(0, 4000), ax=axis1)
fig.tight_layout()
fig.savefig("plots/Frequency of Customers Values.png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Frequency of Customers Values")
# Generate plot for Frequency of Customers Values (for Open Stores)
fig, (axis1) = plt.subplots(1, 1, figsize=(15, 8))
training_df_open["Customers"].plot(kind="hist", bins=70, xlim=(0, 4000), ax=axis1)
fig.tight_layout()
fig.savefig("plots/Frequency of Customers Values (for Open Stores).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Frequency of Customers Values (for Open Stores)")
############################################
# "AvgSales" Data Field #
############################################
fig, axis1 = plt.subplots(1, 1, figsize=(15, 8))
sns.barplot(x="Store", y="AvgSales", data=store_df, ax=axis1, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales (by Store).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales (by Store)")
############################################
# "AvgCustomers" Data Field #
############################################
fig, axis1 = plt.subplots(1, 1, figsize=(15, 8))
sns.barplot(x="Store", y="AvgCustomers", data=store_df, ax=axis1, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Customers (by Store).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Customers (by Store)")
############################################
# "AvgSalesPerCustomer" Data Field #
############################################
fig, axis1 = plt.subplots(1, 1, figsize=(15, 8))
sns.barplot(x="Store", y="AvgSalesPerCustomer", data=store_df, ax=axis1, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales per Customer (by Store).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales per Customer (by Store)")
############################################
# "StoreType" Data Field #
############################################
# Generate plot for No. Of Stores (by Store Type)
fig, (axis1) = plt.subplots(1, 1, figsize=(15, 8))
sns.countplot(x="StoreType", data=store_df, order=["a", "b", "c", "d"])
fig.tight_layout()
fig.savefig("plots/No. Of Stores (by Store Type).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted No. Of Stores (by Store Type)")
# Generate plot for Avg. Sales & Customers (by Store Type)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="StoreType", y="AvgSales", data=store_df, order=["a", "b", "c", "d"], ax=axis1, ci=None)
sns.barplot(x="StoreType", y="AvgCustomers", data=store_df, order=["a", "b", "c", "d"], ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by Store Type).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by Store Type)")
# Generate plot for Avg. Sales per Customer (by Store Type)
fig, axis1 = plt.subplots(1, 1, figsize=(15, 8))
sns.barplot(x="StoreType", y="AvgSalesPerCustomer", order=["a", "b", "c", "d"], data=store_df, ax=axis1, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales per Customer (by Store Type).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales per Customer (by Store Type)")
############################################
# "Assortment" Data Field #
############################################
# Generate plot for No. Of Stores (by Assortment)
fig, (axis1) = plt.subplots(1, 1, figsize=(15, 8))
sns.countplot(x="Assortment", data=store_df, order=["a", "b", "c"])
fig.tight_layout()
fig.savefig("plots/No. Of Stores (by Assortment).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted No. Of Stores (by Assortment)")
# Generate plot for Avg. Sales & Customers (by Assortment)
fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
sns.barplot(x="Assortment", y="AvgSales", data=store_df, order=["a", "b", "c"], ax=axis1, ci=None)
sns.barplot(x="Assortment", y="AvgCustomers", data=store_df, order=["a", "b", "c"], ax=axis2, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales & Customers (by Assortment).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales & Customers (by Assortment)")
# Generate plot for Avg. Sales per Customer (by Assortment)
fig, axis1 = plt.subplots(1, 1, figsize=(15, 8))
sns.barplot(x="Assortment", y="AvgSalesPerCustomer", order=["a", "b", "c"], data=store_df, ax=axis1, ci=None)
fig.tight_layout()
fig.savefig("plots/Avg. Sales per Customer (by Assortment).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Avg. Sales per Customer (by Assortment)")
############################################
# "Promo2" Data Field #
############################################
# Generate plot for No. Of Stores (by Promo2)
fig, (axis1) = plt.subplots(1, 1, figsize=(15, 8))
sns.countplot(x="Promo2", data=store_df)
fig.tight_layout()
fig.savefig("plots/No. Of Stores (by Promo2).png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted No. Of Stores (by Promo2)")
# # Generate plot for Avg. Sales & Customers (by Promo2)
# fig, (axis1, axis2) = plt.subplots(1, 2, figsize=(15, 8))
# sns.barplot(x="Promo2", y="AvgSales", data=store_df, ax=axis1, ci=None)
# sns.barplot(x="Promo2", y="AvgCustomers", data=store_df, ax=axis2, ci=None)
# fig.tight_layout()
# fig.savefig("plots/Avg. Sales & Customers (by Promo2).png", dpi=fig.dpi)
# fig.clf()
# plt.close(fig)
# print("Plotted Avg. Sales & Customers (by Promo2)")
############################################
# "CompetitionDistance" Data Field #
############################################
# Generate plot for CompetitionDistance vs. Avg. Sales
fig, (axis1) = plt.subplots(1, 1, figsize=(15, 8))
store_df.plot(kind="scatter", x="CompetitionDistance", y="AvgSales", ax=axis1)
fig.tight_layout()
fig.savefig("plots/Competition Distance vs. Avg. Sales.png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Competition Distance vs. Avg. Sales")
# Generate plot for CompetitionDistance vs. Avg. Customers
fig, (axis1) = plt.subplots(1, 1, figsize=(15, 8))
store_df.plot(kind="scatter", x="CompetitionDistance", y="AvgCustomers", ax=axis1)
fig.tight_layout()
fig.savefig("plots/Competition Distance vs. Avg. Customers.png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Competition Distance vs. Avg. Customers")
############################################
# "CompetitionSince[X]" Data Field #
############################################
store_id = 6
store_data = training_df_open[training_df_open["Store"] == store_id]
average_store_sales = store_data.groupby("YearMonth")["Sales"].mean()
y = store_df["CompetitionOpenSinceYear"].loc[store_df["Store"] == store_id].values[0]
m = store_df["CompetitionOpenSinceMonth"].loc[store_df["Store"] == store_id].values[0]
# Generate plot for Sales for Store <store_id>
fig, (axis1) = plt.subplots(1, 1, figsize=(15, 8))
ax = average_store_sales.plot(legend=True, marker="o", ax=axis1)
ax.set_xticks(range(len(average_store_sales)))
ax.set_xticklabels(average_store_sales.index.tolist(), rotation=90)
# Add vertical line where competition started
if y >= 2013 and y == y and m == m:
plt.axvline(x=((y - 2013) * 12) + (m - 1), linewidth=3, color="grey")
fig.tight_layout()
fig.savefig("plots/Effect of Competition on Store " + str(store_id) + ".png", dpi=fig.dpi)
fig.clf()
plt.close(fig)
print("Plotted Effect of Competition on Store " + str(store_id) + "")
###########################################
# Correlation of Features in training_df #
###########################################
# One-hot encoding of "DayOfWeek" & "StateHoliday" columns
training_df = pd.get_dummies(training_df, columns=["DayOfWeek", "StateHoliday"])
# Generate correlation matrix for training_df
corr = training_df.corr()
fig, (axis1) = plt.subplots(figsize=(15, 15))
sns.heatmap(corr, square=True, ax=axis1)
plt.yticks(rotation=0)
plt.xticks(rotation=90)
fig.tight_layout()
fig.savefig("plots/Correlation Matrix (training_df).png")
fig.clf()
plt.close(fig)
print("Plotted Correlation Matrix (training_df)")
############################################
# Correlation of Stores #
############################################
# Generate table for 1,115 stores and their total sales every month
store_piv = pd.pivot_table(training_df, values="Sales", index="YearMonth", columns=["Store"], aggfunc="sum")
# Generate correlation matrix for all stores
start_store = 1
end_store = 1115
fig, (axis1) = plt.subplots(1, 1, figsize=(100, 100))
sns.heatmap(store_piv[list(range(start_store, end_store + 1))].corr(), square=True, ax=axis1)
fig.tight_layout()
fig.savefig("plots/Correlation Matrix (All Stores).png")
fig.clf()
plt.close(fig)
print("Plotted Correlation Matrix (All Stores)")
# Generate correlation matrix for Stores 1 - 100
start_store = 1
end_store = 100
fig, (axis1) = plt.subplots(1, 1, figsize=(100, 100))
sns.heatmap(store_piv[list(range(start_store, end_store + 1))].corr(), square=True, ax=axis1)
fig.tight_layout()
fig.savefig("plots/Correlation Matrix (Stores 1 - 100).png")
fig.clf()
plt.close(fig)
print("Plotted Correlation Matrix (Stores 1 - 100)")