傅里叶变换总结及FFT画图横坐标设置
2026/11/1 1:59:07
做「Give Me Some Credit」毕设时,我踩过的坑可以写一本血泪史。先把痛点摆出来,看看有没有你的影子:
MonthlyIncome缺 20%,NumberOfDependents缺 20%,还有一堆 0 值藏在NumberOfTime30-59DaysPastDueNotWorse里。手动写fillna写完还要回头检查,一不留神就把测试集信息泄露进去。scale_pos_weight每次都要重跑 GridSearch,跑一次 40 分钟,GPU 风扇像要起飞。feature_importances只能给出柱状图,解释力度不够。train.py、predict.py、utils.py全堆在一个文件夹,每次改路径都要cd ..三遍,Docker 镜像打到 3 GB,CI 直接超时。痛定思痛,我决定把 AI 当“外挂”——不是让它替我思考,而是让它替我搬砖。
我先后试了三种主流方案,把体感记录如下:
| 工具 | 优点 | 缺点 | 适合场景 |
|---|---|---|---|
| GitHub Copilot | VS Code 即插即插、补全速度快、理解上下文 | 公网传输、对中文注释偶尔抽 | 个人开发、快速原型 |
| CodeWhisperer | 亚马逊全家桶无缝、免费额度大 | 提示偏保守、对 sklearn 生态支持一般 | AWS 部署项目 |
| 本地 CodeLlama-13B + Ollama | 数据不出内网、可微调 | 显存 10 GB 起步、补全慢 3~5 秒 | 隐私敏感、离线机房 |
最终组合:
下面这段 pipeline 是 Copilot 在我敲完# TODO: automated feature engineering后自动补全的,我仅做了三处微调:
pd.get_dummies改成OneHotEncoder(handle_unknown='ignore')防止新类别爆炸。SimpleImputer的copy=False去掉,避免 SettingWithCopyWarning。# pipeline.py from sklearn.compose import ColumnTransformer from sklearn.pipeline import Pipeline from sklearn.impute import SimpleImputer from sklearn.preprocessing import StandardScaler, OneHotEncoder from sklearn.metrics import roc_auc_score import xgboost as xgb num_features = ['RevolvingUtilizationOfUnsecuredLines', 'age', 'NumberOfTime30-59DaysPastDueNotWorse', 'DebtRatio', 'MonthlyIncome', 'NumberOfOpenCreditLinesAndLoans', 'NumberOfTimes90DaysLate', 'NumberRealEstateLoansOrLines', 'NumberOfTime60-89DaysPastDueNotWorse', 'NumberOfDependents'] cat_features = [] # 本数据集无类别特征,保留扩展位 numeric_transformer = Pipeline(steps=[ ('imputer', SimpleImputer(strategy='median')), ('scaler', StandardScaler()) ]) preprocessor = ColumnTransformer( transformers=[ ('num', numeric_transformer, num_features) ]) clf = xgb.XGBClassifier( n_estimators=600, learning_rate=0.05, max_depth=4, subsample=0.8, colsample_bytree=0.8, scale_pos_weight=9, # 粗略平衡 eval_metric='auc', random_state=42 ) model = Pipeline(steps=[('prep', preprocessor), ('xgb', clf)])训练脚本一句话搞定:
# train.py model.fit(X_train, y_train) print("AUC:", roc_auc_score(y_valid, model.predict_proba(X_valid)[:, 1]))Copilot 甚至帮我把if __name__ == "__main__"的模板也补全了,我只需关注数据路径。整个编码时间从 2 小时压缩到 20 分钟。
AI 写完特征工程,顺手把 API 骨架也搭了。我用的三文件结构:
main.py:FastAPI 入口,自动加载joblib模型。schemas.py:Pydantic 校验,防止非法字段入站。predict.py:核心预测函数,返回概率 + SHAP 值。# main.py from fastapi import FastAPI import joblib, os from schemas import CreditRequest, CreditResponse app = FastAPI(title="GiveMeSomeCredit-scoring") model = joblib.load(os.getenv("MODEL_PATH", "pipeline.pkl")) @app.post("/score", response_model=CreditResponse) def score(req: CreditRequest): prob = float(model.predict_proba([req.dict()])[0, 1]) return CreditResponse(default_probability=prob, customer_id=req.customer_id)Dockerfile 由 Copilot 补全,我只改了两行镜像源,构建体积从 1.8 GB 压到 380 MB。
让导师闭嘴的最佳方式,是把 SHAP 图甩给他。借助shap库两行代码:
import shap explainer = shap.TreeExplainer(model.named_steps['xgb']) shap_values = explainer.shap_values(X_valid) shap.summary_plot(shap_values, X_valid, max_display=10)AI 还贴心地生成了 Markdown 报告模板,把 Top5 特征的平均绝对 SHAP 值写进表格,我直接贴进论文,查重率 0%。
train_test_split默认shuffle=True,时间序列数据一定先按月份切,再喂给 AI。pipeline.pkl和data_version一起写进 SQLite,每次预测前比对哈希,不一致就报警告警。np.nan和inf分支。great_expectations每晚跑一遍特征分布,AI 只负责生成 JSON 配置,人工复核。save_model不兼容,CI 里加--no-binary强制回退,防止线上崩。整个毕设周期从 6 周压缩到 2 周,AUC 还提高了 2.3 个百分点。但最宝贵的收获是:
fillna,却不会告诉你“业务上 MonthlyIncome 为 0 代表失业”,需要你自己把规则写回 pipeline。把 AI 当“高级实习生”:脏活累活交给他,决策与背锅留给自己。
如果你也在被「Give Me Some Credit》折磨,不妨把旧代码拖进 VS Code,装个 Copilot,从pipeline.py开始重构。
跑通第一版后,你会和我一样发现:毕业设计不是“给点信用”,而是“给点 AI 协作”,就能让信用评分更快、更稳、更解释得清。