[Numpy-discussion] Adapting algorithm to accept Scipy sparse matrix

David Koch ogdude@googlemail....
Mon Mar 26 08:26:47 CDT 2007

Hi again,

I want to select/access several columns from a sparse csc_matrix. The only
way I could think of is the following enormously inefficient algorithm which
basically initalizes a new lil_matrix (for assigments) and loops over all
the specified columns and does sparse -> dense -> sparse. All this, to
overcome the inability of using "multi-column" slices in csc_matrices.

def spSelCol(X, A):
    "insert doc string"

    n = size(X,0)
    d = size(A)
    X = X.tocsc()
    newX = sparse.lil_matrix((d,n))
    for i in range(0, d):
        # sparse -> dense -> sparse: not good!
        newX[i,:] = X[:,A[i]].toarray().flatten()

    return newX.transpose()

Is there any way the operation can be made more efficient or should I look
elsewhere (CVXOPT Python toolbox ...)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070326/edc34d92/attachment.html 

More information about the Numpy-discussion mailing list