Python实现随机取一个矩阵数组的某几行
废话不多说了,直接上代码吧!
import numpy as np array = np.array([0, 0]) for i in range(10): array = np.vstack((array, [i+1, i+1])) print(array) # [[ 0 0] # [ 1 1] # [ 2 2] # [ 3 3] # [ 4 4] # [ 5 5] # [ 6 6] # [ 7 7] # [ 8 8] # [ 9 9] # [10 10]] rand_arr = np.arange(array.shape[0]) np.random.shuffle(rand_arr) print(array[rand_arr[0:5]]) # [[9 9] # [4 4] # [1 1] # [5 5] # [8 8]] np.random.shuffle(rand_arr) print(array[rand_arr[0:5]]) # [[10 10] # [ 3 3] # [ 4 4] # [ 8 8] # [ 5 5]]
以上这篇Python实现随机取一个矩阵数组的某几行就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。