Numpy随机数1

更多内容查看这里

Simple random data | 常用随机函数

1
import numpy as np
1
2
3
# rand(d0, d1, …, dn)
# random values in a given shape
np.random.rand(1)
array([0.15771123])
1
np.random.rand(3, 4)
array([[0.17303082, 0.13546342, 0.96790769, 0.54289967],
       [0.47752797, 0.3163813 , 0.33804729, 0.63760302],

更多内容查看这里

Simple random data | 常用随机函数

1
import numpy as np

rand(d0, d1, …, dn)

1
2
3
# rand(d0, d1, …, dn)
# random values in a given shape
np.random.rand(1)
array([0.97299322])
1
np.random.rand(3, 4)
array([[0.81969546, 0.63076449, 0.70050141, 0.28701361],
       [0.5909755 , 0.10265285, 0.03841936, 0.04747027],
       [0.50586629, 0.33692611, 0.18006999, 0.37508064]])
1
np.random.rand(5)
array([0.4027442 , 0.29275407, 0.15175853, 0.76296697, 0.84970737])

正态分布随机数 randn(d0, d1, …, dn)

1
2
3
# randn(d0, d1, …, dn)
# return a sample or samples from standard norma sidtribution
np.random.randn(1)
array([0.68010349])
1
np.random.randn(4)
array([-0.22561174, -0.12611506,  1.42778485,  0.69267162])
1
np.random.randn(2, 2)
array([[ 1.09486998, -0.12217683],
       [-0.26184258,  0.70775913]])

Note: We can use matplotlib to show the distrubution

随机整数

randint(low[, high, size, dtype]) / np.random.random_integers(2)

1
2
3
# randint(low[, high, size, dtype])
# return random integers from low(inclusive) to high(exclusive)
np.random.randint(1)
0
1
np.random.randint(3)
2
1
np.random.randint(100, 1000)
945
1
np.random.randint(0, 100, 5)
array([76, 86, 29, 40, 76])
1
np.random.randint(0, 100, (3, 3))
array([[88, 50, 90],
       [64, 69, 57],
       [ 2, 10, 95]])
1
2
3
4
5
# random_integers(low[, high, size])
# random integers of type np.int between low and high, inclusive
np.random.random_integers(2)

# This function is deprecated. Please call randint(l, r + 1) instead
D:\Anaconda3\Anaconda3_py36\lib\site-packages\ipykernel_launcher.py:3: DeprecationWarning: This function is deprecated. Please call randint(1, 2 + 1) instead
  This is separate from the ipykernel package so we can avoid doing imports until





1

随机小数

  • random_sample([size])
  • random([size])
  • ranf([size])
  • sample([size])
1
2
3
# random_sample([size])
# return random floats in the half-open interval[0.0, 1.0)
np.random.random_sample(1)
array([0.42922028])
1
np.random.random_sample((3,3))
array([[0.13911191, 0.29153576, 0.38330187],
       [0.39095818, 0.37114282, 0.69224038],
       [0.3664121 , 0.02533538, 0.76235701]])
1
2
3
# random([size])
# return random floats in the half-open interval[0.0, 1.0)
np.random.random(1)
array([0.57758511])
1
np.random.random((2, 2))
array([[0.89365816, 0.10505705],
       [0.76704421, 0.27889437]])
1
2
3
# ranf([size])
# return random floats int the half-open interval [0.0, 1.0)
np.random.ranf(1)
array([0.64518204])
1
np.random.ranf((3,4))
array([[0.10753966, 0.70902024, 0.63834775, 0.65623879],
       [0.71709776, 0.34437971, 0.42639926, 0.70715686],
       [0.12977266, 0.20338805, 0.94316098, 0.3082274 ]])
1
2
3
# sample([size])
# return random floats int the half-open interval [0.0, 1.0)
np.random.sample(2)
array([0.95896772, 0.64870791])
1
np.random.sample((2, 2))
array([[0.91634732, 0.95899658],
       [0.59189998, 0.53045256]])

自定义随机

  • choice(a[, size, replace, p])
1
2
3
# choice(a[, size, replace, p])
# generate a random sample from a given 1-D array
np.random.choice([3, 2, 1, 0],3)
array([3, 1, 3])
1
np.random.choice([3, 2, 1, 0],3, replace=False)
array([1, 2, 3])
1
np.random.choice(["cat", "dog", "fish", "mouse", "bird"],(2, 2), replace=True)
array([['cat', 'dog'],
       ['bird', 'dog']], dtype='<U5')
1
2
3
4
# p represents properties of the elements of the 1-D array
# sum of p must be 1.0
np.random.choice(["cat", "dog", "fish", "mouse", "bird"],(2, 2), replace=True,
p=[0.1, 0.5, 0.3, 0.05, 0.05])
array([['fish', 'dog'],
       ['dog', 'fish']], dtype='<U5')
1
2
3
# bytes(length)
# return random bytes
np.random.bytes(1)
b'\x8c'

Permutations | 排列

  • shuffle(x)
  • permutation(x)
1
2
3
4
5
6
7
8
9
# shuffle(x)
# modify a sequence in-place by shuffling its contents
# 将序列在原地打乱
x = np.arange(11)
print('----befor shuffle x----')
print(x)
print('----after shuffle x----')
np.random.shuffle(x)
print(x)
----befor shuffle x----
[ 0  1  2  3  4  5  6  7  8  9 10]
----after shuffle x----
[ 8  1  5  4  0  2  3  9  7  6 10]
1
2
3
4
5
6
7
# permutation(x)
# randomly permute a sequence, or return a permuted range
x = np.arange(11)
print("---permutation----")
print(np.random.permutation(x))
print("---x----")
print(x)
---permutation----
[ 9  4  6  0  5 10  8  3  7  2  1]
---x----
[ 0  1  2  3  4  5  6  7  8  9 10]

Random generator | 随机数生成器

如果设置了seed,每次重新运行时生成的随机数都是确定和上次相同的。

1
2
3
4
# RandomState([seed])
# container for the Mersenne Twister pseudo-random number generator
np.random.RandomState(1)
np.random.random(5)
array([0.58048047, 0.57948883, 0.82388234, 0.77501925, 0.70183176])
1
2
3
4
# seed([seed])
# seed the generator
np.random.seed(0)
np.random.random(5)
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])
1
2
3
4
5
6
# get_state()
# return a tuple representing the internal state of the generator
# np.random.get_state()

# set_state(state)
# set the interna state of the generator from a tuple