人工智能训练师(三级)实操考试 — 2.2.1 智能信用评分Logistic回归模型开发与测试

← 返回题目列表 考核时间:20 分钟 20:00
工作任务

互联网金融飞速发展,使得个人金融理财变得越来越容易。而其中信用评分技术是一种对贷款申请人(信用卡申请人)做风险评估分值的统计模型,可以根据客户提供的资料、客户的历史数据、第三方平台数据(芝麻分、京东、微信等),对客户的信用进行评估。

现要求根据提供的finance数据集,补全2.2.1.ipynb代码。选择合适的特征,开发一个申请的评分模型,利用测试工具对模型进行测试,并对测试结果进行分析。

具体要求

(1)正确加载数据集,显示前五行的数据。

(2)使用Logistic模型进行模型训练,设定自变量和因变量,进行模型训练,将训练好的模型以文件名2.2.1_model.pkl保存。

(3)使用测试工具对模型进行测试,并记录测试结果。

(4)对测试结果进行分析,编写测试报告。

(5)运用工具分析算法中错误案例产生的原因并进行纠正,重新得到模型训练结果。

数据集说明

SeriousDlqin2yrs: 过去两年是否严重拖欠(1=有,0=无)

RevolvingUtilizationOfUnsecuredLines: 未偿还信用额度占比

age: 客户年龄   DebtRatio: 债务比率

MonthlyIncome: 月收入   NumberOfDependents: 依赖人数

NumberOfTime30-59DaysPastDueNotWorse: 逾期30-59天次数

NumberOfTimes90DaysLate: 逾期超90天次数

NumberOfTime60-89DaysPastDueNotWorse: 逾期60-89天次数

NumberOfOpenCreditLinesAndLoans: 信贷账户数量

NumberRealEstateLoansOrLines: 房产相关贷款数量

注意事项
  • 在代码的 橙色下划线 处填写正确的 Python 代码
  • 请勿修改源代码的其他部分
  • 填写完成后点击 "检查答案" 查看评分
  • 点击 "显示答案" 可直接查看所有参考答案
  • 点击 "重置" 清空所有填写内容
请在下划线处填写代码
当前得分
0 / 13
In [1] — 导入库 & 加载数据 2分
import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression import pickle from sklearn.metrics import classification_report from imblearn.over_sampling import SMOTE # 加载数据 1分 data = # 显示前五行的数据 1分 print()
In [2] — 特征选择、数据分割与模型训练 6分
# 选择自变量和因变量 X = data.drop(['SeriousDlqin2yrs', 'Unnamed: 0'], axis=1) y = data['SeriousDlqin2yrs'] # 分割训练集和测试集(测试集20%) 2分 X_train, X_test, y_train, y_test = (, random_state=42) # 训练Logistic回归模型(最大迭代次数为1000次) 1分 model = # 训练 Logistic 回归模型 1分 # 保存模型 1分 with open('2.2.1_model.pkl', 'wb') as file: pickle. # 预测并保存结果 1分 y_pred = pd.DataFrame(y_pred, columns=['预测结果']).to_csv('2.2.1_results.txt', index=False)
In [3] — 生成测试报告与评估 1分
# 生成测试报告 report = classification_report(y_test, y_pred, zero_division=1) with open('2.2.1_report.txt', 'w') as file: file.write(report) # 分析测试结果 1分 accuracy = print(f"模型准确率: {accuracy:.2f}")
In [4] — SMOTE重采样与模型纠正 4分
# 处理数据不平衡 1分 smote = SMOTE(random_state=42) X_resampled, y_resampled = # 重新训练模型 1分 # 重新预测 1分 y_pred_resampled = # 保存新结果 pd.DataFrame(y_pred_resampled, columns=['预测结果']).to_csv('2.2.1_results_xg.txt', index=False) # 生成新的测试报告 report_resampled = classification_report(y_test, y_pred_resampled, zero_division=1) with open('2.2.1_report_xg.txt', 'w') as file: file.write(report_resampled) # 分析新的测试结果 1分 accuracy_resampled = print(f"重新采样后的模型准确率: {accuracy_resampled:.2f}")
答案检查结果