本文介绍了切片scipy.sparse矩阵的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通常使用
matrix[:, i:]
它似乎没有我预期的那样快.
It seems not work as fast as I expected.
推荐答案
如果要获取稀疏矩阵作为输出,执行行切片的最快方法是使用csr
类型,对于列切片csc
,在此进行详细介绍.在这两种情况下,您都只需要执行当前操作即可:
If you want to obtain a sparse matrix as output the fastest way to do row slicing is to have a csr
type, and for columns slicing csc
, as detailed here. In both cases you just have to do what you are currently doing:
matrix[l1:l2,c1:c2]
如果要输出其他类型,可能有更快的方法. 在另一个答案中,它解释了许多切片矩阵的方法以及它们的不同时序.例如,如果您希望将ndarray
作为输出,则最快的切片是:
If you want another type as output there maybe faster ways. In this other answer it is explained many methods for slicing a matrix and their different timings compared. For example, if you want a ndarray
as output the fastest slicing is:
matrix.A[l1:l2,c1:c2]
或:
matrix.toarray()[l1:l2,c1:c2]
比:
matrix[l1:l2,c1:c2].A #or .toarray()
这篇关于切片scipy.sparse矩阵的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!