https://docs.scipy.org/doc/numpy-1.17.0/reference/
函数名 说明
seed([seed]) 设定随机种子,这样每次生成的随机数会相同
rand(d0,d1,d2.....) 返回数据在[0,1)之间,具有均匀分布
randn(d0,d1,d2....) 返回标准正态分布(均值0,方差1)
randint(low[,high,size,dtype]) 返回随机整数,包含low,不包含high
choice(a[,size,replace,p]) a是一个数组,从它之间生成随机结果
random([size]) 随机生成[0.0,1.0)之间的小数
shuffle(x) 把数组x进行随机排列
permutation(x) 把数组x进行随机排列,或数字的全排列
normal([loc,scale,size]) 按照平均值loc和方差scale生成高斯分布的数字
uniform([loc,high,size]) 在[loc,high)之间生成均匀分布的数字
import numpy as np
np.random.seed(666)
np.random.rand(5)
array([0.0127032 , 0.4135877 , 0.04881279, 0.09992856, 0.50806631])
np.random.rand(3,4)
array([[0.20024754, 0.74415417, 0.192892 , 0.70084475],
[0.29322811, 0.77447945, 0.00510884, 0.11285765],
[0.11095367, 0.24766823, 0.0232363 , 0.72732115]])
np.random.randn(5)
array([-1.08879299, -0.57577075, -1.68290077, 0.22918525, -1.75662522])
np.random.randn(3,4)
array([[ 0.84463262, 0.27721986, 0.85290153, 0.1945996 ],
[ 1.31063772, 1.5438436 , -0.52904802, -0.6564723 ],
[-0.2015057 , -0.70061583, 0.68713795, -0.02607576]])
np.random.randn(2,3,4)
array([[[-0.82975832, 0.29655378, -0.3126795 , -0.61130127],
[-0.8217515 , 0.8971227 , 0.13607861, -0.2586548 ],
[ 1.11076564, -0.18842439, -0.04148929, -0.98479191]],
[[-1.35228176, 0.19432385, 0.26723935, -0.4264737 ],
[ 1.44773506, -0.1963061 , 1.51814514, 0.07722188],
[-0.06399132, 0.94592341, 1.20409101, -0.45124074]]])
np.random.randint(3) # [0,3)
0
np.random.randint(1,10) # [1,10)
3
np.random.randint(1,10,size=(5,))
array([3, 3, 7, 9, 6])
np.random.randint(1,10,size=5)
array([9, 6, 2, 5, 9])
np.random.randint(10,20,size=(2,3,4))
array([[[14, 13, 15, 14],
[10, 10, 14, 16],
[17, 18, 19, 12]],
[[18, 11, 10, 18],
[19, 12, 16, 19],
[15, 19, 17, 12]]])
np.random.random(5)
array([0.83023273, 0.44767601, 0.2132831 , 0.56115445, 0.71657783])
np.random.random(size=(2,3))
array([[0.7493205 , 0.58624783, 0.54759891],
[0.0817732 , 0.40852941, 0.63205157]])
np.random.random(size=(2,3,4))
array([[[0.12168885, 0.27480879, 0.07770505, 0.15726591],
[0.14978044, 0.38535367, 0.70941476, 0.44518764],
[0.01584702, 0.99491381, 0.90632665, 0.05199571]],
[[0.86100897, 0.51224649, 0.0111548 , 0.49310591],
[0.55102356, 0.27260476, 0.2311436 , 0.95858105],
[0.66579831, 0.84015904, 0.14691185, 0.14394403]]])
np.random.choice(5,3)
array([2, 1, 1])
np.random.choice(5,(2,3))
array([[4, 4, 1],
[4, 4, 3]])
np.random.choice([2,3,4,5,6,7,8,9],3)
array([3, 6, 7])
np.random.choice([2,3,4,5,6,7,8,9],(2,5))
array([[5, 5, 9, 9, 5],
[4, 7, 5, 8, 6]])
a=np.arange(10)
np.random.shuffle(a)
a
array([2, 7, 3, 0, 4, 6, 8, 1, 5, 9])
# 如果是多维数组,只会按照行进行打散,列不变
a=np.arange(20).reshape(4,5)
a
np.random.shuffle(a)
a
array([[ 5, 6, 7, 8, 9],
[15, 16, 17, 18, 19],
[ 0, 1, 2, 3, 4],
[10, 11, 12, 13, 14]])
np.random.permutation(10) # 和shuffle一样 a=np.arange(10) np.random.shuffle(a)
array([6, 5, 7, 1, 0, 8, 3, 2, 4, 9])
# 注意这里不会改变arr数组,而是copy了一个新的数组
arr=np.arange(20).reshape(4,5)
a=np.random.permutation(arr)
a
array([[ 5, 6, 7, 8, 9],
[15, 16, 17, 18, 19],
[10, 11, 12, 13, 14],
[ 0, 1, 2, 3, 4]])
arr
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
np.random.normal(1,10,10)
array([ 9.24188083, 5.79966003, 12.73468012, 10.09048069, -4.71721452,
-0.09497268, 1.19028265, -8.43761065, 7.40573153, -6.86443172])
np.random.normal(1,10,(2,5))
array([[ 7.08869993, -8.31011849, 10.78222248, -6.36918061,
-1.98732618],
[ -3.60587375, -9.88792986, -4.75770746, -15.82900771,
3.29185248]])
np.random.uniform(1,10,10)
array([6.25431357, 3.93121588, 9.00009382, 6.63764074, 8.36986322,
5.92610878, 4.75040805, 7.68742473, 4.32636743, 1.6764989 ])
np.random.uniform(1,10,(3,4))
array([[7.97673681, 2.9746832 , 1.71407914, 5.38102469],
[2.38306512, 8.4561862 , 2.72231711, 3.43368058],
[6.04930979, 9.12142351, 8.66609508, 4.76273765]])
import matplotlib.pyplot as plt
# 绘制sin函数曲线
x=np.linspace(-10,10,100)
y=np.sin(x)
plt.plot(x,y)
plt.show()
x
array([-10. , -9.7979798 , -9.5959596 , -9.39393939,
-9.19191919, -8.98989899, -8.78787879, -8.58585859,
-8.38383838, -8.18181818, -7.97979798, -7.77777778,
-7.57575758, -7.37373737, -7.17171717, -6.96969697,
-6.76767677, -6.56565657, -6.36363636, -6.16161616,
-5.95959596, -5.75757576, -5.55555556, -5.35353535,
-5.15151515, -4.94949495, -4.74747475, -4.54545455,
-4.34343434, -4.14141414, -3.93939394, -3.73737374,
-3.53535354, -3.33333333, -3.13131313, -2.92929293,
-2.72727273, -2.52525253, -2.32323232, -2.12121212,
-1.91919192, -1.71717172, -1.51515152, -1.31313131,
-1.11111111, -0.90909091, -0.70707071, -0.50505051,
-0.3030303 , -0.1010101 , 0.1010101 , 0.3030303 ,
0.50505051, 0.70707071, 0.90909091, 1.11111111,
1.31313131, 1.51515152, 1.71717172, 1.91919192,
2.12121212, 2.32323232, 2.52525253, 2.72727273,
2.92929293, 3.13131313, 3.33333333, 3.53535354,
3.73737374, 3.93939394, 4.14141414, 4.34343434,
4.54545455, 4.74747475, 4.94949495, 5.15151515,
5.35353535, 5.55555556, 5.75757576, 5.95959596,
6.16161616, 6.36363636, 6.56565657, 6.76767677,
6.96969697, 7.17171717, 7.37373737, 7.57575758,
7.77777778, 7.97979798, 8.18181818, 8.38383838,
8.58585859, 8.78787879, 8.98989899, 9.19191919,
9.39393939, 9.5959596 , 9.7979798 , 10. ])
y
array([ 0.54402111, 0.36459873, 0.17034683, -0.03083368, -0.23076008,
-0.42130064, -0.59470541, -0.74392141, -0.86287948, -0.94674118,
-0.99209556, -0.99709789, -0.96154471, -0.8868821 , -0.77614685,
-0.63384295, -0.46575841, -0.27872982, -0.0803643 , 0.12126992,
0.31797166, 0.50174037, 0.66510151, 0.80141062, 0.90512352,
0.97202182, 0.99938456, 0.98609877, 0.93270486, 0.84137452,
0.7158225 , 0.56115544, 0.38366419, 0.19056796, -0.01027934,
-0.21070855, -0.40256749, -0.57805259, -0.73002623, -0.85230712,
-0.93992165, -0.98930624, -0.99845223, -0.96698762, -0.8961922 ,
-0.78894546, -0.64960951, -0.48385164, -0.2984138 , -0.10083842,
0.10083842, 0.2984138 , 0.48385164, 0.64960951, 0.78894546,
0.8961922 , 0.96698762, 0.99845223, 0.98930624, 0.93992165,
0.85230712, 0.73002623, 0.57805259, 0.40256749, 0.21070855,
0.01027934, -0.19056796, -0.38366419, -0.56115544, -0.7158225 ,
-0.84137452, -0.93270486, -0.98609877, -0.99938456, -0.97202182,
-0.90512352, -0.80141062, -0.66510151, -0.50174037, -0.31797166,
-0.12126992, 0.0803643 , 0.27872982, 0.46575841, 0.63384295,
0.77614685, 0.8868821 , 0.96154471, 0.99709789, 0.99209556,
0.94674118, 0.86287948, 0.74392141, 0.59470541, 0.42130064,
0.23076008, 0.03083368, -0.17034683, -0.36459873, -0.54402111])
# 加入噪声
x=np.linspace(-10,10,100)
y=np.sin(x)+np.random.rand(len(x))
plt.plot(x,y)
plt.show()