我将一段 matlab 代码移植到 python 中,但遇到了效率问题。
例如,这里有一个片段:
G = np.vstack((Gx.toarray(), Gy.toarray(), Gd1.toarray(), Gd2.toarray()))
这里要堆叠的所有元素都是 22500 x 22500 稀疏矩阵。它在我的 Windows 64 位机器上直接死掉,并出现以下错误:
return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
MemoryError
我对 Python 还很陌生,有关于此类优化最佳实践的好文章吗?有关 numpy 如何处理内存的信息吗?
据我所知,稀疏矩阵以某种压缩格式存储,占用的空间要少得多,但需要更多的时间来处理。
谢谢!
最佳答案
对于堆叠稀疏矩阵,您可以使用 Scipy sparse's vstack function而不是 NumPy 的 vstack
,就像这样 -
import scipy.sparse as sp
Gout = sp.vstack((Gx,Gy,Gd1,Gd2))
示例运行 -
In [364]: # Generate random sparse matrices
...: Gx = sp.coo_matrix(3*(np.random.rand(10,10)>0.7).astype(int))
...: Gy = sp.coo_matrix(4*(np.random.rand(10,10)>0.7).astype(int))
...: Gd1 = sp.coo_matrix(5*(np.random.rand(10,10)>0.7).astype(int))
...: Gd2 = sp.coo_matrix(6*(np.random.rand(10,10)>0.7).astype(int))
...:
In [365]: # Run original and proposed approaches
...: G = np.vstack((Gx.toarray(), Gy.toarray(), Gd1.toarray(), Gd2.toarray()))
...: Gout = sp.vstack((Gx,Gy,Gd1,Gd2))
...:
In [366]: # Finally verify results
...: np.allclose(G,Gout.toarray())
Out[366]: True
关于python - matlab 到 python 端口优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39724538/