我想评估python(2.7)中numexpr模块的性能。为此,我创建了一个大小为(10 ^ 5,10 ^ 5)的随机稀疏矩阵。但是,下面的脚本已经在表达式求值步骤中引发了错误,说它无法识别对象类型。

我究竟做错了什么?

码:

import timeit
import scipy.sparse as sps
import numpy as np
import numexpr as ne

test_matrix = sps.rand(1e4, 1e4, density=0.01, format='coo', dtype = np.float32)
ne.evaluate('sum(test_matrix, axis = 1)')

setup = 'import numexpr as ne; import numpy as np'
print min(timeit.Timer('ne.evaluate(sum(test_matrix, axis = 1))', setup=setup).repeat(7, 1000))


错误:

追溯(最近一次通话):

File "benchmark_expressmath.py", line 19, in <module>
ne.evaluate('sum(test_matrix, axis = 1)')
File "C:\Users\blahblah\AppData\Local\Continuum\Anaconda\lib\site-packages\numexpr\necompiler.py", line 756, in evaluate
signature = [(name, getType(arg)) for (name, arg) in zip(names, arguments)]
File "C:\Users\blahblah\AppData\Local\Continuum\Anaconda\lib\site-packages\numexpr\necompiler.py", line 654, in getType
raise ValueError("unknown type %s" % a.dtype.name)
ValueError: unknown type object

最佳答案

numexpr期望变量为numpy数组。它不处理scipy的稀疏矩阵。 (例如,请参阅此电子邮件线程:http://numpy-discussion.10968.n7.nabble.com/ANN-numexpr-2-3-final-released-td36154.html

07-24 18:18