kfold

import numpy as np
from sklearn.model_selection import GroupKFold,KFold
def group_k_fold():
    '''
    K-fold iterator variant with non-overlapping groups.
    根据组来划分数据,不同组的类别与数据的折数相等。相同组里面的数据不会出现在两个不同的折中。

    The same group will not appear in two different folds (the number of
    distinct groups has to be at least equal to the number of folds).

    The folds are approximately balanced in the sense that the number of
    distinct groups is approximately the same in each fold.

    '''
    X = np.array([[1, 2], [3, 4], [5, 6], [7, 8],[9,10],[10,11]])
    y = np.array([1, 2, 3, 4,5,6])
    groups = np.array([0, 0, 2, 2, 3, 3])
    group_kfold = GroupKFold(n_splits=3)
    # n_splits: int, default = 5 Number of folds.Must be at least of 2
    group_kfold.get_n_splits(X, y, groups)
    print(group_kfold)

    for train_index, test_index in group_kfold.split(X, y, groups):
        print("TRAIN:", train_index, "TEST:", test_index)

        X_train, X_test = X[train_index], X[test_index]

        y_train, y_test = y[train_index], y[test_index]

        print('\n X_train:', X_train, '\n X_test:', X_test, '\n y_train:', y_train, '\n y_test:', y_test)
        print('**** step ****')

def k_fold():
    '''
    将数据随机地分成K等份,每次留一份作为测试集,其余的作为训练集。进行K次

    '''
    x = ['a', 'b', 'c', 'd', 'e', 'f']
    kf = KFold(n_splits=3)  # 先将KFold实例化  3折就是将训练数据三等分,每次取其中的1/3作为测试集,其余的是训练集
    for train, test in kf.split(x):  # 调用split方法,将原始数据集划分
        print(train, test)

group_k_fold()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值