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

David Koch ogdude@googlemail....
Wed Mar 21 14:47:15 CDT 2007


Alright, may all the trickery rest until that day.

One thing I need to do however is patch a column of "ones" onto a sparse
matrix of format n * d with n >> d. I tried "concatenate" and it didn't work
so I did like this:

def spInsCol(X):
    "insert doc string"

    n, d = shape(X)
    X = X.tocsc()

    newX = sparse.lil_matrix((d + 1,n))
    newX[0,:] = ones(n)

    for i in range(1, d + 1):
        newX[i,:] = X[:,i - 1].toarray().flatten()

    return newX.transpose()

It basically, blows each column in the old matrix to dense format and
assigns it to a row in a new matrix which already contains an extra row of
ones. In the end I transpose. Is there a simpler and more efficient
solution?

Thank you,



David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070321/5650754c/attachment.html 


More information about the Numpy-discussion mailing list