/yihuier:risk-modelingfrom yihuier
Guides credit scorecard modeling using the yihuier toolkit: data exploration, binning, WOE transformation, variable selection, logistic regression, and model evaluation with AUC/KS/PSI quality gates.
Ships in yihuier. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
使用 yihuier 工具包完成标准化建模流程。
直接使用模板脚本 scripts/risk_modeling_template.py。
pip install yihuier
数据要求:
import pandas as pd
from yihuier import Yihuier
data = pd.read_excel('data.xlsx')
yh = Yihuier(data, target='target_column')
numeric_vars = yh.get_numeric_variables()
yh.data = yh.dp_module.fillna_num_var(numeric_vars, fill_type='0')
yh.data = yh.dp_module.target_missing_delete()
# 交叉特征(评分卡常用:负债比、利用率等)
yh.data = yh.fe_module.gen_ratio('debt', 'income', name='dti')
yh.data = yh.fe_module.gen_diff('balance', 'limit', name='net_balance')
# 数学变换(处理偏态变量)
yh.data = yh.fe_module.gen_transform('income', method='log1p', name='income_log')
# 缺失指示符(在 fillna 之前调用)
yh.data = yh.fe_module.gen_missing_flag(['income', 'age'])
# → 新增 miss_income、miss_age 两列
# 批量交叉 + IV 预筛
yh.data = yh.fe_module.batch_cross(
col_list=['income', 'debt', 'balance', 'limit'],
ops=['/', '-'],
iv_threshold=0.02,
max_features=50
)
binning_vars = [col for col in numeric_vars if col != yh.target]
# ChiMerge 分箱(推荐)
bin_df, iv_value = yh.binning_module.binning_num(
col_list=binning_vars,
max_bin=5,
method='ChiMerge',
min_binpct=0.05
)
iv_dict = dict(zip(binning_vars, iv_value))
注意:bin_df 是 DataFrame 列表,每个元素对应一个变量。使用 woe_result_df 获取合并的分箱详情。
yh.binning_module.woe_df_concat()
data_woe = yh.binning_module.woe_transform()
woe_result_df 列名:col, index, bin, min_bin, max_bin, total, totalrate, bad, badrate, woe, bin_iv, IV
# IV 筛选
high_iv_vars = [var for var, iv in iv_dict.items() if iv > 0.05]
# 相关性去重
final_vars = yh.var_select_module.forward_delete_corr_ivfirst(
col_list=high_iv_vars,
threshold=0.6
)
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
X = data_woe[final_vars]
y = data_woe[yh.target]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
model = LogisticRegression(max_iter=1000, class_weight='balanced')
model.fit(X_train, y_train)
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import cross_val_score
y_pred = model.predict_proba(X_test)[:, 1]
auc = roc_auc_score(y_test, y_pred)
ks = yh.me_module.model_ks(y_test, y_pred)
# 交叉验证
cv_scores = cross_val_score(
LogisticRegression(max_iter=1000, class_weight='balanced'),
X, y, cv=5, scoring='roc_auc'
)
print(f"CV AUC: {cv_scores.mean():.4f} ± {cv_scores.std():.4f}")
A, B, base_score = yh.si_module.cal_scale(
score=600, odds=50, PDO=20, model=model
)
scores = base_score + X_test.dot(model.coef_[0]) * B
# 使用 sm_module.calculate_psi(推荐)
train_scores = base_score + X_train.dot(model.coef_[0]) * B
test_scores = base_score + X_test.dot(model.coef_[0]) * B
psi = yh.sm_module.calculate_psi(train_scores, test_scores)
print(f"PSI: {psi:.4f}")
# 或获取详细表格
psi_detail = yh.sm_module.calculate_psi(train_scores, test_scores, return_detail=True)
woe_result_df = yh.binning_module.woe_result_df
scorecard = []
for var in final_vars:
var_df = woe_result_df[woe_result_df['col'] == var]
coef = model.coef_[0][final_vars.index(var)]
for _, row in var_df.iterrows():
scorecard.append({
'变量名': var,
'分箱': row['bin'],
'WOE': row['woe'],
'得分': round(-coef * row['woe'] * B, 2)
})
| 模块 | 功能 | 关键方法 |
|---|---|---|
eda_module | 数据探索 | auto_eda_simple() |
dp_module | 数据处理 | fillna_num_var(), target_missing_delete() |
fe_module | 特征工程 | gen_ratio(), gen_cross(), batch_cross() |
binning_module | 分箱 | binning_num(), woe_df_concat(), woe_transform() |
var_select_module | 变量选择 | forward_delete_corr_ivfirst() |
me_module | 模型评估 | model_ks(), cross_verify() |
si_module | 评分卡实现 | cal_scale(), rule_verify() |
sm_module | 评分卡监控 | calculate_psi(), score_psi() |
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
Other plugins' skills for the same job
npx claudepluginhub encyc/yihuier --plugin yihuier