我遇到了与this question中提到的相同的问题,即我希望2D相关的标量输出为matlab cgi.reg
,但是在python中。我通过编写自己的算法解决了这个问题,但它存在性能问题。
它基于matlab docs中使用的公式:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.cgi\Shell\ExceCGI\Command]
@="C:\\Perl\\bin\\perl.exe -T"
它有效,但速度非常慢。任何想法如何让它更优雅,更快?
提前致谢!
答案 0 :(得分:1)
这种vectorized
方法应该为您加速 -
# Vectorized versions of c,d,e
c_vect = (a-AM)*(b-BM)
d_vect = (a-AM)**2
e_vect = (b-BM)**2
# Finally get r using those vectorized versions
r_out = np.sum(c_vect)/float(np.sqrt(np.sum(d_vect)*np.sum(e_vect)))
运行时测试并验证结果
In [70]: M = 100
...: N = 100
...: a = np.random.rand(M,N)
...: b = np.random.rand(M,N)
...:
...: k = np.shape(a)
...: H=k[0]
...: W=k[1]
...: AM=np.mean(a)
...: BM=np.mean(b)
...:
In [71]: %%timeit
...: c = np.zeros((H,W))
...: d = np.zeros((H,W))
...: e = np.zeros((H,W))
...:
...: for ii in range(0,H):
...: for jj in range(0,W):
...: c[ii,jj]=(a[ii,jj]-AM)*(b[ii,jj]-BM)
...: d[ii,jj]=(a[ii,jj]-AM)**2
...: e[ii,jj]=(b[ii,jj]-BM)**2
...:
...: r = np.sum(c)/float(np.sqrt(np.sum(d)*np.sum(e)))
...:
10 loops, best of 3: 24.7 ms per loop
In [72]: %%timeit
...: c_vect = (a-AM)*(b-BM)
...: d_vect = (a-AM)**2
...: e_vect = (b-BM)**2
...: r_out = np.sum(c_vect)/float(np.sqrt(np.sum(d_vect)*np.sum(e_vect)))
...:
10000 loops, best of 3: 64.7 µs per loop
In [73]: np.allclose(r,r_out)
Out[73]: True