对于数据科学应用程序,我需要先随机整理矩阵的行,然后再进行处理。
有没有一种方法,不仅可以获取索引,对索引进行改组,然后将经过改组的索引传递给矩阵?如:
indx = np.asarray(list(range(0, data.shape[0], 1)))
shufIndx = shuffle(indx)
data = data[shufIndx,:]
return (data)
谢谢!
对于数据科学应用程序,我需要先随机整理矩阵的行,然后再进行处理。
有没有一种方法,不仅可以获取索引,对索引进行改组,然后将经过改组的索引传递给矩阵?如:
indx = np.asarray(list(range(0, data.shape[0], 1)))
shufIndx = shuffle(indx)
data = data[shufIndx,:]
return (data)
谢谢!
With python
(not numpy
), you can directly random.shuffle
the rows:
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]]