NumPy创建随机数数列和特殊数列

2018-11-30  本文已影响5人  juriau

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

如果想要随机样品来自于:N(\mu, \sigma^2),可以使用
sigma * np.random.randn(...) + mu

例子:

>>> np.random.randn()
2.1923875335537315 

>>> 2.5 * np.random.randn(2, 4) + 3
array([[-4.49401501,  4.00950034, -1.81814867,  7.29718677],  
       [ 0.39924804,  4.68456316,  4.99394529,  4.84057254]]) 

更多请参考:https://docs.scipy.org/doc/numpy/reference/routines.random.html


numpy.zeros(shape, dtype=float, order='C')
numpy.ones(shape, dtype=None, order='C')

返回一个给定形状和类型的数组,以0/1填充。

>>> np.zeros(5)
array([ 0.,  0.,  0.,  0.,  0.])

>>> np.zeros((2, 1))
array([[ 0.],
       [ 0.]])

更多请参考:
https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.array-creation.html

上一篇下一篇

猜你喜欢

热点阅读