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

快速(或更多)numpy花式索引编制和减少?

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

快速(或更多)numpy花式索引编制和减少?

numpy.take
由于某种原因,它比花式索引要快得多。唯一的技巧是将数组视为平面。

In [1]: a = np.random.randn(12,6).astype(np.float32)In [2]: c = np.random.randint(0,6,size=(1e5,12)).astype(np.uint8)In [3]: r = np.arange(12)In [4]: %timeit a[r,c].sum(-1)10 loops, best of 3: 46.7 ms per loopIn [5]: rr, cc = np.broadcast_arrays(r,c)In [6]: flat_index = rr*a.shape[1] + ccIn [7]: %timeit a.take(flat_index).sum(-1)100 loops, best of 3: 5.5 ms per loopIn [8]: (a.take(flat_index).sum(-1) == a[r,c].sum(-1)).all()Out[8]: True

我认为,除此以外,您还将看到速度提高很多的唯一其他方法是使用PyCUDA之类的东西为GPU编写自定义内核。



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

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

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