对于DataScience应用程序,在开始处理矩阵之前,我需要随机调整矩阵的行数。
有没有一种方法可以做到这一点,而不是简单地获取索引,打乱索引,然后将打乱后的索引传递给矩阵?如下所示:
indx = np.asarray(list(range(0, data.shape[0], 1)))
shufIndx = shuffle(indx)
data = data[shufIndx,:]
return (data)谢谢!
发布于 2020-05-24 11:00:17
使用python (而不是numpy),您可以直接random.shuffle行:
import random
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix)
random.shuffle(matrix) # random.shuffle mutates the input and returns None
print(matrix)示例输出:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[7, 8, 9], [1, 2, 3], [4, 5, 6]]发布于 2020-05-24 14:41:35
numpy.random.shuffle()应该可以做到这一点
import numpy as np
mat = np.array(range(16)).reshape(4,4)
print(mat,'\n')
np.random.shuffle(mat)
print(mat)输出:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
[[12 13 14 15]
[ 4 5 6 7]
[ 0 1 2 3]
[8 9 10 11]] https://stackoverflow.com/questions/61981070
复制相似问题