[Python] Random processing (create, select, sort)

Random

When you want a random Index

numpy.random.randint(low, high=None, size=None) Low is included, but high is not included. The distribution is uniform

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

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

When you want to shuffle a numpy array

numpy.random.shuffle(x)

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])

** Behavior when shuffling a multidimensional numpy array ** If you shuffle a multidimensional numpy array, it will be shuffled along the first index (first dimension).

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]]

When you want a shuffled Index

numpy.random.permutation(x) If you don't want the data to be shuffled, you can shuffle the Index and pass it.

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])

When randomly selecting 5 out of 100

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

When you want to select data with the specified probability from the list

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])

Recommended Posts

[Python] Random processing (create, select, sort)
[Python] Sort
Python # sort
Create a random string in Python
python image processing
Python file processing
Python distributed processing Spartan
File processing in Python
Python: Natural language processing
Communication processing by Python
Multithreaded processing in python
Create a Python module
Bubble sort in Python
Create your own Random Dot Stereogram (RDS) in Python.
[Python] Create multiple directories
Create SpatiaLite in Python
Python self-made class sort
First Python image processing
Random walk in Python
Text processing in Python
[Memo] Python3 list sort
Queue processing in Python
[Linux] DICOM sort processing
Random string generation (Python)
Python sort cheat sheet
Image processing with Python
Custom sort in Python3
[Python] Sort collection types
Create a Python environment
Python string processing illustration
Various processing of Python
Image processing with Python (Part 2)
100 Language Processing with Python Knock 2015
Balanced Random Forest in python
UTF8 text processing in python
Sort Python module imports alphabetically
Create a Wox plugin (Python)
python3 Measure the processing speed.
Create a function in Python
"Apple processing" with OpenCV3 + Python3
Create a dictionary in Python
Naturally sort Path in Python
Create 3d gif with python3
Python basic dict sort order
Create gif video in Python
Create ToDo List [Python Django]
Acoustic signal processing with Python (2)
Acoustic signal processing with Python
Sort huge files with python
Asynchronous processing (threading) in python
100 Language Processing Knock Chapter 1 (Python)
100 Language Processing Knock Chapter 2 (Python)
Use Random Forest in Python
python in mongodb in descending sort
Image processing with Python (Part 1)
Create JIRA tickets using Python
Create a python numpy array
Image processing with Python (Part 3)
Python beginners organize bubble sort
Image processing by python (Pillow)
Post processing of python (NG)