随机抽取二维矩阵中多行或多列

原文: 随机抽取二维矩阵中多行或多列 0.052 2019.01.26 11:50:14 字数31 阅读2564
使用库numpy
创建一个二维数组

import numpy as nparray = np.arange(24).reshape((4,6))""" array([[ 0,1,2,3,4,5], [ 6,7,8,9, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]) """

【随机抽取二维矩阵中多行或多列】行与列随机抽取类似
行随机抽取
row_rand_array = np.arange(array.shape[0])np.random.shuffle(row_rand_array)row_rand = array[row_rand_array[0:2]]""" row_rand: array([[12, 13, 14, 15, 16, 17], [ 0,1,2,3,4,5]]) """

列随机抽取
col_rand_array = np.arange(array.shape[1])np.random.shuffle(col_rand_array)col_rand = array[:,col_rand_array[0:2]]""" col_rand: array([[ 1,5], [ 7, 11], [13, 17], [19, 23]]) """


    推荐阅读