如何使用scipy.sparse vstack python

时间:2017-12-06 14:48:01

标签: python scipy

我有一个由两个不同矩阵组成的vstack。

im = imread('input.jpg')
dxy = spnabla(im.shape[0], im.shape[1])
def spnabla(M, N):
    dx = spnabla_x(M, N)
    dy = spnabla_y(M, N)
    dxy = sp.vstack((dx, dy))
    return dxy

def spnabla_x(M, N):
    a = np.append(np.ones(N-1), 0)
    dx = sp.diags([np.tile(-a, M), np.tile(a, M)], [0, 1], (M*N, M*N))
    return dx.tocsr()

def spnabla_y(M, N):
    b = np.append(np.tile(np.ones(N), M-1), np.zeros(N))
    dy = sp.diags([-b, b], [0, N], (M*N,M*N))
    return dy.tocsr()

如何在dxy vstack中访问dx和dy?我想将dx和dy显示为图像。那可能吗?

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

sp.vstack垂直连接矩阵,就像np.vstack一样(除了它与组件的coo属性一起工作)。

In [93]: dx = spnabla_x(10,10)
In [94]: dy = spnabla_y(10,10)
In [95]: dx
Out[95]: 
<100x100 sparse matrix of type '<class 'numpy.float64'>'
    with 180 stored elements in Compressed Sparse Row format>
In [96]: dy
Out[96]: 
<100x100 sparse matrix of type '<class 'numpy.float64'>'
    with 180 stored elements in Compressed Sparse Row format>
In [97]: dx.A
Out[97]: 
array([[-1.,  1.,  0., ...,  0.,  0.,  0.],
       [ 0., -1.,  1., ...,  0.,  0.,  0.],
       [ 0.,  0., -1., ...,  0.,  0.,  0.],
       ..., 
       [ 0.,  0.,  0., ..., -1.,  1.,  0.],
       [ 0.,  0.,  0., ...,  0., -1.,  1.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]])
In [98]: dy.A
Out[98]: 
array([[-1.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0., -1.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0., -1., ...,  0.,  0.,  0.],
       ..., 
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]])
In [99]: dxy = sp.vstack((dx,dy))
In [100]: dxy
Out[100]: 
<200x100 sparse matrix of type '<class 'numpy.float64'>'
    with 360 stored elements in Compressed Sparse Row format>

In [101]: dxy
Out[101]: 
<200x100 sparse matrix of type '<class 'numpy.float64'>'
    with 360 stored elements in Compressed Sparse Row format>
In [102]: np.allclose(dxy[:100,:].A,dx.A)

因此N的{​​{1}}行与dxy等相同。我应该警告稀疏矩阵的索引比常规numpy数组慢得多。并且它不会创建dx;它是一个有自己数据的新矩阵。除非您需要对整个view进行一些计算,否则在堆叠dxydx时没有多大意义。

答案 1 :(得分:0)

@hpaulj的答案是正确的,但是这个错误或功能特性只对我有点困扰: sparse.vstack(( csr, csr )) –> csr,但
sparse.vstack((其他))–> coo!?

这里有一个小测试台来展示这一点:

"""print the types of A op B for A, B scipy.sparse csr, csc, lil """
from __future__ import print_function
import sys
import numpy as np
import scipy
from scipy import sparse

print( "\n" + 80 * "=" )
print( "versions: numpy %s  scipy %s  python %s \n" % (
        np.__version__, scipy.__version__, sys.version.split()[0] ))

I = np.eye( 3 )
As = [ sparse.csr_matrix(I),
    sparse.csc_matrix(I),
    sparse.lil_matrix(I) ]

def binop_types( binop, As=As, Bs=As ):
    """ print the types of A op B for A in As, B in Bs """
    for A in As:
        for B in Bs:
            AopB = binop( A, B )
            print( "%s %s -> %s " % (
                type(A).__name__,
                type(B).__name__,
                type(AopB).__name__ ))

def hstackop( A, B ):
    return sparse.hstack(( A, B ))
def vstackop( A, B ):
    return sparse.vstack(( A, B ))

print( __doc__ )
print( "\n-- vstack --" )
binop_types( vstackop )

print( "\n-- hstack --" )
binop_types( hstackop )

print( "\n-- + --" )
binop_types( lambda A, B: A + B )