numpy.random 的函数基本用法

import numpy as np
”’

1)rand(d0, d1, …, dn)

Create an array of the given shape and populate(居住于) it with
random samples from a uniform distribution
over [0, 1). 0~1的均匀分布

        Parameters
        ----------
        d0, d1, ..., dn : int, optional
            The dimensions of the returned array, should all be positive.
            If no argument is given a single Python float is returned.

        Returns
        -------
        out : ndarray, shape ``(d0, d1, ..., dn)``
        Random values.

”’
rand=np.random.rand(3,2)
print(“rand:”,rand)
”’

2)np.random.randn(d0, d1, …, dn) 0~1 标准正态分布 standard normal

”’
randn=np.random.randn(3,2)
print(“randn:”,randn)

”’

3)randint(low[,high,shape]) []中为可选参数

”’
randint=np.random.randint(100,200,(2,3))
”’

4)

shuffle [ˈʃʌfl]乱序 shuffle(x) 改变数组x本身
permutation 置换顺序 permutation(x) 不改变数组x

根据数组最外维进行随机排列,内部的顺序不变
”’
x=np.arange(20,40,step=1,dtype=int).reshape(4,5)
np.random.shuffle(x)
print(“shuffle_x:”,x)
”’

5)

choice(x[,size,replace,p])
从一维数组x中抽取元素,每个元素的抽取概率为p(p为与x同shape的概率数组)中的对应概率,形成size形状的新数组
replace 默认为false 不可重用元素
”’
x.resize((20,))
print(“x_resize:”,x)
print(“choice:”,np.random.choice(x,size=(3,4),p=x/np.sum(x)))

6)均匀分布

print(“np.random.uniform(20,40,(2,4)):”,np.random.uniform(20,40,(2,4)))

7)正态分布(均值,方差,shape)

print(“np.random.normal(0,1,(3,4)):”,np.random.normal(0,1,(3,4)))

8)泊松分布

print(“np.random.poisson(0.3,(2,4)):”,np.random.poisson(0.3,(2,4)))

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值