生成固定大小的numpy数组的随机子样本

时间:2018-04-28 11:40:10

标签: python arrays numpy matrix random

我搜索过这个并找不到它。想象一下,我有一个大小为N的numpy数组。现在我想生成它的大小为M的子样本。基本上我想从这个数组中随机选择M个元素。 N> = M.我该怎么做?

1 个答案:

答案 0 :(得分:3)

np.random.choice()

>>> N = 100; M = 10
>>> a = np.arange(0, N)
>>> np.random.choice(a, M, replace=False)
array([22, 81, 63,  7, 10, 52, 30, 33, 18, 41])

使用replace=False,您不会重复,在这种情况下,M必须是< = N。

编辑:2d案例:

>>> np.arange(0,120).reshape(10,12)
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],
       [ 24,  25,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35],
       [ 36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47],
       [ 48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59],
       [ 60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,  71],
       [ 72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83],
       [ 84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95],
       [ 96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107],
       [108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]])
>>> idx = np.arange(0, 10)
>>> rand_idx = np.random.choice(idx, 5, replace=False)
>>> a[rand_idx]
array([[24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35],
       [36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47],
       [84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95],
       [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
       [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]])