- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我发现我的程序中的一个瓶颈是从给定值列表创建 numpy 数组,最常见的是将四个值放入一个 2x2 数组中。有一种显而易见、易于阅读的方法:
my_array = numpy.array([[1, 3], [2.4, -1]])
这需要 15 秒——非常非常慢,因为我已经做了数百万次。
还有一种更快、更难读的方法:
my_array = numpy.empty((2,2))
my_array[0,0] = 1
my_array[0,1] = 3
my_array[1,0] = 2.4
my_array[1,1] = -1
速度提高了 10 倍,仅需 1 微秒。
有没有既快速又易于阅读的方法?
到目前为止我尝试了什么:使用 asarray
而不是 array
没有区别;将 dtype=float
传递给 array
也没有区别。最后,我明白我可以自己做:
def make_array_from_list(the_list, num_rows, num_cols):
the_array = np.empty((num_rows, num_cols))
for i in range(num_rows):
for j in range(num_cols):
the_array[i,j] = the_list[i][j]
return the_array
这将在 4us 内创建数组,这是中等速度的中等可读性(与上述两种方法相比)。但说真的,我无法相信使用内置方法没有更好的方法。
提前致谢!
最佳答案
这是一个很好的问题。我找不到任何可以接近您完全展开的解决方案速度的东西(编辑 @BiRico 能够想出一些接近的东西。查看评论和更新 :)。以下是我(和其他人)提出的一系列不同选项以及相关的时间安排:
import numpy as np
def f1():
"np.array + nested lists"
my_array = np.array([[1, 3], [2.4, -1]])
def f2():
"np.array + nested tuples"
my_array = np.array(((1, 3), (2.4, -1)))
def f3():
"Completely unrolled"
my_array = np.empty((2,2),dtype=float)
my_array[0,0] = 1
my_array[0,1] = 3
my_array[1,0] = 2.4
my_array[1,1] = -1
def f4():
"empty + ravel + list"
my_array = np.empty((2,2),dtype=float)
my_array.ravel()[:] = [1,3,2.4,-1]
def f5():
"empty + ravel + tuple"
my_array = np.empty((2,2),dtype=float)
my_array.ravel()[:] = (1,3,2.4,-1)
def f6():
"empty + slice assignment"
my_array = np.empty((2,2),dtype=float)
my_array[0,:] = (1,3)
my_array[1,:] = (2.4,-1)
def f7():
"empty + index assignment"
my_array = np.empty((2,2),dtype=float)
my_array[0] = (1,3)
my_array[1] = (2.4,-1)
def f8():
"np.array + flat list + reshape"
my_array = np.array([1, 3, 2.4, -1]).reshape((2,2))
def f9():
"np.empty + ndarray.flat (Pierre GM)"
my_array = np.empty((2,2), dtype=float)
my_array.flat = (1,3,2.4,-1)
def f10():
"np.fromiter (Bi Roco)"
my_array = np.fromiter((1,3,2.4,-1), dtype=float).reshape((2,2))
import timeit
results = {}
for i in range(1,11):
func_name = 'f%d'%i
my_import = 'from __main__ import %s'%func_name
func_doc = globals()[func_name].__doc__
results[func_name] = (timeit.timeit(func_name+'()',
my_import,
number=100000),
'\t'.join((func_name,func_doc)))
for result in sorted(results.values()):
print '\t'.join(map(str,result))
重要的时间点:
在 Ubuntu Linux 上,Core i7:
0.158674955368 f3 Completely unrolled
0.225094795227 f10 np.fromiter (Bi Roco)
0.737828969955 f8 np.array + flat list + reshape
0.782918930054 f5 empty + ravel + tuple
0.786983013153 f9 np.empty + ndarray.flat (Pierre GM)
0.814703941345 f4 empty + ravel + list
1.2375421524 f7 empty + index assignment
1.32230591774 f2 np.array + nested tuples
1.3752617836 f6 empty + slice assignment
1.39459013939 f1 np.array + nested lists
关于python - 从单个值 : Fast and readable method? 构建一个小的 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13131220/
我尝试使用可读性模块,但出现以下错误。我怎样才能让用户获得“可读性”? /usr/local/opt/python/bin/python3.6:没有名为 readability.readability
Node 文档说(可读流的read事件): Once the internal buffer is drained, a readable event will fire again when mor
我必须在 APL 中编码。由于代码要维护很长时间,我想知道是否有一些论文/书籍包含启发式/技巧/示例来帮助设计干净可读的 APL 程序。 这与使用其他编程语言进行编码是一种不同的体验。例如,制作一个函
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
boolean f(boolean A, boolean B, boolean C, boolean D, boolean E) { if (A) { k(); if (B)
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 6年前关闭。 Improve t
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 10年前关闭。 Improve this
如何编写易于被其他人阅读并且没有参与编写任何部分的代码? 最佳答案 确保其他人可以阅读您的代码的最佳方法是确保它清晰简洁。即, 对变量、函数和类使用自记录名称。 对复杂的算法进行注释,这样读者就不必花
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
我有自定义 UITableView,我将背景颜色设置为白色。这适用于黑暗和明亮模式。但在暗模式下,披露指标很难看到。 我尝试使用 cell.tint 颜色修复此问题,但它不起作用。我该如何解决? 深色
我有一堆看起来像这样的字符: Комуникацион&
我正在通过learn you a haskell教程和 我一直在绊倒作者的一些例子 已经给。 例如,他重新实现了 zip 如下: zip' :: [a] -> [b] -> [(a,b)] zip'
假设您正在制作一个博客软件,并且想要显示一个条目获得的评论数量。你可以这样做: [Entry title] [Content........] [ Comments] 这可能会导致: [Entry
简单的问题(如果这太基于意见,我们深表歉意):我应该如何存储百分比? 在编写包含大量分析计算或涉及百分比(返回率、年利率、利润率等)的财务计算的程序时,以下三个选项中哪一个是最佳实践,就性能、可读性、
$ find -readable find: ‘./folder1’: Permission denied 假设 find 检查目录的“r”权限,就像检查文件一样, 为什么find会打印不可读的文件夹
我遇到了一个奇怪的情况,我必须把我的名字和联系信息放在网页上,我认为这很好,只要文本不能被机器人读取、复制等。 基本上我想要的是一个看起来像普通文本但表现得像图像的文本 block 。但是,我不能使用
这个问题在这里已经有了答案: How to use range-based for() loop with std::map? (5 个答案) 关闭 9 年前。 新的基于范围的 for 循环确实提高
如何在命令提示符下生成“可读”退格键? 我有一个微型 C 应用程序,我正在尝试使用 getchar() 方法从输入中读取退格键。 是否有任何组合键可以触发它(并且仍然能够捕获它)? (类似于 Ctrl
我是一名优秀的程序员,十分优秀!