美文网首页
python打乱数据

python打乱数据

作者: 追求科技的足球 | 来源:发表于2020-09-29 10:59 被阅读0次

1、使用index手动打乱

使用numpy中的shuffle打乱index,然后更新索引

代码:

import numpy as np

X = np.array(X)
y = np.array(y)
# 打乱
np.random.seed(666)
index = [i for i in range(len(X))]
np.random.shuffle(index)
X = X[index]
y = y[index]

2、使用sklearn.model_selection中的train_test_split自动打乱

可选test_size比例,random_state(随机数种子,固定住每次随机的不会改变),stratify(按照x或y的比例,可以用于数据分布不均匀的场景)

代码:

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=1, stratify=y)

相关文章

网友评论

      本文标题:python打乱数据

      本文链接:https://www.haomeiwen.com/subject/pezduktx.html