数据挖掘竞赛预测模型——五折交叉验证

使用catboost进行五折交叉验证

import numpy as np
import pandas as pd
import catboost as cbt
from sklearn.metric import f1_score
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold


train_data = pd.read_csv('.....')
train_label = pd.read_csv('.....')
test_data = pd.read_csv('.....')


cat_list = [] #catboost中需要处理的离散特征属性列
oof = np.zeros(train_data.shape[0])  #训练集长度
prediction = np.zeros(test_data.shape[0])     #测试集长度
seeds = [2017,2018,2019,2020]     #随机种子
num_model_seed = 1


train_x, test_x, train_y, test_y = train_test_split(train_data, train_label, test_size=0.2, random_state = 2019)     #拆分训练集
for model_seed in range(num_model_seed):     #选用几个随机种子
    oof_cat = np.zeros(train_data.shape[0])
    prediction_cat = np.zeros(test_data.shape[0])
    skf = StratifiedKFold(n_splits=5, random_state=seeds[model_seed], shuffle=True) #五折交叉验证,shuffle表示是否打乱数据,若为True再设定随机种子random_state
    for index, (train_index, test_index) in enumerate(skf.split(train_data, train_label)): #将数据五折分割
        train_x, test_x, train_y, test_y = train_data.iloc[train_index], test_data.iloc[test_index], train_label.iloc[train_index], train_label.iloc[test_index]
        cbt_model = cbt.CatBoostClassifier(iterations=5000, learning_rate=0.1, max_depth=7, verbose=100, early_stopping_rounds=500,task_type='GPU', eval_metric='F1',cat_features=cat_list)     #设置模型参数,verbose表示每100个训练输出打印一次
        cbt_model.fit(train_x, train_y, eval_set=(test_x, test_y)) #训练五折分割后的训练集
        gc.collect() #垃圾清理,内存清理
        oof_cat[test_index] += cbt_model.predict_proba(test_x)[:,1] #
        prediction_cat += cbt_model.predict_proba(test_data)[:,1]/5
    print('F1', f1_score(train_label, np.round(oof_cat)))
    oof += oof_cat / num_model_seed     #五折训练集取均值
    prediction += prediction_cat / num_model_seed #测试集取均值
print('score', f1_score(train_label, np.round(oof)))

##结果写入csv文件
submit = test_data['id']
submit['label'] = (prediction>=0.499).astype(int)
print(submit['label'].value_counts())
submit.to_csv('submission.csv',index=False)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

o0xgw0o

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值