-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics_module.py
818 lines (548 loc) · 24.7 KB
/
statistics_module.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
import dataset_module
import math
import statistics
#---------------------------statistics_module a
def get_avg_by_user_id(user_id):
try:
transaction_data = dataset_module.get_transaction()
sum_amount = 0
count_amount = 0
for _data in transaction_data:
if _data['user_id'] == user_id:
sum_amount += float(_data['amount_transaction'])
count_amount += 1
if count_amount == 0:
raise ValueError("No transactions found for the given user ID")
avg = sum_amount / count_amount
print("Average amount: ", avg)
except Exception as e:
print("Error: ", str(e))
def get_avg_all():
try:
transaction_data = dataset_module.get_transaction()
sum_amount = 0
count_amount = 0
user_id = 0
for _data in transaction_data:
if user_id == 0:
user_id = _data['user_id']
if _data['user_id'] == user_id:
sum_amount += float(_data['amount_transaction'])
count_amount += 1
else:
if count_amount == 0:
raise ValueError("No transactions found for user ID: " + str(user_id))
avg = sum_amount / count_amount
print("User ID: ", user_id, " Average amount: ", avg)
user_id = _data['user_id']
sum_amount = float(_data['amount_transaction'])
count_amount = 1
if count_amount == 0:
raise ValueError("No transactions found for user ID: " + str(user_id))
avg = sum_amount / count_amount
print("User ID: ", user_id, " Average amount: ", avg)
except Exception as e:
print("Error: ", str(e))
#------------------------------------------------statistics_module a
#---------------------------statistics_module b
def get_mode(arr):
cnt = []
for i in arr:
cnt.append(arr.count(i))
uniq_cnt = []
for i in cnt:
if i not in uniq_cnt:
uniq_cnt.append(i)
if len(uniq_cnt) > 1:
m = []
for i in list(range(len(cnt))):
if cnt[i] == max(uniq_cnt):
m.append(arr[i])
mode = []
for i in m:
if i not in mode:
mode.append(i)
return mode
else:
return "(There is NO mode in the data set)"
def get_mode_of_transactions_by_user_id(user_id):
try:
transaction_data = dataset_module.get_transaction()
list_amounts = []
for _data in transaction_data:
if _data['user_id'] == user_id:
list_amounts.append(float(_data['amount_transaction']))
if not list_amounts:
raise ValueError("No transactions found for the given user ID")
# Calculate mode of transaction amounts
mode = get_mode(list_amounts)
print("User ID: ", user_id, " Mode: ", mode)
except Exception as e:
print("Error: ", str(e))
def get_mode_of_transactions_all():
try:
transaction_data = dataset_module.get_transaction()
list_amounts = []
user_id = 0
for _data in transaction_data:
if user_id == 0:
user_id = _data['user_id']
if _data['user_id'] == user_id:
list_amounts.append(float(_data['amount_transaction']))
else:
if not list_amounts:
raise ValueError("No transactions found for user ID: " + str(user_id))
mode = get_mode(list_amounts)
print("User ID: ", user_id, " Mode: ", mode)
user_id = _data['user_id']
list_amounts = [float(_data['amount_transaction'])]
if not list_amounts:
raise ValueError("No transactions found for user ID: " + str(user_id))
mode = get_mode(list_amounts)
print("User ID: ", user_id, " Mode: ", mode)
except Exception as e:
print("Error: ", str(e))
#------------------------------------------------statistics_module b
#---------------------------statistics_module C
def get_median(arr_list):
arr_list.sort()
count_arr = len(arr_list)
half = count_arr//2
if count_arr % 2 == 0:
return (arr_list[half-1] + arr_list[half]) / 2
else:
return arr_list[half]
def get_median_of_transactions_by_user_id(user_id):
try:
transaction_data = dataset_module.get_transaction()
list_amounts = []
for _data in transaction_data:
if _data['user_id'] == user_id:
list_amounts.append(float(_data['amount_transaction']))
if not list_amounts:
raise ValueError("No transactions found for user ID: " + str(user_id))
median = get_median(list_amounts)
print("User ID: ", user_id, " Median: ", median)
except Exception as e:
print("Error: ", str(e))
def get_median_of_transactions_all():
try:
transaction_data = dataset_module.get_transaction()
list_amounts = []
user_id = 0
for _data in transaction_data:
if user_id == 0:
user_id = _data['user_id']
if _data['user_id'] == user_id:
list_amounts.append(float(_data['amount_transaction']))
else:
if not list_amounts:
raise ValueError("No transactions found for user ID: " + str(user_id))
median = get_median(list_amounts)
print("User ID: ", user_id, " Median: ", median)
user_id = _data['user_id']
list_amounts = []
list_amounts.append(float(_data['amount_transaction']))
if not list_amounts:
raise ValueError("No transactions found for user ID: " + str(user_id))
median = get_median(list_amounts)
print("User ID: ", user_id, " Median: ", median)
except Exception as e:
print("Error: ", str(e))
#------------------------------------------------statistics_module C
#---------------------------statistics_module d
def get_Q1(count):
return (count+1)/4
def get_Q3(count):
return (3*(count+1))/4
def getnumber(num1,list_arr):
ret= 0
num = num1-1
if(num-int(num)>0):
return (list_arr[int(num)] + list_arr[int(num)+1])/2
else:
return list_arr[int(num)]
def get_interquartile_range_of_transactions_by_user_id(user_id):
try:
transaction_data = dataset_module.get_transaction()
list_amounts = []
for _data in transaction_data:
if _data['user_id'] == user_id:
list_amounts.append(float(_data['amount_transaction']))
if not list_amounts:
raise ValueError("No transactions found for user ID: " + str(user_id))
list_amounts.sort()
Q1_index = get_Q1(len(list_amounts))
Q3_index = get_Q3(len(list_amounts))
Q1_number = getnumber(Q1_index, list_amounts)
Q3_number = getnumber(Q3_index, list_amounts)
IQR = Q3_number - Q1_number
print("User ID: ", user_id, " Interquartile Range: ", IQR)
except Exception as e:
print("Error: ", str(e))
def get_interquartile_range_of_transactions_all():
try:
transaction_data = dataset_module.get_transaction()
list_amounts = []
user_id = 0
for _data in transaction_data:
if user_id == 0:
user_id = _data['user_id']
if _data['user_id'] == user_id:
list_amounts.append(float(_data['amount_transaction']))
else:
list_amounts.sort()
Q1_index = get_Q1(len(list_amounts))
Q3_index = get_Q3(len(list_amounts))
Q1_number = getnumber(Q1_index, list_amounts)
Q3_number = getnumber(Q3_index, list_amounts)
IQR = Q3_number - Q1_number
print("User ID: ", user_id, " Interquartile Range: ", IQR)
user_id = _data['user_id']
list_amounts = []
list_amounts.append(float(_data['amount_transaction']))
list_amounts.sort()
Q1_index = get_Q1(len(list_amounts))
Q3_index = get_Q3(len(list_amounts))
Q1_number = get_number(Q1_index, list_amounts)
Q3_number = get_number(Q3_index, list_amounts)
IQR = Q3_number - Q1_number
print("User ID: ", user_id, " Interquartile Range: ", IQR)
except Exception as e:
print("Error: ", str(e))
#------------------------------------------------statistics_module d
#------------------------------------------------statistics_module e
def get_centroid_of_transactions_all():
try:
transaction_data = dataset_module.get_transaction()
list_x_coordinate = []
list_y_coordinate = []
user_id = 0
for _data in transaction_data:
if user_id == 0:
user_id = _data['user_id']
if _data['user_id'] == user_id:
list_x_coordinate.append(float(_data['x_coordinate']))
list_y_coordinate.append(float(_data['y_coordinate']))
else:
_len = len(list_x_coordinate)
centroid_x = sum(list_x_coordinate) / _len
centroid_y = sum(list_y_coordinate) / _len
print("User ID: ", user_id, " Centroid: ", [centroid_x, centroid_y])
user_id = _data['user_id']
list_x_coordinate = []
list_y_coordinate = []
list_x_coordinate.append(float(_data['x_coordinate']))
list_y_coordinate.append(float(_data['y_coordinate']))
_len = len(list_x_coordinate)
centroid_x = sum(list_x_coordinate) / _len
centroid_y = sum(list_y_coordinate) / _len
print("User ID: ", user_id, " Centroid: ", [centroid_x, centroid_y])
except Exception as e:
print("Error: ", str(e))
#------------------------------------------------statistics_module e
#---------------------------statistics_module f
def get_standard_deviatio_of_transactions_by_user_id(user_id):
try:
transaction_data = dataset_module.get_transaction()
sum_amount = 0
count_amount = 0
list_amounts = []
difference_value = 0
for _data in transaction_data:
if _data['user_id'] == user_id:
sum_amount += float(_data['amount_transaction'])
count_amount += 1
list_amounts.append(float(_data['amount_transaction']))
avg = sum_amount / count_amount
for _data in list_amounts:
difference_value += ((_data - avg) * (_data - avg))
variance = difference_value / count_amount
standard_deviation = math.sqrt(variance)
print("User ID: ", user_id, " Standard Deviation of Amount: ", standard_deviation)
except Exception as e:
print("Error: ", str(e))
def get_standard_deviatio_of_transactions_all():
try:
transaction_data = dataset_module.get_transaction()
sum_amount = 0
count_amount = 0
list_amounts = []
difference_value = 0
user_id = 0
for _data in transaction_data:
if user_id == 0:
user_id = _data['user_id']
if _data['user_id'] == user_id:
sum_amount += float(_data['amount_transaction'])
count_amount += 1
list_amounts.append(float(_data['amount_transaction']))
else:
avg = sum_amount / count_amount
for _data_new in list_amounts:
difference_value += ((_data_new - avg) * (_data_new - avg))
variance = difference_value / count_amount
print("User ID: ", user_id, " Standard Deviation of Amount: ", math.sqrt(variance))
user_id = _data['user_id']
sum_amount = 0
count_amount = 0
list_amounts = []
difference_value = 0
sum_amount += float(_data['amount_transaction'])
count_amount += 1
list_amounts.append(float(_data['amount_transaction']))
avg = sum_amount / count_amount
for _data1 in list_amounts:
difference_value += ((_data1 - avg) * (_data1 - avg))
variance = difference_value / count_amount
print("User ID: ", user_id, " Standard Deviation of Amount: ", math.sqrt(variance))
except Exception as e:
print("Error: ", str(e))
#------------------------------------------------statistics_module f
#---------------------------statistics_module l
def get_nth_percentiles_of_transactions_by_user_id(user_id, perc: int):
try:
transaction_data = dataset_module.get_transaction()
list_amounts = []
for _data in transaction_data:
if _data['user_id'] == user_id:
list_amounts.append(float(_data['amount_transaction']))
list_amounts.sort() # Corrected sorting
percentile_index = int(math.ceil((len(list_amounts) * perc) / 100)) - 1
percentile_value = list_amounts[percentile_index]
print("User ID: ", user_id, " nth Percentile Amount: ", percentile_value)
except Exception as e:
print("Error: ", str(e))
def get_nth_percentiles_of_transactions_all(perc: int):
try:
transaction_data = dataset_module.get_transaction()
list_amounts = []
user_id = 0
for _data in transaction_data:
if user_id == 0:
user_id = _data['user_id']
if _data['user_id'] == user_id:
list_amounts.append(float(_data['amount_transaction']))
else:
list_amounts.sort() # Corrected sorting
percentile_index = int(math.ceil((len(list_amounts) * perc) / 100)) - 1
percentile_value = list_amounts[percentile_index]
print("User ID: ", user_id, " nth Percentile Amount: ", percentile_value)
user_id = _data['user_id']
list_amounts = []
list_amounts.append(float(_data['amount_transaction']))
list_amounts.sort() # Corrected sorting
percentile_index = int(math.ceil((len(list_amounts) * perc) / 100)) - 1
percentile_value = list_amounts[percentile_index]
print("User ID: ", user_id, " nth Percentile Amount: ", percentile_value)
except Exception as e:
print("Error: ", str(e))
#------------------------------------------------statistics_module l
#------------------------------------------------statistics_module k
def get_outlier_of_transactions_by_user_id(user_id):
try:
transaction_data = dataset_module.get_transaction()
list_x_coordinate = []
list_y_coordinate = []
for _data in transaction_data:
if _data['user_id'] == user_id:
list_x_coordinate.append(float(_data['x_coordinate']))
list_y_coordinate.append(float(_data['y_coordinate']))
list_x_coordinate.sort()
list_y_coordinate.sort()
Q1_index = get_Q1(len(list_x_coordinate))
Q3_index = get_Q3(len(list_x_coordinate))
Q1_number = getnumber(Q1_index, list_x_coordinate)
Q3_number = getnumber(Q3_index, list_x_coordinate)
IQR = Q3_number - Q1_number
lower_bound = Q1_number - (1.5 * IQR)
upper_bound = Q3_number + (1.5 * IQR)
outliers_x = [x for x in list_x_coordinate if x <= lower_bound or x >= upper_bound]
Q1_index_y = get_Q1(len(list_y_coordinate))
Q3_index_y = get_Q3(len(list_y_coordinate))
Q1_number_y = getnumber(Q1_index_y, list_y_coordinate)
Q3_number_y = getnumber(Q3_index_y, list_y_coordinate)
IQR_y = Q3_number_y - Q1_number_y
lower_bound_y = Q1_number_y - (1.5 * IQR_y)
upper_bound_y = Q3_number_y + (1.5 * IQR_y)
outliers_y = [y for y in list_y_coordinate if y <= lower_bound_y or y >= upper_bound_y]
print("User ID: ", user_id, " Outliers X: ", outliers_x, " Outliers Y: ", outliers_y)
except Exception as e:
print("Error: ", str(e))
def get_outlier_of_transactions_all():
try:
transaction_data = dataset_module.get_transaction()
if not transaction_data:
raise ValueError("No transaction data found.")
list_x_coordinate = []
list_y_coordinate = []
user_id = 0
for _data in transaction_data:
if(user_id == 0):
user_id = _data['user_id']
if(_data['user_id'] == user_id):
list_x_coordinate.append(float(_data['x_coordinate']))
list_y_coordinate.append(float(_data['y_coordinate']))
else:
list_x_coordinate.sort()
list_y_coordinate.sort()
Q1_index = get_Q1(len(list_x_coordinate))
Q3_index = get_Q3(len(list_x_coordinate))
Q1_number = getnumber(Q1_index, list_x_coordinate)
Q3_number = getnumber(Q3_index, list_x_coordinate)
IQR = Q3_number - Q1_number
lower_bound = Q1_number - (1.5 * IQR)
upper_bound = Q3_number + (1.5 * IQR)
outliers_x = [x for x in list_x_coordinate if x <= lower_bound or x >= upper_bound]
Q1_index_y = get_Q1(len(list_y_coordinate))
Q3_index_y = get_Q3(len(list_y_coordinate))
Q1_number_y = getnumber(Q1_index_y, list_y_coordinate)
Q3_number_y = getnumber(Q3_index_y, list_y_coordinate)
IQR_y = Q3_number_y - Q1_number_y
lower_bound_y = Q1_number_y - (1.5 * IQR_y)
upper_bound_y = Q3_number_y + (1.5 * IQR_y)
outliers_y = [y for y in list_y_coordinate if y <= lower_bound_y or y >= upper_bound_y]
print("User ID : ", user_id, " outliers x : ", outliers_x, " outliers y : ", outliers_y)
user_id = _data['user_id']
list_x_coordinate = []
list_y_coordinate = []
list_x_coordinate.append(float(_data['x_coordinate']))
list_y_coordinate.append(float(_data['y_coordinate']))
list_x_coordinate.sort()
list_y_coordinate.sort()
Q1_index = get_Q1(len(list_x_coordinate))
Q3_index = get_Q3(len(list_x_coordinate))
Q1_number = getnumber(Q1_index, list_x_coordinate)
Q3_number = getnumber(Q3_index, list_x_coordinate)
IQR = Q3_number - Q1_number
lower_bound = Q1_number - (1.5 * IQR)
upper_bound = Q3_number + (1.5 * IQR)
outliers_x = [x for x in list_x_coordinate if x <= lower_bound or x >= upper_bound]
Q1_index_y = get_Q1(len(list_y_coordinate))
Q3_index_y = get_Q3(len(list_y_coordinate))
Q1_number_y = getnumber(Q1_index_y, list_y_coordinate)
Q3_number_y = getnumber(Q3_index_y, list_y_coordinate)
IQR_y = Q3_number_y - Q1_number_y
lower_bound_y = Q1_number_y - (1.5 * IQR_y)
upper_bound_y = Q3_number_y + (1.5 * IQR_y)
outliers_y = [y for y in list_y_coordinate if y <= lower_bound_y or y >= upper_bound_y]
print("User ID : ",user_id ," outliers x : ",outliers_x," outliers y : ",outliers_y)
except Exception as e:
raise Exception("An error occurred while processing transactions: {}".format(str(e)))
#------------------------------------------------statistics_module k
#------------------------------------------------statistics_module j
def get_frequencies_of_transactions(x, y):
try:
count = 0
transaction_data = dataset_module.get_transaction()
for _data in transaction_data:
if _data['x_coordinate'] == x and _data['y_coordinate'] == y:
count += 1
return count / len(transaction_data)
except ZeroDivisionError:
print("Error: No transactions found in the dataset.")
return 0
except Exception as e:
print("Error:", e)
return None
#------------------------------------------------statistics_module j
#-----------------------------------------------------statistics_module i
def calculate_zscore(data):
mean = sum(data) / len(data)
variance = sum([(x - mean) ** 2 for x in data]) / len(data)
std_dev = math.sqrt(variance)
z_scores = [(x - mean) / std_dev for x in data]
return z_scores
def get_zscore_of_transactions_by_user_id(user_id):
try:
transaction_data = dataset_module.get_transaction()
list_amounts = []
for _data in transaction_data:
if _data['user_id'] == user_id:
list_amounts.append(float(_data['amount_transaction']))
z_scores = calculate_zscore(list_amounts)
# print("z_scores ",z_scores)
return z_scores
except KeyError:
print("Error: 'amount_transaction' key not found in the transaction data.")
return None
except ValueError as e:
print("Error:", e)
return None
except Exception as e:
print("Error:", e)
return None
def get_zscoresr_of_transactions_all():
try:
transaction_data = dataset_module.get_transaction()
list_amounts = []
for _data in transaction_data:
list_amounts.append(float(_data['amount_transaction']))
z_scores = calculate_zscore(list_amounts)
return z_scores
except KeyError:
print("Error: 'amount_transaction' key not found in the transaction data.")
return None
except ValueError as e:
print("Error:", e)
return None
except Exception as e:
print("Error:", e)
return None
#-----------------------------------------------------statistics_module i
#-----------------------------------------------------statistics_module h
def get_abnormal_transactions(user_id):
try:
transaction_data = dataset_module.get_transaction()
list_amounts = []
user_transactions = []
for _data in transaction_data:
if _data['user_id'] == user_id:
list_amounts.append(float(_data['amount_transaction']))
user_transactions.append(_data)
mean = sum(list_amounts) / len(list_amounts)
std_dev = (sum((x - mean)**2 for x in list_amounts) / len(list_amounts))**0.5
abnormal_transactions = []
for t in user_transactions:
if float(t['amount_transaction']) > mean + 3 * std_dev:
abnormal_transactions.append(t)
return abnormal_transactions
except KeyError:
print("Error: 'amount_transaction' or 'user_id' key not found in the transaction data.")
return None
except ValueError as e:
print("Error:", e)
return None
except ZeroDivisionError:
print("Error: Division by zero occurred.")
return None
except Exception as e:
print("Error:", e)
return None
#-----------------------------------------------------statistics_module h
def get_fraudulent_by_transaction_id(transaction_id):
try:
flag = 0
transaction_data = dataset_module.get_transaction()
list_amounts = []
for _data in transaction_data:
if _data['transaction_id'] == transaction_id:
print(_data['isfraudulent'])
if _data['isfraudulent'] == "false":
flag = 1
print("The transaction is not fraudulent")
print(" User ID :",_data['user_id'], " Transaction ID :",_data['transaction_id']," Description :",_data['description_transaction']," Amount :",_data['amount_transaction']," X Coordinate :",_data['x_coordinate']," Y Coordinate :",_data['y_coordinate'] )
else:
print("It is a fraudulent transaction")
print(" User ID :",_data['user_id'], " Transaction ID :",_data['transaction_id']," Description :",_data['description_transaction']," Amount :",_data['amount_transaction']," X Coordinate :",_data['x_coordinate']," Y Coordinate :",_data['y_coordinate'] )
if flag == 0:
print("transaction id not found")
except KeyError:
print("Error: 'amount_transaction' key not found in the transaction data.")
return None
except ValueError as e:
print("Error:", e)
return None
except Exception as e:
print("Error:", e)
return None