numpy 和 pandas 中常用的一些函数及其参数

numpy中有一些常用的用来产生随机数的函数,randn()和rand()就属于这其中。 
numpy.random.randn(d0, d1, …, dn)是从标准正态分布中返回一个或多个样本值。 
numpy.random.rand(d0, d1, …, dn)的随机样本位于[0, 1)中。 

 

代码:

import numpy as np 

arr1 = np.random.randn(2,4)
print(arr1)
print('******************************************************************')
arr2 = np.random.rand(2,4)
print(arr2) 

结果:

[[-1.03021018  0.5197033   0.52117459 -0.70102661]
 [ 0.98268569  1.21940697 -1.095241   -0.38161758]]
******************************************************************
[[ 0.19947349  0.05282713  0.56704222  0.45479972]
 [ 0.28827103  0.1643551   0.30486786  0.56386943]]

Notes

For random samples from N(\mu, \sigma^2), use:

sigma * np.random.randn(...) + mu

Examples

>>> np.random.randn()
2.1923875335537315 #random

Two-by-four array of samples from N(3, 6.25):

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

 

posted @ 2018-01-29 20:41  KévinX  阅读(704)  评论(0编辑  收藏  举报