Skip to content

Commit b2eae8e

Browse files
committed
fix: remove StandardScaler from fraud detection vectorization
StandardScaler inverts relative distances when most claims share identical features (e.g. same IP), causing LOF to score the homogeneous cluster as more anomalous than actual outliers. Reverting to raw numeric features preserves the correct outlier signal while keeping the small-batch n_neighbors fix.
1 parent f8b97f3 commit b2eae8e

1 file changed

Lines changed: 3 additions & 9 deletions

File tree

app/ai-service/services/fraud_detection.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import List
1010

1111
import numpy as np
12-
from sklearn.preprocessing import LabelEncoder, StandardScaler
12+
from sklearn.preprocessing import LabelEncoder
1313
from sklearn.neighbors import LocalOutlierFactor
1414

1515
from schemas.fraud import ClaimMetadata, ClaimFraudResult
@@ -26,7 +26,7 @@
2626

2727

2828
def _vectorize(claims: List[ClaimMetadata]) -> np.ndarray:
29-
"""Convert claim metadata into a normalised numeric feature matrix."""
29+
"""Convert claim metadata into a numeric feature matrix."""
3030
ip_enc = LabelEncoder()
3131
hash_enc = LabelEncoder()
3232
loc_enc = LabelEncoder()
@@ -40,19 +40,13 @@ def _vectorize(claims: List[ClaimMetadata]) -> np.ndarray:
4040
hash_enc.fit(hashes)
4141
loc_enc.fit(locs)
4242

43-
raw = np.column_stack([
43+
return np.column_stack([
4444
ip_enc.transform(ips).astype(float),
4545
hash_enc.transform(hashes).astype(float),
4646
loc_enc.transform(locs).astype(float),
4747
np.array(amounts, dtype=float),
4848
])
4949

50-
# Standardise every column so Euclidean distance in LOF is not
51-
# dominated by whichever column has the largest range (e.g. raw
52-
# token amounts vs. small integer codes).
53-
scaler = StandardScaler()
54-
return scaler.fit_transform(raw)
55-
5650

5751
def detect_fraud(claims: List[ClaimMetadata]) -> List[ClaimFraudResult]:
5852
"""

0 commit comments

Comments
 (0)