-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcharts.py
254 lines (223 loc) · 6.72 KB
/
charts.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
""" Prepare data for charts """
import pandas as pd
def gen_amt_data(transactions: pd.DataFrame) -> list:
"""
Create a list of amt values for fraudulent and non-fraudulent transactions
Args:
- transactions: the transactions dataframe
Returns:
- a list of two dictionaries containing the data for the two histograms
"""
amt_fraud = transactions[transactions["fraud"]]["amt"]
amt_no_fraud = transactions[~transactions["fraud"]]["amt"]
amt_data = [
{"Amount ($)": list(amt_no_fraud)},
{"Amount ($)": list(amt_fraud)},
]
return amt_data
def gen_gender_data(transactions: pd.DataFrame) -> pd.DataFrame:
"""
Create a dataframe containing the percentage of fraudulent transactions
per gender
Args:
- transactions: the transactions dataframe
Returns:
- the resulting dataframe
"""
male_fraud_percentage = len(
transactions[transactions["fraud"]].loc[transactions["gender"] == "M"]
) / len(transactions[transactions["fraud"]])
female_fraud_percentage = 1 - male_fraud_percentage
male_not_fraud_percentage = len(
transactions[~transactions["fraud"]].loc[transactions["gender"] == "M"]
) / len(transactions[~transactions["fraud"]])
female_not_fraud_percentage = 1 - male_not_fraud_percentage
gender_data = pd.DataFrame(
{
"Fraudulence": ["Not Fraud", "Fraud"],
"Male": [male_not_fraud_percentage, male_fraud_percentage],
"Female": [female_not_fraud_percentage, female_fraud_percentage],
}
)
return gender_data
def gen_cat_data(transactions: pd.DataFrame) -> pd.DataFrame:
"""
Generates a dataframe with the percentage difference
between fraudulent and non-fraudulent transactions per category
Args:
- transactions: the transactions dataframe
Returns:
- the resulting dataframe
"""
categories = transactions["category"].unique()
fraud_categories = [
len(
transactions[transactions["fraud"]].loc[
transactions["category"] == category
]
)
for category in categories
]
fraud_categories_norm = [
category / len(transactions[transactions["fraud"]])
for category in fraud_categories
]
not_fraud_categories = [
len(
transactions[~transactions["fraud"]].loc[
transactions["category"] == category
]
)
for category in categories
]
not_fraud_categories_norm = [
category / len(transactions[~transactions["fraud"]])
for category in not_fraud_categories
]
diff_categories = [
fraud_categories_norm[i] - not_fraud_categories_norm[i]
for i in range(len(categories))
]
cat_data = pd.DataFrame(
{
"Category": categories,
"Difference": diff_categories,
}
)
cat_data = cat_data.sort_values(by="Difference", ascending=False)
return cat_data
def gen_age_data(transactions: pd.DataFrame) -> pd.DataFrame:
"""
Generates a dataframe with the percentage of fraudulent transactions
per age
Args:
- transactions: the transactions dataframe
Returns:
- the resulting dataframe
"""
age = range(111)
fraud_age = [
len(transactions[transactions["fraud"]].loc[transactions["age"] == age])
/ len(transactions[transactions["fraud"]])
for age in age
]
not_fraud_age = [
len(transactions[~transactions["fraud"]].loc[transactions["age"] == age])
/ len(transactions[~transactions["fraud"]])
for age in age
]
age_data = pd.DataFrame(
{
"Age": age,
"Fraud": fraud_age,
"Not Fraud": not_fraud_age,
}
)
return age_data
def gen_hour_data(transactions: pd.DataFrame) -> pd.DataFrame:
"""
Generates a dataframe with the percentage of fraudulent transactions
per hour
Args:
- transactions: the transactions dataframe
Returns:
- the resulting dataframe
"""
hours = range(1, 25)
fraud_hours = [
len(transactions[transactions["fraud"]].loc[transactions["hour"] == hour])
/ len(transactions[transactions["fraud"]])
for hour in hours
]
not_fraud_hours = [
len(transactions[~transactions["fraud"]].loc[transactions["hour"] == hour])
/ len(transactions[~transactions["fraud"]])
for hour in hours
]
hour_data = pd.DataFrame(
{
"Hour": hours,
"Fraud": fraud_hours,
"Not Fraud": not_fraud_hours,
}
)
return hour_data
def gen_day_data(transactions: pd.DataFrame) -> pd.DataFrame:
"""
Generates a dataframe with the percentage of fraudulent transactions
per weekday
Args:
- transactions: the transactions dataframe
Returns:
- the resulting dataframe
"""
days = range(7)
days_names = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
]
fraud_days = [
len(transactions[transactions["fraud"]].loc[transactions["day"] == day])
/ len(transactions[transactions["fraud"]])
for day in days
]
not_fraud_days = [
len(transactions[~transactions["fraud"]].loc[transactions["day"] == day])
/ len(transactions[~transactions["fraud"]])
for day in days
]
day_data = pd.DataFrame(
{
"Day": days_names,
"Fraud": fraud_days,
"Not Fraud": not_fraud_days,
}
)
return day_data
def gen_month_data(transactions: pd.DataFrame) -> pd.DataFrame:
"""
Generates a dataframe with the percentage of fraudulent transactions
per month
Args:
- transactions: the transactions dataframe
Returns:
- the resulting dataframe
"""
months = range(1, 13)
months_names = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
]
fraud_months = [
len(transactions[transactions["fraud"]].loc[transactions["month"] == month])
/ len(transactions[transactions["fraud"]])
for month in months
]
not_fraud_months = [
len(transactions[~transactions["fraud"]].loc[transactions["month"] == month])
/ len(transactions[~transactions["fraud"]])
for month in months
]
month_data = pd.DataFrame(
{
"Month": months_names,
"Fraud": fraud_months,
"Not Fraud": not_fraud_months,
}
)
return month_data