Skip to main content
 首页 » 编程设计

python之无法在 scipy 中创建非空 csr.matrix()

2024年10月25日11kuangbin

我需要稀疏矩阵来解决问题,根据 scipy 中 scr.matrix() 的描述 http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html#scipy.sparse.csr_matrix它非常适合我的问题。 但是我什至无法初始化它。 当我使用此文档中的空矩阵示例时 http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.sparse.csr_matrix.html它工作正常,与文档中的完全一样

>>> import numpy as np 
>>> from scipy.sparse import csr_matrix 
>>> csr_matrix((3, 4), dtype=np.int8).toarray() 
array([[0, 0, 0, 0], 
       [0, 0, 0, 0], 
       [0, 0, 0, 0]], dtype=int8) 

但是当我使用非空矩阵的例子或者尝试用我自己的数据填充它时

>>> row = np.array([0, 0, 1, 2, 2, 2]) 
>>> col = np.array([0, 2, 2, 0, 1, 2]) 
>>> data = np.array([1, 2, 3, 4, 5, 6]) 
>>> csr_matrix((data, (row, col)), shape=(3, 3)).toarray() 

我总是收到这条消息

/Library/Python/2.7/site-packages/numpy-1.9.2-py2.7-macosx-10.10- 
intel.egg/numpy/core/fromnumeric.py:2507: VisibleDeprecationWarning:  
`rank` is deprecated; use the `ndim` attribute or function instead.  
 To find the rank of a matrix see `numpy.linalg.matrix_rank`. 
 VisibleDeprecationWarning) 

这是什么意思?我完全卡住了。不好意思问这个问题,我是 scipy 的新手,需要帮助。

请您参考如下方法:

这只是一个警告,我希望创建您的矩阵。 Scipy 正在调用一个旧的 numpy 函数。它已于 2014 年 4 月在 scipy 中修复。

Scipy 变化在: https://github.com/scipy/scipy/commit/fa1782e04fdab91f672ccf7a4ebfb887de50f01c