前言:在通过使用特征工程,模型调优等诸多方法的实践后,在单模型已经是比价再难有突破的情况下,我们可以尝试使用模型的融合,本文将重点讲述StratifiedKFold与stacking的两种模型方法,包括源码的示意过程。
StratifiedKFold用法类似Kfold,但是他是分层采样,确保训练集,测试集中各类别样本的比例与原始数据集中相同。
from sklearn.model_selection import StratifiedKFold
X = np.array([[1, 2, 3, 4],
[11, 12, 13, 14],
[21, 22, 23, 24],
[31, 32, 33, 34],
[41, 42, 43, 44],
[51, 52, 53, 54],
[61, 62, 63, 64],
[71, 72, 73, 74]])
y = np.array([1, 1, 0, 0, 1, 1, 0, 0])
stratified_folder = StratifiedKFold(n_splits=4, random_state=0, shuffle=False)
for train_index, test_index in stratified_folder.split(X, y):
print("Stratif
本文介绍了在Sklearn中如何使用StratifiedKFold进行分层采样,并探讨了模型融合方法Stacking的思路和实现。通过Stacking,利用多个弱学习器的输出作为新特征,训练一个次级学习器来提高预测性能。文章还提到了数据标准化在不同模型中的应用,并提供了Stacking的代码示例。
订阅专栏 解锁全文
625





