-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcard_tracker.py
More file actions
220 lines (165 loc) · 6.56 KB
/
Copy pathcard_tracker.py
File metadata and controls
220 lines (165 loc) · 6.56 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
import datetime
import numpy as np
# Linked list
class Node:
def __init__(self, v=None):
# pandas uses the same reference in apply :), discovered the hard way
self.v = v.copy()
self.n = None
# Class used to store context for a particular card
class CardTracker:
# Names of the features
colnames = [
"Txn_Amount_Month",
"Average_3Months",
"Average_DailyMonth",
"Amount_SameDay",
"Number_Same_Day",
"Amount_Currency_Type_Month",
"Number_Currency_Type_Month",
"Amount_Country_Type_Month",
"Number_Country_Type_Month",
"Unique emails",
"Unique ips",
"New email",
"New ip"
]
def __init__(self, id) -> None:
self.id = id
self._last_30_days: Node = None
self._last_90_days: Node = None
self._last_day: Node = None
self._last: Node = None
# 1 day
self._amount_1 = 0
self._count_1 = 0
# 30 days
self._amount_30 = 0
self._count_30 = 0
self._currency_amount_30 = {}
self._currency_count_30 = {}
self._country_amount_30 = {}
self._country_count_30 = {}
# 90 days
self._amount_90 = 0
self._count_90 = 0
self._emails = set()
self._ips = set()
# Consumes a line and outputs the features
def feed(self, entry):
current_ts = entry['creationdate']
self._move_queue(current_ts)
if entry['mail_id'] not in self._emails:
new_email = 1
self._emails.add(entry['mail_id'])
else:
new_email = 0
if entry['ip_id'] not in self._ips:
new_ip = 1
self._ips.add(entry['ip_id'])
else:
new_ip = 0
ret = [
# Txn amount over month
self._amount_30 / self._count_30 if self._count_30 != 0 else 0,
# Average over 3 months
self._amount_90 / 12,
# Average daily over month
self._amount_30 / 30,
# Amount same day
self._amount_1,
# Number same day
self._count_1,
# Amount currency type over month
self._currency_amount_30.get(entry['currencycode'], 0) / 30,
# Number currency type over month
self._currency_count_30.get(entry['currencycode'], 0),
# Amount country type over month
self._country_amount_30.get(entry['shoppercountrycode'], 0) / 30,
# Number country type over month
self._country_count_30.get(entry['shoppercountrycode'], 0),
# Number of unique emails used
len(self._emails),
# Number of unique ips used
len(self._ips),
new_email,
new_ip
]
# add it
if self._last is None:
self._last = self._last_day = self._last_30_days = self._last_90_days = Node(entry)
else:
self._last.n = Node(entry)
self._last = self._last.n
if self._last_day is None:
self._last_day = self._last
if self._last_30_days is None:
self._last_30_days = self._last
if self._last_90_days is None:
self._last_90_days = self._last
amount = entry['amount']
# 1
self._amount_1 += amount
self._count_1 += 1
# 30
self._amount_30 += amount
self._count_30 += 1
self._currency_amount_30[entry['currencycode']] = self._currency_amount_30.get(entry['currencycode'],
0) + amount
self._currency_count_30[entry['currencycode']] = self._currency_count_30.get(entry['currencycode'], 0) + 1
self._country_amount_30[entry['shoppercountrycode']] = self._country_amount_30.get(entry['shoppercountrycode'],
0) + amount
self._country_count_30[entry['shoppercountrycode']] = self._country_count_30.get(entry['shoppercountrycode'],
0) + 1
# 90
self._amount_90 += amount
self._count_90 += 1
return ret
# Dump the expired nodes
def _move_queue(self, current_ts):
# if we discard a node, subtract it's value from the cached sums
# 1
new_date = current_ts - np.timedelta64(1, 'D')
while self._last_day is not None and self._last_day.v['creationdate'] < new_date:
self._amount_1 -= self._last_day.v['amount']
self._count_1 -= 1
self._last_day = self._last_day.n
# 30
new_date = current_ts - np.timedelta64(30, 'D')
while self._last_30_days is not None and self._last_30_days.v['creationdate'] < new_date:
amount = self._last_30_days.v['amount']
self._amount_30 -= amount
self._count_30 -= 1
self._currency_amount_30[self._last_30_days.v['currencycode']] = self._currency_amount_30.get(
self._last_30_days.v['currencycode'], 0) - amount
self._currency_count_30[self._last_30_days.v['currencycode']] = self._currency_count_30.get(
self._last_30_days.v['currencycode'], 0) - 1
self._country_amount_30[self._last_30_days.v['shoppercountrycode']] = self._country_amount_30.get(
self._last_30_days.v['shoppercountrycode'],
0) - amount
self._country_count_30[self._last_30_days.v['shoppercountrycode']] = self._country_count_30.get(
self._last_30_days.v['shoppercountrycode'],
0) - 1
self._last_30_days = self._last_30_days.n
new_date = current_ts - np.timedelta64(90, 'D')
while self._last_90_days is not None and self._last_90_days.v['creationdate'] < new_date:
# 90
self._amount_90 -= self._last_90_days.v['amount']
self._count_90 -= 1
self._last_90_days = self._last_90_days.n
'''
def extract_feature(row, _hash):
key = row['card_id']
if key in _hash:
card_tracker = _hash[key]
else:
card_tracker = CardTracker(key)
_hash[key] = card_tracker
return card_tracker.feed(row)
def extract_features_from_data(df):
_hash = {}
features = df.apply(lambda e: extract_feature(e, _hash), axis=1)
return features
'''
if __name__ == "__main__":
print("Library module. No main function")