LoginSignup
15
16

More than 5 years have passed since last update.

[Python]Random処理(作成、選択、並べ替え)

Last updated at Posted at 2016-03-04

Random

ランダムなIndexがほしい時

numpy.random.randint(low, high=None, size=None)
low は含まれるが、highは含まれない。
分布は一様分布

In [49]: np.random.randint(0,100,size=10)
Out[49]: array([91, 64, 71, 65, 94, 58, 53, 14, 49, 52])

In [50]: np.random.randint(0,100,size=(2,3))
Out[50]: 
array([[18, 30, 22],
       [95, 34, 85]])

In [51]: x = np.random.randint(0,100,size=1000)

randint.png

一様分布(uniform distribution)

numpy.random.rand(d0, d1, ..., dn)

In [12]: np.random.rand(10)
Out[12]: 
array([ 0.88113083,  0.20320019,  0.32004609,  0.37492399,  0.14972709,
        0.12742709,  0.75792997,  0.61045003,  0.04126539,  0.89053443])

In [13]: np.random.rand(2,3)
Out[13]: 
array([[ 0.9020553 ,  0.83688775,  0.28991469],
       [ 0.83150826,  0.13279892,  0.12171419]])

In [14]: x = np.random.rand(10000)

uniform_distribution.png

正規分布(standard normal distribution)

numpy.random.randn(d0, d1, ..., dn)

In [17]: np.random.randn(10)
Out[17]: 
array([-0.82515182,  0.76906557, -0.7280289 , -0.01442234, -1.06237328,
        0.39381346, -1.15061995, -0.09937671,  0.66138369,  1.21645816])

In [18]: np.random.randn(2,3)
Out[18]: 
array([[ 0.80548817, -1.2232574 , -0.33438786],
       [-1.85177797, -1.30872667, -0.28216313]])

In [19]: x = np.random.randn(10000)

standard_normal_distribution.png

In [41]: def N(mean, var, shape):
    ...:     return np.sqrt(var)*np.random.randn(*shape) + mean
    ...: 
    ...: 

In [42]: N(5.0, 10.0, [10])
Out[42]: 
array([ 4.86729898,  6.7609417 ,  4.26180116,  3.88441367,  5.98194004,
       -1.13748272,  5.74417308,  4.0822398 ,  8.02914619,  6.88882414])

In [43]: N(5.0, 10.0, [2, 3])
Out[43]: 
array([[ 4.97538772,  5.79304735,  0.39961495],
       [ 0.38103024,  3.00300634, -0.95105639]])

In [44]: x = N(5.0, 10.0, [10000])

standard_normal_distribution2.png

numpy arrayをshuffleしたいとき

numpy.random.shuffle(x)
※引数で渡したデータはshuffleされる

In [55]: A = np.arange(100)

In [56]: np.random.shuffle(A)

In [57]: A
Out[57]: 
array([77, 74, 91, 29, 16, 23, 54, 10,  4, 21, 89, 32, 28, 52, 47, 27, 68,
        3, 73, 64, 63, 33, 39, 67, 62, 37, 66, 55, 57, 83, 98, 35, 13, 84,
       90, 26, 45, 44, 82, 41, 15, 42, 79, 65, 43, 24, 95, 72, 78,  1, 69,
       51, 18, 61, 31, 50,  2, 46, 94, 17, 70,  7, 85,  6, 87, 56, 40, 60,
       19, 25,  0, 88, 30,  5, 86, 34, 93,  9, 48, 20,  8, 36, 49, 11, 99,
       97, 71, 38, 75, 59, 58, 22, 14, 92, 76, 80, 53, 96, 12, 81])

多次元のnumpy arrayをshuffleしたときの動作
多次元のnumpy arrayをshuffleした場合は、最初のindex(最初のdimension)に沿ってshuffleされる。

In [19]: A = np.arange(9).reshape(3,3)

In [20]: print A
[[0 1 2]
 [3 4 5]
 [6 7 8]]

In [21]: np.random.shuffle(A)

In [22]: print A
[[3 4 5]
 [0 1 2]
 [6 7 8]]

shuffleされたIndexがほしい時

numpy.random.permutation(x)
データをshuffleしてほしくない時は、IndexをShuffleして渡すと良い

In [58]: A = np.arange(100)

In [59]: x = np.random.permutation(100)

In [60]: A[x]
Out[60]: 
array([14, 36, 70, 75,  7, 80, 19, 99, 40, 97, 31, 20, 87, 68, 78, 50, 73,
       66, 12, 45, 69, 72, 24, 42, 58, 89, 71, 98, 90, 63, 34, 81, 67, 55,
       17,  1, 23, 62, 28, 39, 85, 65, 52, 29, 22, 49,  9, 13, 18, 25, 59,
       83, 38, 74, 27, 30, 64, 84, 16, 77, 95, 61, 33,  5, 37, 44, 46, 48,
       54, 82, 76, 92, 57, 10, 60, 53, 47, 56, 41,  8, 26, 91,  0, 93, 11,
        2,  3, 94, 86, 32, 43, 88, 35, 96, 79, 51, 21, 15,  6,  4])

In [61]: A
Out[61]: 
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個あるうちからRandomに5個選ぶとき

In [62]: A[x][:5]
Out[62]: array([14, 36, 70, 75,  7])

リストから指定した確率でデータを選びたいとき

numpy.random.choice(a, size=None, replace=True, p=None)

In [98]: Data = ['A','B','C','D','E']

In [99]: np.random.choice(Data, 10, p=[0.1, 0.4, 0.2, 0.1, 0.2])
Out[99]: 
array(['D', 'B', 'B', 'D', 'B', 'B', 'E', 'B', 'E', 'D'], 
      dtype='|S1')

In [100]: x = np.random.choice(Data, 10000, p=[0.1, 0.4, 0.2, 0.1, 0.2])

choice.png

h = dict((c, (x==c).sum()/float(len(x))) for c in x)
fig, ax = plt.subplots()
ind = np.arange(len(Data))
ax.set_xticklabels(Data)
ax.set_xticks(ind + 0.5)
ax.bar(ind, [h[k] for k in Data])
15
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
16