gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
分层划分从1和非1中,分别抽取0.3;组合成测试集,使用numpy:shuffle打乱,choose选择,实现trainx, trainy, testx, testy =…
- User
分层划分从1和非1中,分别抽取0.3;组合成测试集,使用numpy:shuffle打乱,choose选择,实现trainx, trainy, testx, testy = Train_Test_Split(X,Y, test_size, random_seed)
- Assistant
import numpy as np
def Train_Test_Split(X, Y, test_size, random_seed): np.random.seed(random_seed)
code# 分别获取1和非1的索引 index_1 = np.where(Y == 1)[0] index_non_1 = np.where(Y != 1)[0] # 从1和非1中分别抽取0.3的样本数量 sample_size_1 = int(len(index_1) * test_size) sample_size_non_1 = int(len(index_non_1) * test_size) # 随机选择1的样本索引 np.random.shuffle(index_1) test_index_1 = index_1[:sample_size_1] train_index_1 = index_1[sample_size_1:] # 随机选择非1的样本索引 np.random.shuffle(index_non_1) test_index_non_1 = index_non_1[:sample_size_non_1] train_index_non_1 = index_non_1[sample_size_non_1:] # 组合测试集和训练集的样本索引 train_index = np.concatenate((train_index_1, train_index_non_1)) test_index = np.concatenate((test_index_1, test_index_non_1)) # 根据索引获取训练集和测试集的数据 trainX = X[train_index] trainY = Y[train_index] testX = X[test_index] testY = Y[test_index] return trainX, trainY, testX, testY