MichaelGelshtein commited on
Commit
10ae982
·
verified ·
1 Parent(s): 6b9ebd6

Upload 2 files

Browse files
README.md ADDED
@@ -0,0 +1,233 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ task_categories:
4
+ - tabular-classification
5
+ size_categories:
6
+ - 100K<n<1M
7
+ ---
8
+
9
+ <video src="presentation.mp4" controls style="max-width: 720px;"></video>
10
+
11
+ # PaySim Financial Fraud Detection — EDA & Dataset Analysis
12
+
13
+ ## Dataset Overview
14
+
15
+ This project analyzes the **PaySim Financial Fraud Detection** dataset from Kaggle (source: chitwanmanchanda/fraudulent-transactions-data). The dataset contains simulated mobile money transactions with fraud labels, representing 100,000 randomly sampled transactions (random_state=42) from the original 6,362,620 rows.
16
+
17
+ **Key characteristics:**
18
+ - **Total records:** 100,000 transactions
19
+ - **Target variable:** `isFraud` (0 = legitimate, 1 = fraudulent)
20
+ - **Data quality issues found & resolved:** 200 missing `amount` values, 300 missing `oldbalanceOrg` values, 500 duplicate rows, 400 inconsistent `type` formatting
21
+ - **Class imbalance:** Fraud is extremely rare (~0.14% of all transactions)
22
+
23
+ ---
24
+
25
+ ## Research Question
26
+
27
+ **"How do specific economic indicators, such as transaction types and sudden inconsistencies between account balances, act as financial fingerprints to accurately predict fraudulent activity, and can identifying these logical gaps in the data lead to more efficient and cost-effective detection systems?"**
28
+
29
+ This project investigates whether patterns in transaction characteristics—particularly transaction type, amount magnitude, and account balance behavior—can serve as reliable fraud indicators without relying on demographic or user-level features.
30
+
31
+ ---
32
+
33
+ ## Features
34
+
35
+ | Feature | Type | Description |
36
+ |---------|------|-------------|
37
+ | `step` | int | Time step of the transaction (1-743, representing hours in simulation) |
38
+ | `type` | str | Transaction type: CASH_IN, CASH_OUT, DEBIT, PAYMENT, TRANSFER |
39
+ | `amount` | float | Transaction amount (in currency units) |
40
+ | `oldbalanceOrg` | float | Original account balance before transaction |
41
+ | `newbalanceOrig` | float | Original account balance after transaction |
42
+ | `oldbalanceDest` | float | Destination account balance before transaction |
43
+ | `newbalanceDest` | float | Destination account balance after transaction |
44
+ | `isFraud` | int | Binary fraud label (0 = legitimate, 1 = fraudulent) |
45
+ | `isFlaggedFraud` | int | Whether transaction was flagged by bank's system |
46
+ | `amount_log` | float | Log-transformed amount (log1p) for analysis |
47
+
48
+ ---
49
+
50
+ ## EDA Methodology
51
+
52
+ ### 1. Data Loading & Cleaning
53
+
54
+ **Issues introduced to simulate real-world financial data quality problems:**
55
+ - 200 missing values in `amount` (simulating incomplete transaction records)
56
+ - 300 missing values in `oldbalanceOrg` (simulating balance data not captured at logging time)
57
+ - 500 duplicate rows (simulating double-logging errors in the payment system)
58
+ - 400 rows with inconsistent casing in `type` (simulating data entry errors)
59
+
60
+ **Cleaning steps applied:**
61
+ - **Missing `amount` → dropped rows:** A transaction with no amount is unusable and cannot be safely imputed
62
+ - **Missing `oldbalanceOrg` → filled with median:** Median chosen over mean because the amount column is right-skewed; median is a robust estimator
63
+ - **Duplicates → `drop_duplicates()`:** Removed all 500 duplicate rows to prevent inflated counts
64
+ - **`type` inconsistency → `.str.upper().str.strip()`:** Standardized all 5 transaction types to uppercase to prevent `transfer` and `TRANSFER` being treated as separate categories
65
+
66
+ ### 2. Outlier Detection & Treatment
67
+ - Applied **Interquartile Range (IQR) method** on `amount` feature
68
+ - **5,358 outliers detected** (5.4% of data)
69
+ - **Decision:** Retained all outliers because fraud transactions often occur at extreme values (high amounts)
70
+ - Applied **log1p transformation** to `amount` → `amount_log` for better visualization and modeling
71
+
72
+ ### 3. Descriptive Statistics
73
+ - Calculated summary statistics by fraud status
74
+ - **Key insight:** Fraud transactions are drastically larger than legitimate ones
75
+ - Average fraud amount: ~1,340,000
76
+ - Average legitimate amount: ~179,000
77
+ - **Ratio: 7.5x larger**
78
+
79
+ ### 4. Correlation Analysis
80
+ - Generated correlation heatmap of all numerical features
81
+ - Identified relationships between balance features and fraud status
82
+
83
+ ---
84
+
85
+ ## Key Findings
86
+
87
+ ### Overall Fraud Characteristics
88
+ - **Fraud rate:** 0.14% of all transactions (142 fraudulent out of 100,000)
89
+ - **Class imbalance:** Highly skewed toward legitimate transactions
90
+ - **Amount severity:** Fraudulent transactions are 7.5x larger on average
91
+ - **Bank's detection:** Only 0.56% of fraudulent transactions flagged (isFlaggedFraud=1)
92
+
93
+ ### Transaction Type Distribution
94
+ - Most common type: TRANSFER (35%)
95
+ - PAYMENT transactions: 31%
96
+ - CASH_OUT: 24%
97
+ - DEBIT: 8%
98
+ - CASH_IN: 2%
99
+
100
+ ### Fraud Distribution by Type
101
+ - **TRANSFER:** 0.99% fraud rate (77 frauds out of 7,806 transfers)
102
+ - **CASH_OUT:** 0.30% fraud rate (65 frauds out of 23,235 cash-outs)
103
+ - **PAYMENT:** 0% fraud rate (0 frauds)
104
+ - **DEBIT:** 0% fraud rate (0 frauds)
105
+ - **CASH_IN:** 0% fraud rate (0 frauds)
106
+
107
+ ---
108
+
109
+ ## Four Research Questions & Insights
110
+
111
+ ### Research Q1: "Do high-value transactions carry higher fraud risk?"
112
+ **Visualization:** Box plot of transaction amounts by fraud status
113
+
114
+ **Finding:** Yes, fraud transactions occupy the extreme upper tail of the amount distribution. The highest-value transactions (>100K) show disproportionately high fraud rates, indicating that transaction magnitude is a critical risk indicator.
115
+
116
+ **Conclusion:** Amount is a strong univariate fraud signal. Fraudsters tend to target high-value transfers to maximize stolen funds per transaction.
117
+
118
+ ---
119
+
120
+ ### Research Q2: "Do fraudsters drain the sender's account to zero?"
121
+ **Visualization:** Bar chart showing proportion of transactions where `newbalanceOrig ≈ 0`
122
+
123
+ **Finding:**
124
+ - Fraudsters drain sender account to zero in **96% of fraud cases**
125
+ - Legitimate transactions show account depletion in only **0.3% of cases**
126
+ - This represents a massive logical inconsistency: legitimate users rarely empty accounts, but fraudsters always do
127
+
128
+ **Conclusion:** Balance depletion is one of the strongest fraud indicators. This "emptying the well" behavior is a logical fingerprint of fraudulent behavior—attackers maximize extraction and disappear.
129
+
130
+ ---
131
+
132
+ ### Research Q3: "Are certain transaction types fraud-free?"
133
+ **Visualization:** Bar chart showing fraud rate (%) by transaction type
134
+
135
+ **Finding:**
136
+ - TRANSFER and CASH_OUT are the ONLY types with fraud (0.99% and 0.30% respectively)
137
+ - PAYMENT, DEBIT, and CASH_IN have 0% fraud in this dataset
138
+ - This suggests fraud is concentrated in transaction types that transfer money away from the original account
139
+
140
+ **Conclusion:** Transaction type is a critical categorical predictor. A simple rule-based filter (flag TRANSFER & CASH_OUT only) would catch 100% of fraud with minimal false positives.
141
+
142
+ ---
143
+
144
+ ### Research Q4: "Is fraud correlated with transaction amount ranges?"
145
+ **Visualization:** Bar chart showing fraud rate (%) across amount bins (0-1K, 1K-10K, 10K-100K, >100K)
146
+
147
+ **Finding:**
148
+ - 0-1K: 0.04% fraud rate
149
+ - 1K-10K: 0.10% fraud rate
150
+ - 10K-100K: 0.27% fraud rate
151
+ - >100K: 1.08% fraud rate
152
+ - **Clear monotonic increase:** Larger bins have exponentially higher fraud risk
153
+
154
+ **Conclusion:** Amount binning creates a simple but effective fraud scoring feature. Transaction amount is strongly predictive; the largest 1% of transactions by amount contain ~8x more fraud than the smallest.
155
+
156
+ ---
157
+
158
+ ## Key Decisions & Rationale
159
+
160
+ ### Why keep outliers?
161
+ Fraud is inherently an outlier behavior. Removing high-value transactions would eliminate the most suspicious cases. **Decision: Retain all 5,358 outliers** because they represent the exact transactions we want to catch.
162
+
163
+ ### Why log-transform the amount?
164
+ Raw amounts span from 0 to millions, creating extreme skewness. Log transformation:
165
+ - Makes distributions more interpretable
166
+ - Reduces the visual dominance of extreme values
167
+ - Improves visualization clarity without losing information
168
+
169
+ ### Why 100K sample?
170
+ The original dataset has 6.3M rows. A 100K random sample:
171
+ - Maintains representative fraud rate (~0.14%)
172
+ - Reduces computational overhead
173
+ - Sufficient for EDA without requiring distributed computing
174
+ - Preserves randomness with seed=42 for reproducibility
175
+
176
+ ### Why focus on these 4 research questions?
177
+ Each addresses a different aspect of fraud detection logic:
178
+ - Q1: Amount-based risk
179
+ - Q2: Balance consistency (logical gaps in data)
180
+ - Q3: Transaction-type filtering (zero false negatives)
181
+ - Q4: Amount-bin stratification (granular risk tiers)
182
+
183
+ Together they reveal that fraud has **multiple independent signals**: type, amount, and balance behavior all strongly predict fraud independently.
184
+
185
+ ---
186
+
187
+ ## Visualizations
188
+
189
+ The following plots were generated during EDA:
190
+
191
+ ![Amount Distribution](plots/amount_distribution.png)
192
+ *(run notebook to generate and upload plot images)*
193
+
194
+ ![Correlation Heatmap](plots/correlation_heatmap.png)
195
+ *(run notebook to generate and upload plot images)*
196
+
197
+ ![Transaction Type Distribution](plots/transaction_type_pie.png)
198
+ *(run notebook to generate and upload plot images)*
199
+
200
+ ![Fraud Rate by Transaction Type](plots/fraud_by_type_bar.png)
201
+ *(run notebook to generate and upload plot images)*
202
+
203
+ ![Amount Box Plot by Fraud Status](plots/amount_boxplot.png)
204
+ *(run notebook to generate and upload plot images)*
205
+
206
+ ![Account Depletion Analysis](plots/depletion_bar.png)
207
+ *(run notebook to generate and upload plot images)*
208
+
209
+ ![Fraud Rate by Amount Bin](plots/fraud_by_amount_bin.png)
210
+ *(run notebook to generate and upload plot images)*
211
+
212
+ ---
213
+
214
+ ## Conclusion
215
+
216
+ This EDA reveals that **financial fraud leaves multiple consistent fingerprints** in transaction data. Rather than relying on complex models or external features, we can identify fraud through:
217
+
218
+ 1. **Transaction type filtering** (TRANSFER & CASH_OUT only)
219
+ 2. **Amount thresholds** (very large transactions are 1000x more likely to be fraud)
220
+ 3. **Balance logic checks** (account depletion to zero is virtually diagnostic)
221
+
222
+ These insights suggest that **cost-effective fraud detection is possible** through:
223
+ - Simple rule-based systems that catch most fraud with high precision
224
+ - Tiered monitoring (higher scrutiny for TRANSFER/CASH_OUT types)
225
+ - Real-time balance anomaly detection (flag when `newbalanceOrig ≈ 0`)
226
+
227
+ Future work could focus on ensemble methods combining these signals, time-series analysis (seasonal patterns), and network-level features (repeated defrauders) to achieve even higher detection rates.
228
+
229
+ ---
230
+
231
+ **Dataset source:** Kaggle — PaySim Financial Fraud Detection
232
+ **Sample method:** Random sampling, n=100,000, random_state=42
233
+ **Analysis date:** April 2026
assignment_1_EDA_Data_Science_FINAL.ipynb ADDED
The diff for this file is too large to render. See raw diff