-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerchant_float_engine.py
More file actions
176 lines (128 loc) · 5.32 KB
/
Copy pathmerchant_float_engine.py
File metadata and controls
176 lines (128 loc) · 5.32 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
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
import os
# CORE ENGINE (USED BY BOTH CSV + API)
def process_dataframe(df):
try:
df['transaction_date'] = pd.to_datetime(df['transaction_date'])
# FEATURE ENGINEERING
merchant_profiles = df.groupby('merchant_id').agg({
'daily_revenue': ['mean', 'std'],
'returning_customer_ratio': 'mean',
'avg_settlement_delay_hours': 'mean',
'transaction_count': 'mean',
'refund_count': 'sum'
})
merchant_profiles.columns = [
'avg_rev',
'rev_volatility',
'avg_loyalty',
'avg_delay',
'avg_trans_count',
'total_refunds'
]
merchant_profiles = merchant_profiles.fillna(0)
# CLUSTERING
scaler = StandardScaler()
scaled_features = scaler.fit_transform(merchant_profiles)
kmeans = KMeans(n_clusters=4, random_state=42, n_init=10)
merchant_profiles['cluster'] = kmeans.fit_predict(scaled_features)
# SEGMENT
def map_segment(row):
if row['avg_rev'] > merchant_profiles['avg_rev'].median() and row['avg_loyalty'] > 0.5:
return "Elite (High Growth)"
elif row['avg_delay'] > 30:
return "At Risk (Settlement Delay)"
elif row['rev_volatility'] > merchant_profiles['rev_volatility'].mean() * 1.5:
return "Volatile (Unstable)"
else:
return "Steady (Reliable)"
merchant_profiles['segment_name'] = merchant_profiles.apply(map_segment, axis=1)
# SCORING
final_output = []
for merchant_id, row in merchant_profiles.iterrows():
consistency_score = max(0, 100 - (row['rev_volatility'] / (row['avg_rev'] + 1) * 100))
loyalty_score = row['avg_loyalty'] * 100
transaction_score = (min(row['avg_trans_count'], 20) / 20) * 100
revenue_score = min(row['avg_rev'] / 80000, 1.2) * 100
base_score = (
0.25 * loyalty_score +
0.25 * consistency_score +
0.15 * transaction_score +
0.35 * revenue_score
)
delay_penalty = min(row['avg_delay'] * 0.3, 15)
cluster_penalty_map = {0: 0, 1: 3, 2: 6, 3: 10}
cluster_penalty = cluster_penalty_map.get(row['cluster'], 5)
norm_score = base_score - delay_penalty - cluster_penalty
norm_score = min(max(norm_score, 0), 100)
credit_score = int(300 + (norm_score * 5.5))
# RISK
if credit_score >= 700:
risk_level = "Low"
max_loan = round(row['avg_rev'] * 15, -3)
elif credit_score >= 520:
risk_level = "Medium"
max_loan = round(row['avg_rev'] * 7, -3)
else:
risk_level = "High"
max_loan = 0
# INSIGHTS
m_raw = df[df['merchant_id'] == merchant_id]
peak_hr = int(m_raw['peak_sales_hour'].mode()[0])
insights = []
if row['avg_loyalty'] < 0.4:
insights.append("Low repeat customers")
if row['avg_delay'] > 30:
insights.append("Settlement delays affecting cash flow")
if row['rev_volatility'] > merchant_profiles['rev_volatility'].mean() * 1.5:
insights.append("Revenue is inconsistent")
if row['avg_rev'] > merchant_profiles['avg_rev'].median():
insights.append("Strong revenue performance")
insights.append(f"Peak sales at {peak_hr}:00")
final_output.append({
"merchant_id": merchant_id,
"segment": row['segment_name'],
"credit_score": credit_score,
"risk_level": risk_level,
"loan_offer": int(max_loan),
"cluster": int(row['cluster']),
"insights": insights
})
return final_output
except Exception as e:
import traceback
traceback.print_exc()
return None
# CSV VERSION (FOR TESTING / DEMO)
def run_merchant_float_ai(csv_path):
try:
if not os.path.exists(csv_path):
raise FileNotFoundError(f"{csv_path} not found")
df = pd.read_csv(csv_path)
print(f"✅ Data loaded: {len(df)} rows")
return process_dataframe(df)
except Exception as e:
import traceback
traceback.print_exc()
return None
# API VERSION (FOR INTERSWITCH / LIVE DATA)
def run_merchant_float_ai_from_df(df):
return process_dataframe(df)
# TEST RUN
if __name__ == "__main__":
print("🚀 Running MerchantFloat AI Engine...")
path = "merchantfloat_dataset.csv"
print(" Current Directory:", os.getcwd())
results = run_merchant_float_ai(path)
if results:
print("\n--- MERCHANTFLOAT AI REPORT ---\n")
for m in results:
print(f"Merchant: {m['merchant_id']}")
print(f"Score: {m['credit_score']} | Risk: {m['risk_level']}")
print(f"Segment: {m['segment']}")
print(f"Loan Offer: {m['loan_offer']}")
print(f"Insights: {', '.join(m['insights'])}")
print("-" * 40)