基于LightGBM的信用卡使用意图预测模型
基于LightGBM的信用卡使用意图预测模型
代码功能:
本代码使用LightGBM模型对信用卡使用意图进行预测,并对模型进行训练和测试。
**代码详解:**pythonimport lightgbm as lgbfrom sklearn.model_selection import RandomizedSearchCVfrom sklearn.metrics import roc_auc_score, classification_report, confusion_matrixfrom scipy.stats import randint as sp_randint
1. 定义LGBM分类器lg = lgb.LGBMClassifier()
2. 定义参数字典params = { 'boosting_type': ['gdbt', 'dart', 'rf'], 'max_depth': sp_randint(-1, 20), 'learning_rate': [0.1, 0.2, 0.3, 0.4, 0.5], 'n_estimators': sp_randint(50, 400)}
3. 随机搜索最佳参数rscv = RandomizedSearchCV(lg, param_distributions=params, cv=5, scoring='roc_auc', n_iter=10, n_jobs=-1)rscv.fit(x, y)
4. 使用最佳参数重新定义LGBM分类器lg = lgb.LGBMClassifier(**rscv.best_params_)
5. 训练模型lg.fit(x_train, y_train)
6. 训练集预测及评估y_train_pred = lg.predict(x_train)y_train_prob = lg.predict_proba(x_train)[:, 1]print('ROC score for train is :', roc_auc_score(y_train, y_train_prob))print('Classification report for train:
')print(classification_report(y_train, y_train_pred))print(confusion_matrix(y_train, y_train_pred))
7. 测试集预测及评估y_test_pred = lg.predict(x_test)y_test_prob = lg.predict_proba(x_test)[:, 1]print('ROC score for test is :', roc_auc_score(y_test, y_test_prob))print('Classification report for test :
')print(classification_report(y_test, y_test_pred))print(confusion_matrix(y_test, y_test_pred))
代码步骤:
- 导入必要的库,包括lightgbm、RandomizedSearchCV、roc_auc_score、classification_report和confusion_matrix。2. 定义一个LGBM分类器,命名为lg。3. 定义一个参数字典params,包括'boosting_type'、'max_depth'、'learning_rate'和'n_estimators'等参数,用于随机搜索最佳参数。4. 使用RandomizedSearchCV函数进行随机搜索,找到最佳参数组合。5. 使用最佳参数组合重新定义LGBM分类器。6. 使用训练数据集x_train和y_train对LGBM分类器进行训练。7. 对训练数据集进行预测,并计算ROC得分、分类报告和混淆矩阵等指标。8. 对测试数据集x_test进行预测,并计算ROC得分、分类报告和混淆矩阵等指标。
总结:
本代码利用LightGBM模型实现了信用卡使用意图的预测,并通过随机搜索优化了模型参数,最终在测试集上取得了较好的预测效果。
原文地址: https://www.cveoy.top/t/topic/fVPp 著作权归作者所有。请勿转载和采集!