训练集,测试集划分

训练集,测试集划分

如果只想划分一个训练集,测试集

from sklearn.model_selection import StratifiedShuffleSplit
import numpy as np
import os
import shutil

if name == "main":
X = np.arange(100)
Y = np.zeros(100)
ss = StratifiedShuffleSplit(n_splits=1)

for train_index, test_index in ss.split(X, Y):
	print("TRAIN:", train_index, "\n", "TEST:", test_index)

另一种方法:使用random函数
import random
import numpy as np

if name == "main":
list = np.arange(100)
slice = random.sample(list,100)
train = slice[:int(len(slice)0.95)]
test = slice[int(len(slice)
0.95)::]
print(slice)
print('\n')
print(train)
print('\n')
print(test)
print('\n')

参考:
[1] python中数据集划分函数StratifiedShuffleSplit的使用 https://blog.csdn.net/m0_38061927/article/details/76180541
[2] sk-learn中StratifiedShuffleSplit()函数 实现对数据集的划分 https://blog.csdn.net/u012193416/article/details/79313601
[3] Python模块:生成随机数模块random https://blog.csdn.net/pipisorry/article/details/39086463

posted @ 2022-06-22 09:41  xiaoxuxli  阅读(47)  评论(0编辑  收藏  举报