from sklearn.model_selection import KFold
import pandas as pd
data = pd.read_csv("F:\\PaperCode\\Mypaper_python_code\\data\\dataset_split\\data_trainANDval.tsv", sep = "\t")
X = data.iloc[:, 0]
y = data.iloc[:, 1]
# print(X)
# print(type(X))
kf = KFold(n_splits=5, shuffle=False) # 初始化KFold
# 存放5折的训练集划分
X_train_files = []
y_train_files = []
X_test_files = []
y_test_files = []
p = 0
for train_index , test_index in kf.split(X): # 调用split方法切分数据
# # print('train_index:%s , test_index: %s ' %(train_index,test_index))
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
# 保存方式:
# 1.保存到列表
X_train_files.append(X_train)
y_train_files.append(y_train)
X_test_files.append(X_test)
y_test_files.append(y_test)
# 2.存入tsv文件中
X_train.to_csv("F:\\PaperCode\\Mypaper_python_code\\data\\dataset_split\\" + str(p) + "\\X_train.tsv", index=False)
y_train.to_csv("F:\\PaperCode\\Mypaper_python_code\\data\\dataset_split\\" + str(p) + "\\y_train.tsv", index=False)
X_test.to_csv("F:\\PaperCode\\Mypaper_python_code\\data\\dataset_split\\" + str(p) + "\\X_test.tsv", index=False)
y_test.to_csv("F:\\PaperCode\\Mypaper_python_code\\data\\dataset_split\\" + str(p) + "\\y_test.tsv", index=False)
p += 1
# print(y_test_files)
【交叉验证】k折交叉验证_从文件中划分
于 2024-03-22 08:47:51 首次发布