- 网格搜索
- 随机搜索(简单介绍,非重点 实战中很少用到,可以不了解)
- 贝叶斯优化(2种实现逻辑,以及如何避开必须用交叉验证的问题)
- time库的计时模块,方便后人查看代码运行时长
今日作业: 对于信贷数据的其他模型,如LightGBM和KNN 尝试用下贝叶斯优化和网格搜索
X = data.drop(['Credit Default'], axis = 1)
Y = data['Credit Default']
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2, random_state = 42)
print(f"训练集形状={X_train.shape},测试集形状={X_test.shape}")
lgb_model = lgb.LGBMClassifier(random_state = 42)
lgb_model.fit(X_train, y_train)
lgb_pred = lgb_model.predict(X_test)
print("\nLightGBM 分类报告:")
print(classification_report(y_test, lgb_pred))
print("LightGBM 混淆矩阵:")
print(confusion_matrix(y_test, lgb_pred))
lgb_accuracy = accuracy_score(y_test, lgb_pred)
lgb_precision = precision_score(y_test, lgb_pred)
lgb_recall = recall_score(y_test, lgb_pred)
lgb_f1 = f1_score(y_test, lgb_pred)
print("LGBM 模型评估指标:")
print(f"准确率:{lgb_accuracy:.4f}")
print(f"精确率:{lgb_precision:.4f}")
print(f"召回率:{lgb_recall:.4f}")
print("--- 1.默认参数lightgbm (训练集 -> 测试集) ---")
import time
start_time = time.time()
lgb_model = lgb.LGBMClassifier(random_state=42)
lgb_model.fit(X_train, y_train)
lgb_pred = lgb_model.predict(X_test)
end_time = time.time()
print(f"训练与预测耗时: {end_time - start_time:.4f} 秒")
print("\n默认lightgbm 在测试集上的分类报告:")
print(classification_report(y_test, lgb_pred))
print("默认lightgbm 在测试集上的混淆矩阵:")
print(confusion_matrix(y_test, lgb_pred))
训练与预测耗时: 0.1345 秒
默认lightgbm 在测试集上的分类报告:
precision recall f1-score support
0 0.77 0.95 0.85 1059
1 0.72 0.31 0.44 441
accuary 0.76
macro avg 0.75 0.63 0.64 1500
weighted avg 0.75 0.76 0.73 1500
默认lightgbm 在测试集上的混淆矩阵:
[[1006 53]
[ 303 138]]
使用网格搜索的方法进行优化
X = data.drop(['Credit Default'], axis = 1)
Y = data['Credit Default']
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2, random_state = 42)
print(f"训练集形状={X_train.shape},测试集形状={X_test.shape}")
print("\n--- 2.网格搜索优化lightgbm (训练集 -> 测试集) ---")
import time
from sklearn.model_selection import GridSearchCV
param_grid ={
'n_estimators': [50, 100, 200],
'max_depth': [None, 10, 20, 30],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
}
grid_search = GridSearchCV(estimator=lgb.LGBMClassifier(random_state=42),
param_grid=param_grid,
cv=5,
n_jobs=-1,
scoring='accuracy')
start_time = time.time()
grid_search.fit(X_train, y_train)
end_time = time.time()
print(f"网格搜索耗时: {end_time - start_time:.4f} 秒")
print("最佳参数: ", grid_search.best_params_)
best_model = grid_search.best_estimator_
best_pred = best_model.predict(X_test)
print("\n网格搜索优化后的lightgbm 在测试集上的分类报告:")
print(classification_report(y_test, best_pred))
print("网格搜索优化后的lightgbm 在测试集上的混淆矩阵:")
print(confusion_matrix(y_test, best_pred))
没有结果,不知道是不是电脑带不动的原因,把代码发给AI,AI说我的代码可以正常跑,但是就是跑不动,还报错,我真服了。。。。。
贝叶斯优化
from skopt import BayesSearchCV
from skopt.space import Integer
import lightgbm as lgb
lgb.LGBMClassifier()
from sklearn.metrics import classification_report, confusion_matrix
import time
# 定义要搜索的参数空间
search_space = {
'n_estimators': Integer(50, 200),
'max_depth': Integer(10, 30),
'min_samples_split': Integer(2, 10),
'min_samples_leaf': Integer(1, 4)
}
# 创建贝叶斯优化搜索对象
bayes_search = BayesSearchCV(
estimator=lgb.LGBMClassifier(random_state=42),
search_spaces=search_space,
n_iter=32, # 迭代次数,可根据需要调整
cv=5, # 5折交叉验证,这个参数是必须的,不能设置为1,否则就是在训练集上做预测了
n_jobs=-1,
scoring='accuracy'
)
start_time = time.time()
# 在训练集上进行贝叶斯优化搜索
bayes_search.fit(X_train, y_train)
end_time = time.time()
print(f"贝叶斯优化耗时: {end_time - start_time:.4f} 秒")
print("最佳参数: ", bayes_search.best_params_)
# 使用最佳参数的模型进行预测
best_model = bayes_search.best_estimator_
best_pred = best_model.predict(X_test)
print("\n贝叶斯优化后的lightgbm 在测试集上的分类报告:")
print(classification_report(y_test, best_pred))
print("贝叶斯优化后的lightgbm 在测试集上的混淆矩阵:")
print(confusion_matrix(y_test, best_pred))
过程:定义要搜索的参数空间,创造贝叶斯优化搜索对象,在训练集上进行贝叶斯优化搜索,用最佳参数模型在测试集上进行预测。@浙大疏锦行
135

被折叠的 条评论
为什么被折叠?



