python – 应用于数组列表时Numpy max slow
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 应用于数组列表时Numpy max slow,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2620字,纯文字阅读大概需要4分钟。
内容图文
![python – 应用于数组列表时Numpy max slow](/upload/InfoBanner/zyjiaocheng/785/75096c7eb15a480684d3e384dcd6367c.jpg)
我执行一些计算以获得numpy数组的列表.随后,我想找到沿第一轴的最大值.我目前的实施(见下文)非常缓慢,我想找到替代方案.
原版的
pending = [<list of items>]
matrix = [compute(item) for item in pending if <some condition on item>]
dominant = np.max(matrix, axis = 0)
修订版1:这个实现更快(~10x;可能是因为numpy不需要弄清楚数组的形状)
pending = [<list of items>]
matrix = [compute(item) for item in pending if <some condition on item>]
matrix = np.vstack(matrix)
dominant = np.max(matrix, axis = 0)
我运行了几个测试,减速似乎是由于数组列表内部转换为numpy数组
Timer unit: 1e-06 s
Total time: 1.21389 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
4 def direct_max(list_of_arrays):
5 1000 1213886 1213.9 100.0 np.max(list_of_arrays, axis = 0)
Total time: 1.20766 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
8 def numpy_max(list_of_arrays):
9 1000 1151281 1151.3 95.3 list_of_arrays = np.array(list_of_arrays)
10 1000 56384 56.4 4.7 np.max(list_of_arrays, axis = 0)
Total time: 0.15437 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
12 @profile
13 def stack_max(list_of_arrays):
14 1000 102205 102.2 66.2 list_of_arrays = np.vstack(list_of_arrays)
15 1000 52165 52.2 33.8 np.max(list_of_arrays, axis = 0)
有没有办法加速最大功能或者是否可以使用我的计算结果有效地填充numpy数组,以便max最快?
解决方法:
你可以使用reduce(np.maximum,matrix),这里是一个测试:
import numpy as np
np.random.seed(0)
N, M = 1000, 1000
matrix = [np.random.rand(N) for _ in xrange(M)]
%timeit np.max(matrix, axis = 0)
%timeit np.max(np.vstack(matrix), axis = 0)
%timeit reduce(np.maximum, matrix)
结果是:
10 loops, best of 3: 116 ms per loop
10 loops, best of 3: 10.6 ms per loop
100 loops, best of 3: 3.66 ms per loop
编辑
`argmax()’更难,但你可以使用for循环:
def argmax_list(matrix):
m = matrix[0].copy()
idx = np.zeros(len(m), dtype=np.int)
for i, a in enumerate(matrix[1:], 1):
mask = m < a
m[mask] = a[mask]
idx[mask] = i
return idx
它仍然比argmax()更快:
%timeit np.argmax(matrix, axis=0)
%timeit np.argmax(np.vstack(matrix), axis=0)
%timeit argmax_list(matrix)
结果:
10 loops, best of 3: 131 ms per loop
10 loops, best of 3: 21 ms per loop
100 loops, best of 3: 13.1 ms per loop
内容总结
以上是互联网集市为您收集整理的python – 应用于数组列表时Numpy max slow全部内容,希望文章能够帮你解决python – 应用于数组列表时Numpy max slow所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。