【问题标题】:What is the default indexing type of scipy.sparse.csr_matrix?scipy.sparse.csr_matrix 的默认索引类型是什么?
【发布时间】:2017-07-21 19:55:02
【问题描述】:

scipy.sparse.csr_matrix 具有 dataindicesindptr 属性。

indicesindptr的默认dtype是什么?

对于numpy,默认索引类型为numpy.intp,但这与indicesdtypescipy.sparse.csr_matrix 不匹配。

Documentation 的 scipy.sparse.csr_matrix

对于我的笔记本电脑:

import numpy as np
import scipy.sparse as ss
a = ss.csr_matrix(np.arange(12).reshape(3,4), dtype=float)
print(a.indices.dtype)
print(np.intp)

结果:

int32
<class 'numpy.int64'>

【问题讨论】:

    标签: numpy scipy sparse-matrix


    【解决方案1】:

    sparse.compressed._cs_matrix__init__

                idx_dtype = get_index_dtype(maxval=max(M,N))
                self.data = np.zeros(0, getdtype(dtype, default=float))
                self.indices = np.zeros(0, idx_dtype)
                self.indptr = np.zeros(self._swap((M,N))[0] + 1, dtype=idx_dtype)
    

    sparse.compressed.get_index_dtype 根据矩阵的形状在np.int32np.int64 之间进行选择。如果太大而无法使用32 进行索引,则使用64。但请检查该功能以了解详细信息。


    In [789]:  np.iinfo(np.int32).max
    Out[789]: 2147483647
    In [790]: a=sparse.csr_matrix((1,2147483646))
    In [791]: a
    Out[791]: 
    <1x2147483646 sparse matrix of type '<class 'numpy.float64'>'
        with 0 stored elements in Compressed Sparse Row format>
    In [792]: a.indices.dtype
    Out[792]: dtype('int32')
    In [793]: a=sparse.csr_matrix((1,2147483648))
    In [794]: a.indices.dtype
    Out[794]: dtype('int64')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-29
      • 2014-07-28
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 2010-12-06
      • 2014-09-30
      相关资源
      最近更新 更多