KAGGLE竞赛实战2-捷信金融违约预测竞赛-part2-用lightgbm建立baseline

接着上一篇,用lightgbm建立baseline,发现模型效果得到了很大优化(模型分提升为0.73)

In[211]:

from sklearn.model_selection import cross_val_score,KFold

In[228]:

import lightgbm as lgb

In[229]:

from lightgbm import LGBMClassifier

In[232]:

from lightgbm import early_stopping

In[237]:

from sklearn.metrics import roc_auc_score

In[215]:

bad_chars=[':','""','\\','']

for feature in application_train.columns:

if any(bad_char in feature for bad_char in bad_chars):

print(f"Feature '{feature}'包含非法字符。")

In[216]:

#去掉特殊字符import pandas as pd

import re

def clean_column_names(df):

"""

清理DataFrame列名,去除特殊字符,使其符合JSON格式要求。

参数:

df (pd.DataFrame): 输入的DataFrame。

返回:

pd.DataFrame: 列名已清理的DataFrame。

"""

定义一个函数,用于替换单个列名中的特殊字符

def replace_chars(col_name):

替换掉所有非字母数字和下划线的字符

return re.sub(r'\W+', '_', col_name)

应用替换函数到所有列名

df.columns = [replace_chars(col) for col in df.columns]

return df

In[217]:

application_train=clean_column_names(application_train)

application_test=clean_column_names(application_test)

In[218]:

bad_chars=[':','""','\\','']

for feature in application_train.columns:

if any(bad_char in feature for bad_char in bad_chars):

print(f"Feature '{feature}'包含非法字符。")

In[238]:

def fit(train=application_train, valid=application_test):

"""

模型训练函数,

参数:train训练集

valid测试集

返回值:

valid_auc:验证集上AUC指标

feature_importances:特征重要性

test_results:测试集结果

"""

test = valid.copy()

x_train = train.drop(['SK_ID_CURR', 'TARGET'], axis=1)

y_train = train['TARGET']

五折交叉验证

folds = KFold(n_splits=5, shuffle=True, random_state=1412)

定义变量保存预测结果

oof_preds = np.zeros(y_train.shape[0])

test_preds = np.zeros(test.shape[0])

提取特征名

feature_names = list(x_train.columns)

空数组用于存储特征重要性值

feature_importance_values = np.zeros(len(feature_names))

实例化模型

lgb = LGBMClassifier(n_estimators=10000, early_stopping_round=200, random_state=24)

for fold_idx, (train_idx, valid_idx) in enumerate(folds.split(x_train)):

X = x_train.iloc[train_idx]

y = y_train.iloc[train_idx]

valid_X = x_train.iloc[valid_idx]

valid_y = y_train.iloc[valid_idx]

定义早停回调函数

callbacks = [early_stopping(stopping_rounds=200)]

拟合模型

lgb.fit(X, y, eval_set=[(X, y), (valid_X, valid_y)], callbacks=callbacks)

记录特征重要性

feature_importance_values += lgb.feature_importances_ / folds.n_splits

在验证集上进行预测

proba = lgb.predict_proba(valid_X, num_iteration=lgb.best_iteration_)

oof_preds[valid_idx] = proba[:, 1] # 选择正类概率

test_preds += lgb.predict_proba(test[feature_names], num_iteration=lgb.best_iteration_)[:, 1]

valid_auc = roc_auc_score(y_train, oof_preds)

feature_importances = pd.DataFrame({'feature': feature_names, 'importance': feature_importance_values})

test['TARGET'] = test_preds

return valid_auc, feature_importances, test[['SK_ID_CURR', 'TARGET']]

In[246]:

valid_auc,feature_importance,submission=fit(application_train[:50000],application_test)

#发现报错了Do not support special JSON characters in feature name,原因是有些列名里有特殊的字符,这是get_dummies时产生的

In[247]:

valid_auc #0.7421786519800682

In[248]:

#看下特征重要性

feature_importance.sort_values(by='importance',ascending=False)

In[250]:

submission.to_csv('baseline_model_lightgbm.csv',index=False)#提交后成绩0.73

In[251]:

application_train.to_csv('original_application_train.csv')#保存下结果

application_test.to_csv('original_application_test.csv')

相关推荐
skywalk81634 分钟前
copyparty 是一款使用单个 Python 文件实现的内网文件共享工具,具有跨平台、低资源占用等特点,适合需要本地化文件管理的场景
开发语言·python
Godspeed Zhao4 分钟前
自动驾驶中的传感器技术42——Radar(3)
人工智能·机器学习·自动驾驶
Godspeed Zhao6 分钟前
自动驾驶中的传感器技术41——Radar(2)
人工智能·机器学习·自动驾驶
BYSJMG9 分钟前
计算机毕设选题:基于Python+MySQL校园美食推荐系统【源码+文档+调试】
大数据·开发语言·python·mysql·django·课程设计·美食
非门由也1 小时前
《sklearn机器学习——数据预处理》类别特征编码
人工智能·机器学习·sklearn
焦耳加热7 小时前
阿德莱德大学Nat. Commun.:盐模板策略实现废弃塑料到单原子催化剂的高值转化,推动环境与能源催化应用
人工智能·算法·机器学习·能源·材料工程
CodeCraft Studio7 小时前
PDF处理控件Aspose.PDF教程:使用 Python 将 PDF 转换为 Base64
开发语言·python·pdf·base64·aspose·aspose.pdf
困鲲鲲8 小时前
Python中内置装饰器
python
摩羯座-185690305948 小时前
Python数据可视化基础:使用Matplotlib绘制图表
大数据·python·信息可视化·matplotlib
爱隐身的官人9 小时前
cfshow-web入门-php特性
python·php·ctf