2024-04-28 07:07:35 发布
网友
你可以使用numpy.random.shuffle
import numpy as np N = 4601 data = np.arange(N*58).reshape(-1, 58) np.random.shuffle(data) a = data[:int(N*0.6)] b = data[int(N*0.6):int(N*0.8)] c = data[int(N*0.8):]
如果您想用相同的第一维连续地洗牌多个数组x,y,z,HYRY的答案的一个补充:x.shape[0] == y.shape[0] == z.shape[0] == n_samples。
x.shape[0] == y.shape[0] == z.shape[0] == n_samples
你可以:
rng = np.random.RandomState(42) # reproducible results with a fixed seed indices = np.arange(n_samples) rng.shuffle(indices) x_shuffled = x[indices] y_shuffled = y[indices] z_shuffled = z[indices]
然后像海瑞的回答那样,对每个洗牌数组进行分割。
如果要随机选择行,可以使用标准Python库中的random.sample:
random.sample
import random population = range(4601) # Your number of rows choice = random.sample(population, k) # k being the number of samples you require
无需替换的random.sample示例,因此不必担心重复的行会在choice中结束。给定一个名为matrix的numpy数组,您可以通过切片来选择行,如下所示:matrix[choice]。
choice
matrix
matrix[choice]
当然,k可以等于总体中的总元素数,然后choice将包含行索引的随机顺序。然后你可以根据自己的需要对choice进行分区。
k
你可以使用numpy.random.shuffle
如果您想用相同的第一维连续地洗牌多个数组x,y,z,HYRY的答案的一个补充:
x.shape[0] == y.shape[0] == z.shape[0] == n_samples
。你可以:
然后像海瑞的回答那样,对每个洗牌数组进行分割。
如果要随机选择行,可以使用标准Python库中的
random.sample
:无需替换的
random.sample
示例,因此不必担心重复的行会在choice
中结束。给定一个名为matrix
的numpy数组,您可以通过切片来选择行,如下所示:matrix[choice]
。当然,
k
可以等于总体中的总元素数,然后choice
将包含行索引的随机顺序。然后你可以根据自己的需要对choice
进行分区。相关问题 更多 >
编程相关推荐