栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

对沿轴的给定2D概率数组矢量化``numpy.random.choice''

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

对沿轴的给定2D概率数组矢量化``numpy.random.choice''

这是一种获取每一行随机索引的矢量化方法,其概率

a
2D
数组-

(a.cumsum(1) > np.random.rand(a.shape[0])[:,None]).argmax(1)

概括覆盖

2D
数组的行和列-

def random_choice_prob_index(a, axis=1):    r = np.expand_dims(np.random.rand(a.shape[1-axis]), axis=axis)    return (a.cumsum(axis=axis) > r).argmax(axis=axis)

让我们通过运行一百万次来验证给定的样本-

In [589]: a = np.array([     ...:     [.1, .3, .6],     ...:     [.2, .4, .4],     ...: ])In [590]: choices = [random_choice_prob_index(a)[0] for i in range(1000000)]# This should be close to first row of given sampleIn [591]: np.bincount(choices)/float(len(choices))Out[591]: array([ 0.099781,  0.299436,  0.600783])

运行时测试

原始的循环方式-

def loopy_app(categorical_distributions):    m, n = categorical_distributions.shape    out = np.empty(m, dtype=int)    for i,row in enumerate(categorical_distributions):        out[i] = np.random.choice(n, p=row)    return out

更大数组上的时间-

In [593]: a = np.array([     ...:     [.1, .3, .6],     ...:     [.2, .4, .4],     ...: ])In [594]: a_big = np.repeat(a,100000,axis=0)In [595]: %timeit loopy_app(a_big)1 loop, best of 3: 2.54 s per loopIn [596]: %timeit random_choice_prob_index(a_big)100 loops, best of 3: 6.44 ms per loop


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/634005.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号