一、random
-
seed(int n) 随机数种子(其实生成随机数是根据随机数种子使用一定的算法生成随机数的,因此也可以说是伪随机数)
-
rand(int n) 产生n个均匀分布的样本值
import numpy as np
from numpy import random
print(random.rand(10))
"""
输出:
[0.00279392 0.30422703 0.40830302 0.76097027 0.88393813 0.95769368
0.54477196 0.91185166 0.91407798 0.54808962]
"""
- randint(begin,end,num) begin和end之间随机生成num个数
import numpy as np
from numpy import random
print(random.randint(10,20,5))·
"""
输出:
[15 10 10 10 16]
"""
- randn(N,M) 生成N*M的符合正态分布(均值为0,标准差为1)的数组
import numpy as np
from numpy import random
print(random.randn(10,5))
"""
[[-0.14930229 -0.49723555 0.05889083 -1.18325212 0.09198058]
[-0.12299026 -0.50251851 -1.11138151 -0.14130106 1.96066248]
[-0.86989821 1.55537731 0.14948361 0.65432214 -1.18670275]
[ 0.22100948 -0.61138931 0.61774483 0.19456671 -2.07787105]
[-0.20535281 0.08024417 0.13413211 -1.56538371 0.88922951]
[-0.79801568 -0.41154799 -0.91625006 1.44174114 1.87717894]
[ 1.31754142 0.19543874 -1.17530768 0.36376645 -0.07837382]
[ 0.89909056 0.48339419 1.32612804 -0.22504098 -0.31849442]
[-0.57056038 0.13980028 1.116261 1.27374768 -0.25722175]
[-1.13907277 -0.3458683 0.10948824 -0.45425931 -0.30000255]]
"""
- normal(N,M) 生成N*M的符合普通正态分布的数组
import numpy as np
from numpy import random
print(random.normal(size=(10,5)))
"""
[[ 1.07713963 0.90726022 -0.42696564 -1.47677281 -0.07285344]
[-0.85633398 1.88888854 0.48570094 0.23756726 0.48340864]
[-0.31309425 -0.65975846 0.31369926 -0.52956323 1.43464665]
[-1.45134155 0.29923932 -0.69863863 -0.27948516 -1.21324693]
[-0.11547236 1.12712333 -0.67411053 -0.36735838 0.27807234]
[ 1.59508896 -1.59286109 0.87856752 1.67809503 0.27672422]
[ 0.61031653 -0.40900192 0.1169569 -0.07748274 -2.32155114]
[-0.21815467 0.0928035 -0.94198498 2.60294661 -0.75585361]
[-0.39176799 0.42874459 -0.92383274 0.44186166 0.19437499]
[-0.77975909 0.11364601 -0.23020196 0.28517307 1.09343548]]
"""
- uniform(min,max) min到max中随机生成一个数
import numpy as np
from numpy import random
print(random.uniform(10,100))
- Beta(ndarray1,ndarray2) 生成beta分布样本值,参数大于0
- gamma(ndarray1,ndarray2) 生成gamma分布样本值
二、linspace
numpy.linspace(start,end,num,endpoint=True,retstep=False,dtype=None)
start :开始位置
end :结束位置
num :数据个数
endpoint :是否包含结束位置(默认是True,包含)
retstep :是否显示间距(如果显示的还,第一个元素data[0]显示数组,第二个元素data[1]显示间距)
dtype :数据类型
import numpy as np
lin_data = np.linspace(10,100,15)
print(lin_data)
"""
[ 10. 16.42857143 22.85714286 29.28571429 35.71428571
42.14285714 48.57142857 55. 61.42857143 67.85714286
74.28571429 80.71428571 87.14285714 93.57142857 100. ]
"""
import numpy as np
lin_data = np.linspace(10,100,15,retstep=True)
print(lin_data)
"""
(array([ 10. , 16.42857143, 22.85714286, 29.28571429,
35.71428571, 42.14285714, 48.57142857, 55. ,
61.42857143, 67.85714286, 74.28571429, 80.71428571,
87.14285714, 93.57142857, 100. ]), 6.428571428571429)
"""
lin_data[0]
"""
array([ 10. , 16.42857143, 22.85714286, 29.28571429,
35.71428571, 42.14285714, 48.57142857, 55. ,
61.42857143, 67.85714286, 74.28571429, 80.71428571,
87.14285714, 93.57142857, 100. ])
"""
lin_data[1]
"""
6.428571428571429
"""