前言:在通过使用特征工程,模型调优等诸多方法的实践后,在单模型已经是比价再难有突破的情况下,我们可以尝试使用模型的融合,本文将重点讲述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("Stratified Train Index:", train_index)
print("Stratified Test Index:", test_index)
print("