有效地重塑稀疏矩阵,Python,SciPy 0.12 [英] Reshape sparse matrix efficiently, Python, SciPy 0.12

查看:147
本文介绍了有效地重塑稀疏矩阵,Python,SciPy 0.12的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在SciPy中调整稀疏矩阵大小的另一篇文章中,当要分别使用scipy.sparse.vstackhstack添加更多的行或列时,答案有效.在SciPy 0.12中, reshape

In another post regarding resizing of a sparse matrix in SciPy the accepted answer works when more rows or columns are to be added, using scipy.sparse.vstack or hstack, respectively. In SciPy 0.12 the reshape or set_shape methods are still not implemented.

是否有一些稳定的良好做法来重塑SciPy 0.12中的稀疏矩阵?进行一些时间比较会很好.

Are there some stabilished good practices to reshape a sparse matrix in SciPy 0.12? It would be nice to have some timing comparisons.

推荐答案

SciPy 1.1.0 ,即

As of SciPy 1.1.0, the reshape and set_shape methods have been implemented for all sparse matrix types. The signatures are what you would expect and are as identical to the equivalent methods in NumPy as feasible (e.g. you can't reshape to a vector or tensor).

签名:

reshape(self, shape: Tuple[int, int], order: 'C'|'F' = 'C', copy: bool = False) -> spmatrix

示例:

>>> from scipy.sparse import csr_matrix
>>> A = csr_matrix([[0,0,2,0], [0,1,0,3]])
>>> print(A)
  (0, 2)    2
  (1, 1)    1
  (1, 3)    3
>>> B = A.reshape((4,2))
>>> print(B)
  (1, 0)    2
  (2, 1)    1
  (3, 1)    3
>>> C = A.reshape((4,2), order='F')
>>> print(C)
  (0, 1)    2
  (3, 0)    1
  (3, 1)    3

完全公开:我写了实现.

这篇关于有效地重塑稀疏矩阵,Python,SciPy 0.12的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆