scipy.sparse.csr_matrix的默认索引类型是什么? [英] What is the default indexing type of scipy.sparse.csr_matrix?

查看:71
本文介绍了scipy.sparse.csr_matrix的默认索引类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

scipy.sparse.csr_matrix具有dataindicesindptr属性.

indicesindptr的默认dtype是什么?

对于numpy,默认索引类型为numpy.intp,但与scipy.sparse.csr_matrixindicesdtype不匹配.

For numpy, the default indexing type is numpy.intp, but that doesn't match the dtype of indices of a 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'>

推荐答案

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.但是请检查该功能以获取详细信息.

sparse.compressed.get_index_dtype chooses between np.int32 and np.int64 depending on the shape of the matrix. If too big to index with 32 it uses 64. But check that function for details.

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')

这篇关于scipy.sparse.csr_matrix的默认索引类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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