12

【Python】【numpy】random随机数生成

 3 years ago
source link: https://www.guofei.site/2017/07/05/numpyrandom.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【Python】【numpy】random随机数生成

2017年07月05日

Author: Guofei

文章归类: A蒙特卡洛方法 ,文章编号: 10022


版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2017/07/05/numpyrandom.html

Edit

最佳实践

np.random.choice 只能针对规整的数组或矩阵进行choice
random.choice 只能每次抽样一个
a=random.shuffle;a[:3]会改变原序列
下面做了个最佳方案

  • 有放回抽样
    all_papers=[[1,[2]],[3,[4]]]
    random.choices(all_papers,k=2)
    # random.choice(all_papers) # 单个抽样
  • 无放回抽样
    random.sample(all_papers,k=2)
    

随机种子

np.random.seed(seed=None)

shuffle&permutation重新洗牌

np.random.shuffle(arr) # 直接改arr,返回none
np.random.permutation(arr) # 不改arr,返回重新洗牌后的

多维数组:只shuffle第0维

choice随机选数

numpy.random.choice(a, size=None, replace=True, p=None)

生成随机数


numpy.random.randint(low, high=None, size=None, dtype='l')
左闭右开的整数

numpy.random.random_integers(low, high=None, size=None)
开区间的整数

numpy.random.uniform(low=0.0, high=1.0, size=None)
均匀分布

rand#均匀分布

binomial#二项分布

numpy.random.normal(loc=0.0, scale=1.0, size=None)
#正态分布

np.random.beta()

numpy.random.f(dfnum, dfden, size=None)


chisquare
#卡方分布

gamma
#Gamma分布

random

import random

random.random() # 0-1 之间的自然数
random.randint(1, 2) # 整数,含头含尾
random.randrange(start=1, stop=10, step=2) # 含头不含尾

参考文献

numpy官方文档


您的支持将鼓励我继续创作!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK