np.random.uniform,random,choice,newaxis

23 篇文章 0 订阅
19 篇文章 0 订阅

np.random.uniform(low=0.0, high=1.0, size=None)

均匀分布,随机生成下一个实数,在 [low, high) 范围内。

low  :   随机数的最小值,包含该值。

high  :  随机数的最大值,不包含该值。

https://blog.csdn.net/u013920434/article/details/52507173  讲的很全面

https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.uniform.html  官方讲解

a = np.random.uniform(1, 2, 10)
>>> a
array([1.78110153, 1.08004137, 1.52783342, 1.4495069 , 1.14119001,
       1.53451488, 1.35288078, 1.2797133 , 1.75361729, 1.1551278 ])
>>> a.shape
(10,)
>>> b = np.random.uniform(1, 2, 10)[:, np.newaxis]  #加了一个维度
>>> b
array([[1.19651492],
       [1.39541099],
       [1.03515532],
       [1.85664151],
       [1.84396349],
       [1.55697423],
       [1.14567357],
       [1.11043705],
       [1.80660254],
       [1.50724142]])
>>> b.shape
(10, 1)

 

numpy.random.randomsize = None 

(0,  1)中返回随机浮点数。

>>> print(np.random.random())
0.4362833489716911
>>> print(np.random.random(5,))
[0.23883434 0.56420743 0.97575548 0.44025629 0.73052107]

>>> print( np.random.random((2,4)))  # 定义具体维度时random后需2层括号
[[0.56629049 0.25066895 0.22551347 0.69089366]
 [0.86297426 0.45869556 0.07899109 0.94200317]]

https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.random.html

 

np.random.choice(asize=Nonereplace=Truep=None)

--------------------- 
作者:qfpkzheng 
来源:CSDN 
原文:https://blog.csdn.net/qfpkzheng/article/details/79061601 
版权声明:本文为博主原创文章,转载请附上博文链接!

# 参数意思分别 是从a 中以概率P,随机选择3个, p没有指定的时候相当于是一致的分布
a1 = np.random.choice(a=5, size=3, replace=False, p=None)
print(a1)
# 非一致的分布,会以多少的概率提出来
a2 = np.random.choice(a=5, size=3, replace=False, p=[0.2, 0.1, 0.3, 0.4, 0.0])
print(a2)
# replacement 代表的意思是抽样之后还放不放回去,如果是False的话,那么出来的三个数都不一样,如果是
True的话, 有可能会出现重复的,因为前面的抽的放回去了。

 

--------------------- 
作者:超级杰哥 
来源:CSDN 
原文:https://blog.csdn.net/autoliuweijie/article/details/51982514 
版权声明:本文为博主原创文章,转载请附上博文链接!

a : 1-D array-like or int
    If an ndarray, a random sample is generated from its elements. 
    If an int, the random sample is generated as if a was np.arange(n)

size : int or tuple of ints, optional 

replace : boolean, optional
    Whether the sample is with or without replacement

p : 1-D array-like, optional
    The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.

>>> np.random.choice(5, 3)
array([0, 3, 4])

>>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
array([3, 3, 0])

>>> np.random.choice(5, 3, replace=False)
array([3,1,0])

>>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
array([2, 3, 0])

>>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']

>>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'],

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值