-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_lightgbm.py
More file actions
265 lines (200 loc) · 8.76 KB
/
build_lightgbm.py
File metadata and controls
265 lines (200 loc) · 8.76 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
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
import pandas as pd
import numpy as np
import lightgbm as lgb
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, roc_auc_score, confusion_matrix
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
print("=" * 60)
print("LightGBM 模型:预测客户未来 3 个月资产提升至 100 万+的概率")
print("=" * 60)
# 1. 读取数据
print("\n[1] 读取数据...")
df_base = pd.read_csv('customer/customer_base.csv')
df_behavior = pd.read_csv('customer/customer_behavior_assets.csv')
# 排序
df_behavior = df_behavior.sort_values(['customer_id', 'stat_month']).reset_index(drop=True)
print(f" 客户基础数据:{len(df_base)} 条")
print(f" 客户行为数据:{len(df_behavior)} 条")
# 2. 构造时序特征函数
def create_time_series_features(group, base_month, lookback=6):
"""
为每个客户在特定基线月份构造时序特征
group: 某个客户的历史数据
base_month: 基线月份
lookback: 往回看几个月
"""
months_list = sorted(group['stat_month'].unique())
if base_month not in months_list:
return None
base_idx = months_list.index(base_month)
if base_idx < lookback - 1:
return None
feature_months = months_list[base_idx - lookback + 1: base_idx + 1]
feature_data = group[group['stat_month'].isin(feature_months)].copy()
if len(feature_data) < lookback:
return None
future_months = []
base_year, base_m = int(base_month.split('-')[0]), int(base_month.split('-')[1])
for i in range(1, 4):
m = base_m + i
y = base_year
while m > 12:
m -= 12
y += 1
future_months.append(f"{y}-{m:02d}")
future_data = group[group['stat_month'].isin(future_months)]
features = {}
base_data = feature_data[feature_data['stat_month'] == base_month].iloc[0]
features['total_assets'] = base_data['total_assets']
features['deposit_balance'] = base_data['deposit_balance']
features['financial_balance'] = base_data['financial_balance']
features['fund_balance'] = base_data['fund_balance']
features['insurance_balance'] = base_data['insurance_balance']
features['product_count'] = base_data['product_count']
features['app_login_count'] = base_data['app_login_count']
features['app_financial_view_time'] = base_data['app_financial_view_time']
features['credit_card_monthly_expense'] = base_data['credit_card_monthly_expense']
features['investment_monthly_count'] = base_data['investment_monthly_count']
features['financial_repurchase_count'] = base_data['financial_repurchase_count']
assets = feature_data.sort_values('stat_month')['total_assets'].values
if len(assets) >= 2:
features['asset_growth_rate_1m'] = (assets[-1] - assets[-2]) / (assets[-2] + 1)
if len(assets) >= 4:
features['asset_growth_rate_3m'] = (assets[-1] - assets[-4]) / (assets[-4] + 1)
if len(assets) >= 6:
features['asset_growth_rate_6m'] = (assets[-1] - assets[-6]) / (assets[-6] + 1)
features['asset_mean_6m'] = np.mean(assets)
features['asset_std_6m'] = np.std(assets)
features['asset_cv_6m'] = np.std(assets) / (np.mean(assets) + 1)
features['current_vs_avg'] = (assets[-1] - np.mean(assets)) / (np.mean(assets) + 1)
if len(assets) >= 2:
x = np.arange(len(assets))
slope, intercept = np.polyfit(x, assets, 1)
features['trend_slope'] = slope
y_pred = slope * x + intercept
ss_res = np.sum((assets - y_pred) ** 2)
ss_tot = np.sum((assets - np.mean(assets)) ** 2)
features['trend_r_squared'] = 1 - (ss_res / (ss_tot + 1))
if len(assets) >= 6:
recent_3_mean = np.mean(assets[-3:])
earlier_3_mean = np.mean(assets[-6:-3])
features['asset_momentum'] = (recent_3_mean - earlier_3_mean) / (earlier_3_mean + 1)
if len(future_data) > 0:
max_assets_future = future_data['total_assets'].max()
features['target'] = 1 if max_assets_future >= 1000000 else 0
else:
return None
features['base_month'] = base_month
features['customer_id'] = group['customer_id'].iloc[0]
return features
# 3. 构造训练数据
print("\n[2] 构造时序特征和目标变量...")
base_months = ['2024-07', '2024-08', '2024-09', '2024-10', '2024-11',
'2024-12', '2025-01', '2025-02', '2025-03']
all_samples = []
for customer_id, group in df_behavior.groupby('customer_id'):
for base_month in base_months:
result = create_time_series_features(group, base_month, lookback=6)
if result is not None:
all_samples.append(result)
df_samples = pd.DataFrame(all_samples)
print(f" 构造样本数:{len(df_samples)}")
# 4. 合并静态特征
print("\n[3] 合并静态特征...")
static_features = ['age', 'monthly_income', 'gender', 'occupation_type',
'lifecycle_stage', 'marriage_status', 'city_level']
df_static = df_base[['customer_id'] + static_features].copy()
df_train = df_samples.merge(df_static, on='customer_id', how='left')
print(f" 合并后样本数:{len(df_train)}")
# 5. 数据预处理
print("\n[4] 数据预处理...")
target_col = 'target'
feature_cols = [col for col in df_train.columns if col not in ['target', 'customer_id', 'base_month']]
X = df_train[feature_cols].copy()
y = df_train[target_col].copy()
from sklearn.preprocessing import LabelEncoder
categorical_cols = ['gender', 'occupation_type', 'lifecycle_stage', 'marriage_status', 'city_level']
for col in categorical_cols:
le = LabelEncoder()
X[col] = le.fit_transform(X[col].astype(str))
X = X.fillna(0)
print(f" 特征数:{len(feature_cols)}")
print(f" 正样本数:{y.sum()} ({y.mean()*100:.2f}%)")
print(f" 负样本数:{len(y) - y.sum()} ({(1-y.mean())*100:.2f}%)")
# 6. 划分训练集和测试集
print("\n[5] 划分数据集...")
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
print(f" 训练集:{len(X_train)} 条")
print(f" 测试集:{len(X_test)} 条")
# 7. 训练 LightGBM 模型
print("\n[6] 训练 LightGBM 模型...")
model = lgb.LGBMClassifier(
n_estimators=100,
max_depth=6,
learning_rate=0.1,
random_state=42,
class_weight='balanced',
verbosity=-1
)
model.fit(X_train, y_train)
# 8. 模型评估
print("\n[7] 模型评估...")
y_pred = model.predict(X_test)
y_pred_proba = model.predict_proba(X_test)[:, 1]
print("\n分类报告:")
print(classification_report(y_test, y_pred, target_names=['未达 100 万', '达到 100 万']))
auc_score = roc_auc_score(y_test, y_pred_proba)
print(f"AUC 分数:{auc_score:.4f}")
cm = confusion_matrix(y_test, y_pred)
print("\n混淆矩阵:")
print(cm)
# 9. 特征重要性
print("\n[8] 特征重要性:")
print("-" * 60)
feature_importance = pd.DataFrame({
'feature': feature_cols,
'importance': model.feature_importances_
})
feature_importance = feature_importance.sort_values('importance', ascending=False)
print("\n特征重要性(按重要性排序 Top 20):")
print(feature_importance.head(20)[['feature', 'importance']].to_string(index=False))
feature_importance.to_csv('lgbm_feature_importance.csv', index=False)
print("\n特征重要性已保存到 lgbm_feature_importance.csv")
# 10. 特征重要性可视化
print("\n[9] 生成特征重要性可视化...")
plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
fig, ax = plt.subplots(figsize=(14, 12))
top_features = feature_importance.head(20)
bars = ax.barh(range(len(top_features)), top_features['importance'], color='#667eea')
ax.set_yticks(range(len(top_features)))
ax.set_yticklabels(top_features['feature'], fontsize=11)
for i, (bar, imp) in enumerate(zip(bars, top_features['importance'])):
width = bar.get_width()
ax.text(width + 0.1,
bar.get_y() + bar.get_height()/2,
f'{imp:.1f}',
va='center', ha='left',
fontsize=10)
ax.set_xlabel('特征重要性 (Feature Importance)', fontsize=12)
ax.set_title('LightGBM 特征重要性 Top 20\n(按重要性排序)', fontsize=14, fontweight='bold')
plt.tight_layout()
plt.savefig('lgbm_feature_importance.png', dpi=150, bbox_inches='tight')
print(" 特征重要性可视化已保存到 lgbm_feature_importance.png")
# 11. 显示关键发现
print("\n[10] 关键发现:")
print("-" * 60)
top_5_features = feature_importance.head(5)[['feature', 'importance']]
print("\n最重要的特征(Top 5):")
for idx, row in top_5_features.iterrows():
print(f" {row['feature']}: {row['importance']:.1f}")
print("\n" + "=" * 60)
print("LightGBM 模型训练完成!")
print("=" * 60)
print("\n生成的文件:")
print(" 1. lgbm_feature_importance.csv - 特征重要性数据")
print(" 2. lgbm_feature_importance.png - 特征重要性图")