-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataWrangling.py
231 lines (183 loc) · 7.3 KB
/
dataWrangling.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
#Clean the data and generate sample data
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
import matplotlib.pyplot as plt
import plotly.plotly as py
import seaborn as sns
import plotly.graph_objs as go
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import xlrd
from sklearn.cluster import KMeans
import pylab as pl
import sys
df = pd.read_excel("https://archive.ics.uci.edu/ml/machine-learning-databases/00352/Online%20Retail.xlsx")
print("Frames\n",df.head())
#DATA WRANGLING
#dropping NaN data rows
df = df.dropna()
#dropping description column
df=df.drop(columns=['Description'])
df['Amount'] = df.UnitPrice * df.Quantity
#removing unit price
df=df.drop(columns=['UnitPrice'])
df['date'] = [d.date() for d in df['InvoiceDate']]
df['time'] = [d.time() for d in df['InvoiceDate']]
#CLEANING CUSTOMERS
print("\n\nCleaning")
#dropping NaN values in CustomerID
df = df[np.isfinite(df['CustomerID'])] #df.loc[df['InvoiceNo']==573174] reference for NaN value
#removing customers with less than 1Re of transaction
df=df[(df['Amount'] >=1)]
#grouping by countries and adding purchasing percentage on basis of quantity
group_country = df.groupby(['Country'],as_index=False).sum()
group_country = group_country.drop(columns=['CustomerID'])
group_country.sort_values('Quantity',ascending=False,inplace=True)
total_purchased = group_country['Quantity'].sum()
group_country['Buy_perc']=(group_country['Quantity']/total_purchased)*100
print("\n\nBuyers % Plotting")
#Buy_Perc plotting
country=list(group_country['Country'])
Cust_id=list(group_country['Buy_perc'])
plt.figure(figsize=(12,8))
sns.barplot(country, Cust_id, alpha=1)
plt.xticks(rotation='60')
plt.show()
#analysing country with highest purchasing
x=group_country.where(group_country['Buy_perc']==group_country['Buy_perc'].max())
x=x['Country'].dropna().get_values().tolist()[0]
dfcur = df.where(df.Country == x)
dfcur=dfcur.dropna()
#first time buyers
x=dfcur.groupby('CustomerID',).count()
no_customers= x['InvoiceNo'].count()
no_first_timers=x.where(x['InvoiceNo']==1).dropna().count()['InvoiceNo']
print('Percentage of new customers purchasing for the first time',(no_first_timers*100/no_customers))
objects = ('First/New Customers', 'Existing')
y_pos = np.arange(len(objects))
perc = [no_first_timers,no_customers]
plt.bar(y_pos, perc, align='center', alpha=0.5)
plt.xticks(y_pos, objects)
plt.ylabel('Percentage')
plt.title('Customers')
plt.show()
#Converting to date to shortened string formate %y(2)%m
dfcur['dateS']=dfcur['date'].apply(lambda x: x.strftime('%y%m'))
#print('Far date: ',dfcur['dateS'].min(),"\tRecent: ",dfcur['dateS'].max())
#RMF MODEL
print("\n\nRecency Analysis")
#---- Recency Analysis------
def recency(row):
if int(row['dateS']) > 1109:
val = 5
elif int(row['dateS']) <= 1109 and int(row['dateS']) > 1106:
val = 4
elif int(row['dateS']) <= 1106 and int(row['dateS']) > 1103:
val = 3
elif int(row['dateS']) <= 1103 and int(row['dateS']) > 1101:
val = 2
else:
val = 1
return val
dfcur['Recency_val'] = dfcur.apply(recency, axis=1)
dfcur.head()
#table with just recency values
rec_df = dfcur
rec_df = rec_df.drop(columns=['Quantity','InvoiceNo','StockCode','InvoiceDate','Country','Amount','date','time','dateS'])
recencyTable = rec_df.drop_duplicates( keep=False)
rec_df.head()
plt.figure(figsize=(12,8))
sns.countplot(x="Recency_val", data=rec_df)
plt.ylabel('Count', fontsize=12)
plt.xlabel('Recency_Value', fontsize=12)
plt.xticks(rotation='vertical')
plt.title("Frequency of Recency_Flag", fontsize=15)
plt.show()
print("\tRecency Analysis\n",rec_df.groupby('Recency_val',as_index=False).count())
print("\n\nFrequency Analysis")
#------Frequency Analysis-------
freq_df = dfcur[['Country','InvoiceNo','CustomerID']].drop_duplicates()
freq_count = freq_df.groupby(['Country','CustomerID'],as_index=False)[['InvoiceNo']].count()
freq_count.head()
unique_invoice=freq_df[['InvoiceNo']].drop_duplicates()
unique_invoice.head()
#Dividing the dataframe into 5 bands
unique_invoice['fband']=pd.qcut(unique_invoice['InvoiceNo'],5)
unique_invoice.head()
freqBandTable = unique_invoice[['fband']].drop_duplicates().reset_index()
freqBandTable
def frequ(row):
if row['InvoiceNo'] <= 13:
val = 1
elif row['InvoiceNo'] > 13 and row['InvoiceNo']<=24:
val =2
elif row['InvoiceNo']>24 and row['InvoiceNo']<=35:
val = 3
elif row['InvoiceNo']>35 and row['InvoiceNo']<=60:
val = 4
else:
val = 5
return val
freq_count['freq_val'] = freq_count.apply(frequ,axis=1)
freq_count.head()
plt.figure(figsize=(12,8))
sns.countplot(x="freq_val", data=freq_count)
plt.ylabel('Count', fontsize=12)
plt.xlabel('Frequency_Value', fontsize=12)
plt.xticks(rotation='vertical')
plt.title("Frequency Analysis", fontsize=15)
plt.show()
print("\tFrequency Analysis\n",freq_count.groupby('freq_val',as_index=False).count())
print("\n\nMoentary Analysis")
#----MonetaryValue Analysis------
#monetary value for each country
monetary_df_countries = df.groupby(['Country','CustomerID'],as_index=False)['Amount'].sum()
monetary_df_countries.head()
ctry=list(monetary_df_countries['Country'])
amt=list(monetary_df_countries['Amount'])
plt.figure(figsize=(12,8))
sns.barplot(ctry, amt, alpha=1)
plt.xticks(rotation='60')
plt.show()
print("\tMonetary Analysis Countrywise\n",monetary_df_countries.groupby('Country',as_index=False)[['Amount']].sum().sort_values('Amount', ascending=False).reset_index(drop=True).head())
#getting monetary band
monprice_df = dfcur[['CustomerID','Amount']].drop_duplicates()
monprice_df = monprice_df.groupby(['CustomerID'],as_index=False)[['Amount']].sum()
monprice_df['monetary'] = pd.qcut(monprice_df['Amount'],5)
monprice_df=monprice_df.sort_values(by=['monetary'])
monetary_band = monprice_df[['monetary']].drop_duplicates().reset_index(drop=True)
monetary_band
x=monetary_band['monetary'].get_values().tolist()
x
def mon(row):
if row['Amount'] <= x[0].right:
val = 1
elif row['Amount']> x[0].right and row['Amount']<=x[1].right:
val = 2
elif row['Amount']> x[1].right and row['Amount']<=x[2].right:
val = 3
elif row['Amount']> x[2].right and row['Amount']<=x[3].right:
val = 4
else:
val = 5
return val
monprice_df['Mont_val'] = monprice_df.apply(mon,axis=1)
monprice_df.head()
plt.figure(figsize=(12,8))
sns.countplot(x="Mont_val", data=monprice_df)
plt.ylabel('Count', fontsize=12)
plt.xlabel('Monetary_Value', fontsize=12)
plt.xticks(rotation='vertical')
plt.title("Monetary Analysis", fontsize=15)
plt.show()
print("\tMonetary Analysis\n",monprice_df.groupby(['Mont_val'],as_index=False)['monetary'].count())
#fetching the sample with RMF values
uk_df=pd.merge(rec_df,freq_count)
uk_df=pd.merge(uk_df,monprice_df)
sample_df=uk_df
sample_df=sample_df.drop_duplicates().reset_index(drop=True)
sample_df = sample_df.drop(columns=['Country','InvoiceNo','monetary'])
sample_df.head()
sample_df.to_csv('sample_data.csv')