有效地重塑稀疏矩阵,Python,SciPy 0.12

Sau*_*tro 4 python scipy sparse-matrix

关于在SciPy中调整稀疏矩阵大小的另一篇文章中,当分别使用scipy.sparse.vstack或添加更多行或列时,接受的答案有效hstack.在SciPy 0.12中,仍未实施reshapeset_shape方法.

是否有一些稳定的良好实践来重塑SciPy 0.12中的稀疏矩阵?进行一些时序比较会很不错.

War*_*ser 8

我不知道任何既定的良好实践,所以这里是一个相当直接的重塑函数,适用于coo_matrix.它将其参数转换为coo_matrix,因此它将实际用于其他稀疏格式(但它返回一个coo_matrix).

from scipy.sparse import coo_matrix


def reshape(a, shape):
    """Reshape the sparse matrix `a`.

    Returns a coo_matrix with shape `shape`.
    """
    if not hasattr(shape, '__len__') or len(shape) != 2:
        raise ValueError('`shape` must be a sequence of two integers')

    c = a.tocoo()
    nrows, ncols = c.shape
    size = nrows * ncols

    new_size =  shape[0] * shape[1]
    if new_size != size:
        raise ValueError('total size of new array must be unchanged')

    flat_indices = ncols * c.row + c.col
    new_row, new_col = divmod(flat_indices, shape[1])

    b = coo_matrix((c.data, (new_row, new_col)), shape=shape)
    return b
Run Code Online (Sandbox Code Playgroud)

例:

In [43]: a = coo_matrix([[0,10,0,0],[0,0,0,0],[0,20,30,40]])

In [44]: a.A
Out[44]: 
array([[ 0, 10,  0,  0],
       [ 0,  0,  0,  0],
       [ 0, 20, 30, 40]])

In [45]: b = reshape(a, (2,6))

In [46]: b.A
Out[46]: 
array([[ 0, 10,  0,  0,  0,  0],
       [ 0,  0,  0, 20, 30, 40]])
Run Code Online (Sandbox Code Playgroud)

现在,我确信这里有几个常规贡献者可以提出更好的东西(更快,更高效,更少填充... :)