kaggle平台学习复习笔记 | 数据划分与模型集成

本文介绍了数据集的划分方法,如留出法、K折交叉验证,并探讨了它们在数据一致性分析中的作用。接着,展示了如何在Titanic数据集中应用这些方法,包括数据预处理和模型评估,最后讨论了不同机器学习模型的性能,如朴素贝叶斯、逻辑回归和随机森林等,并给出了在Kaggle比赛中的一些技巧。

数据集划分与交叉验证

数据集划分
通常有两种方法:

  1. 留出法(Hold-out) 适用于数据量大的情况
  2. K折交叉验证(K-fold CV) 适用于数据量一般情况 时间比较长
  3. 自助采样(Bootstrap) 较少使用

交叉验证得到的模型更加稳定.

数据一致性分析

在这里插入图片描述

理想情况下AUC接近0.5

sklearn中封装的一系列的数据划分的代码

# hold-out
from sklearn.model_selection import train_test_split

# K折交叉验证
from sklearn.model_selection import KFold
from sklearn.model_selection import RepeatedKFold

# K折分布保持交叉验证
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import RepeatedStratifiedKFold

# 时间序列划分方法
from sklearn.model_selection import TimeSeriesSplit

# booststrap 采样
from sklearn.utils import resample

构造数据集

X = np.zeros((20, 5))
Y = np.array([1]*5 + [2]*5 + [3]*5 + [4]*5)
print(X, Y)

在这里插入图片描述
留出法代码

# 直接按照比例拆分
train_X, val_X, train_y, val_y = train_test_split(X, Y, test_size = 0.2)
print(train_y, val_y)

# 按照比例 & 标签分布划分
train_X, val_X, train_y, val_y = train_test_split(X, Y, test_size = 0.2, stratify=Y)
print(train_y, val_y)

两种划分效果如下:
在这里插入图片描述
可以看到,第一种划分不均匀,当添加参数stratify=Y时,
第二种划分方式变的均匀了。

K折交叉验证
不均匀的分布方式 KFold

kf = KFold(n_splits=5)
for train_idx, test_idx, in kf.split(X, Y):
    print(train_idx, test_idx)
    print('Label', Y[test_idx])
    print('')

在这里插入图片描述
均匀分布方式 StratifiedKFold

# 5折划分方式
kf = StratifiedKFold(n_splits=5)
for train_idx, test_idx, in kf.split(X, Y):
    print(train_idx, test_idx)
    print('Label', Y[test_idx])
    print('')

在这里插入图片描述
均匀重复采样 RepeatedStratifiedKFold

kf = RepeatedStratifiedKFold(n_splits=5, n_repeats=2)
for train_idx, test_idx, in kf.split(X, Y):
    print(train_idx, test_idx)
    print('Label', Y[test_idx])
    print('')

在这里插入图片描述

模型集成方法

机器学习的模型集成

  1. 模型集成和集成学习
    在这里插入图片描述

  2. Vote和Blend
    Vote主要用于分类任务
    Blend主要用于回归任务
    主要方法:
    对结果简单投票或者平均
    加权权重可以修改

  3. Stacking
    在这里插入图片描述
    多次学习
    在第一次学习的基础上生成新的特征,
    在将新的特征拼接到训练集上,进行新的学习

深度学习的模型集成

  1. 数据扩增
    在这里插入图片描述
    数据集和测试集都可以进行数据扩增

  2. snapshot Ensemble
    在这里插入图片描述
    保存多个局部最优解,找到最终最优解

Titanic为例的简单应用

  1. Load data
import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

import warnings
warnings.filterwarnings('ignore')
#import train and test CSV files
train_df = pd.read_csv("../input/train.csv")
test_df = pd.read_csv("../input/test.csv")

#take a look at the training data
train_df.describe(include="all")

在这里插入图片描述

print(train_df.columns)

在这里插入图片描述

train_df.sample(5)

在这里插入图片描述

#see a summary of the training dataset
train_df.describe(include = "all")
  1. Data Visualization
# 性别和幸存的关系
sns.barplot(x="Sex", y="Survived", data=train_df)

在这里插入图片描述
类似的查看其他字段和幸存率的关系

#draw a bar plot of survival by Pclass
sns.barplot(x="Pclass", y="Survived", data=train_df)

#draw a bar plot for SibSp vs. survival
sns.barplot(x="SibSp", y="Survive
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lijiamingccc

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

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

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

打赏作者

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

抵扣说明:

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

余额充值