def KFold(num, n_folds=5):
    """
    k折叠交叉验证算法,如果份数不能整除数据的数量
    会尽量分成相近的数量
    :param num: 所有数据的数量
    :param n_folds: k，分成k份
    :return:
    """
    folds = []
    indics = list(range(num))

    for i in range(n_folds):
        # 将一部分数据作为验证集
        valid_indics = indics[(i * num // n_folds):((i + 1) * num // n_folds)]
        # 剩余部分作为测试集
        train_indics = [index for index in range(0, (i * num // n_folds))]
        train_indics.extend([index for index in range((i + 1) * num // n_folds, num)])
        folds.append([train_indics, valid_indics])
    return folds