- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在从事我的第一个大型 Python 项目。我有一个函数,其中包含以下代码:
# EXPAND THE EXPECTED VALUE TO APPLY TO ALL STATES,
# THEN UPDATE fullFnMat
EV_subset_expand = np.kron(EV_subset, np.ones((nrows, 1)))
fullFnMat[key] = staticMat[key] + EV_subset_expand
在我的代码分析器中,这个 kronecker 产品似乎占用了大量时间。
Function was called by...
ncalls tottime cumtime
/home/stevejb/myhg/dpsolve/ootest/tests/ddw2011/profile_dir/BellmanEquation.py:17(bellmanFn) <- 19 37.681 38.768 /home/stevejb/myhg/dpsolve/ootest/tests/ddw2011/profile_dir/dpclient.py:467(solveTheModel)
{numpy.core.multiarray.concatenate} <- 342 27.319 27.319 /usr/lib/pymodules/python2.7/numpy/lib/shape_base.py:665(kron)
/home/stevejb/myhg/dpsolve/ootest/tests/ddw2011/profile_dir/dpclient.py:467(solveTheModel) <- 1 11.041 91.781 <string>:1(<module>)
{method 'argsort' of 'numpy.ndarray' objects} <- 19 7.692 7.692 /usr/lib/pymodules/python2.7/numpy/core/fromnumeric.py:597(argsort)
/usr/lib/pymodules/python2.7/numpy/core/numeric.py:789(outer) <- 171 2.526 2.527 /usr/lib/pymodules/python2.7/numpy/lib/shape_base.py:665(kron)
{method 'max' of 'numpy.ndarray' objects} <- 209 2.034 2.034 /home/stevejb/myhg/dpsolve/ootest/tests/ddw2011/profile_dir/dpclient.py:391(getValPolMatrices)
有没有办法在 Numpy 中获得更快的 kronecker 产品?看起来它不应该花这么长时间。
最佳答案
您当然可以查看np.kron
的源代码。它可以在 numpy/lib/shape_base.py
中找到,您可以查看是否可以进行改进或简化以提高效率。或者,您可以使用 Cython 或其他一些绑定(bind)到低级语言来编写自己的代码,以尝试获得更好的性能。
或者正如@matt 所建议的那样,像下面这样的东西本身可能会更快:
import numpy as np
nrows = 10
a = np.arange(100).reshape(10,10)
b = np.tile(a,nrows).reshape(nrows*a.shape[0],-1) # equiv to np.kron(a,np.ones((nrows,1)))
或:
b = np.repeat(a,nrows*np.ones(a.shape[0],np.int),axis=0)
时间:
In [80]: %timeit np.tile(a,nrows).reshape(nrows*a.shape[0],-1)
10000 loops, best of 3: 25.5 us per loop
In [81]: %timeit np.kron(a,np.ones((nrows,1)))
10000 loops, best of 3: 117 us per loop
In [91]: %timeit np.repeat(a,nrows*np.ones(a.shape[0],np.int),0)
100000 loops, best of 3: 12.8 us per loop
在上面的示例中,对大小数组使用 np.repeat
可以实现相当不错的 10 倍加速,这还算不错。
关于python - 加速 numpy kronecker 产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7193870/
我在构建执行特定任务的可变参数函数时遇到问题。对于我的特定示例,我使用的是 2x2 矩阵,但您可以想象我的问题可以推广到许多数据类型。此外,对于我的示例,“眼睛”是 2x2 单位矩阵。 好的,所以我想
我正在尝试使用 cvxpy 进行优化。 w=cvxpy.Variable((10,1)) cvxpy.kron(w,w) 这不起作用说第一个参数应该是常量, 然后我尝试使用 numpy numpy.k
我想加速以下 for 循环。 % Use random matrices for testing. % Elapsed time of the following code is around 19
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
因此,我正在尝试计算两个任意维度矩阵的克罗内克积。 (为了示例,我使用了相同维度的方阵) 最初我尝试使用 kron : a = np.random.random((60,60)) b = np.ran
我正在从事我的第一个大型 Python 项目。我有一个函数,其中包含以下代码: # EXPAND THE EXPECTED VALUE TO APPLY TO ALL STAT
背景 生成随机权重列表后: sizes = [784,30,10] weights = [np.random.randn(y, x) for x, y in zip(sizes[:-1],sizes[
我正在尝试计算 张量积(更新:我想要的实际上叫做 Kronecker product ,这种命名困惑是我找不到 np.kron 的原因) 的多个矩阵,这样我就可以将变换应用于向量,这些向量本身就是多个
我可以通过放弃 for 循环来优化 Matlab 中的以下代码吗? A = []; B = randn(4,8); C = randn(8,4); I = randperm(8,3); J = ran
我正在开发一个 python 项目并使用 numpy。我经常需要通过单位矩阵计算矩阵的克罗内克积。这些是我代码中的一个相当大的瓶颈,所以我想优化它们。我必须服用两种产品。第一个是: np.kron(n
假设我们有 2 个 2X2 numpy 数组: X=np.array([[0,1],[1,0]]) 和 I=np.array([[1,0],[0,1]]) 考虑一下克罗内克产品 XX=X^X 我让符号
我正在尝试仅使用 repmat 和 reshape 来复制 Kron 产品,我相信我已经非常接近了,但我无法完成最后一次正确的 reshape 。特别是我在 reshape A 时遇到问题 为简单起见
我是一名优秀的程序员,十分优秀!