python - 为 SciPy 稀疏 CSC 矩阵构建 Indptr
问题描述
我有许多表示我需要表示为 SciPy sparse 的稀疏矩阵(即具有非零条目的列)的列表csc_matrix
。但是,请注意,我的稀疏矩阵中只有一行,因此列表仅指向该行中具有非零条目的列。例如:
sparse_input = [4, 10, 21] # My lists are much, much longer but very sparse
该列表告诉我单行稀疏矩阵中的哪些列存在非零值。这就是密集矩阵的样子。
x = np.array([[0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1]])
我可以使用(data, (row, col))
语法,但由于我的列表超长csc_matrix
,构建需要大量时间和内存。因此,我正在考虑使用该indptr
界面,但我无法弄清楚如何indptr
直接从给定的非零列条目的稀疏列表中快速自动构建。我试着看,csr_matrix(x).indptr
我看到indptr
看起来像:
array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3], dtype=int32)
我已经阅读了 SciPy 文档和稀疏矩阵维基百科页面,但我似乎无法想出一种有效的方法来indptr
直接从非零列的列表中构造。考虑到稀疏矩阵中只有三个非零条目,感觉indptr
不应该这么长。
解决方案
只是制作矩阵并探索它们的属性怎么样?
In [144]: from scipy import sparse
In [145]: x = np.array([[0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1]])
In [146]: M = sparse.coo_matrix(x)
In [147]: M
Out[147]:
<1x22 sparse matrix of type '<class 'numpy.int64'>'
with 3 stored elements in COOrdinate format>
In [148]: M.row
Out[148]: array([0, 0, 0], dtype=int32)
In [149]: M.col
Out[149]: array([ 4, 10, 21], dtype=int32)
In [150]: M.data
Out[150]: array([1, 1, 1])
社会责任:
In [152]: Mr = M.tocsr()
In [153]: Mr.indptr
Out[153]: array([0, 3], dtype=int32)
In [155]: Mr.indices
Out[155]: array([ 4, 10, 21], dtype=int32)
In [156]: Mr.data
Out[156]: array([1, 1, 1], dtype=int64)
csc:
In [157]: Mc = M.tocsc()
In [158]: Mc.indptr
Out[158]:
array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3], dtype=int32)
In [159]: Mc.indices
Out[159]: array([0, 0, 0], dtype=int32)
In [160]: Mc.data
Out[160]: array([1, 1, 1], dtype=int64)
直接nonzero
上x
:
In [161]: np.nonzero(x)
Out[161]: (array([0, 0, 0]), array([ 4, 10, 21]))
对于像这样的 1 行矩阵,我怀疑您是否会通过csr
indptr
直接创建来节省很多时间。大部分工作将在nonzero
步骤中进行。但请随意体验。
===
一些时间
In [162]: timeit sparse.coo_matrix(x)
95.8 µs ± 110 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [163]: timeit sparse.csr_matrix(x)
335 µs ± 2.59 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [164]: timeit M.tocsr()
115 µs ± 948 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [165]: timeit M.tocsc()
117 µs ± 90.4 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [166]: sparse.csr_matrix?
In [167]: timeit M.tocsc()
117 µs ± 1.17 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [168]: timeit sparse.csc_matrix(x)
335 µs ± 257 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [169]: timeit sparse.coo_matrix(x).tocsr()
219 µs ± 3.34 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
我有点惊讶的csr_matrix
是,它比coo
转换慢。
现在让我们尝试用indptr
etc制作矩阵。
In [170]: timeit np.nonzero(x)
2.52 µs ± 65.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [173]: timeit sparse.csr_matrix((Mr.data, Mr.indices, Mr.indptr))
92.5 µs ± 79.3 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [174]: %%timeit
...: indices = np.nonzero(x)[1]
...: data = np.ones_like(indices)
...: indptr = np.array([0,len(indices)])
...: sparse.csr_matrix((data, indices, indptr))
...:
...:
161 µs ± 605 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
推荐阅读
- java - 二叉树的打印级别顺序遍历,从左到右和从右到左交替
- python - 一次从 Numpy 数组中选择多个切片
- c# - 可编辑组合框不获取项目 wpf
- git - 拥有一个映射不同文件夹的 git repo 远程
- vb.net - WCF 服务托管在 IIS10 上。可以浏览 .svc 文件但 wsdl 不显示
- python - SQL 风格在 Dask Series 或 DataFrame 列上展开
- mysql - NOT IN 子查询与 ON != 操作
- python - keras 中的自定义损失函数返回 nan loss
- python - 使用字典的python中的Dijkstra算法
- nuget - 无法让 Pipeline 将自定义 NuGet 服务器与 Azure DevOps 构建一起使用