How to elementwise-multiply a scipy.sparse matrix by a broadcasted dense 1d array?

I replied over at scipy.org as well, but I thought I should add an answer here, in case others find this page when searching. You can turn the vector into a sparse diagonal matrix and then use matrix multiplication (with *) to do the same thing as broadcasting, but efficiently. >>> d = ssp.lil_matrix((3,3)) >>> … Read more

Concatenate sparse matrices in Python using SciPy/Numpy

You can use the scipy.sparse.hstack to concatenate sparse matrices with the same number of rows (horizontal concatenation): from scipy.sparse import hstack hstack((X, X2)) Similarly, you can use scipy.sparse.vstack to concatenate sparse matrices with the same number of columns (vertical concatenation). Using numpy.hstack or numpy.vstack will create an array with two sparse matrix objects.

Iterating through a scipy.sparse vector (or matrix)

Edit: bbtrb’s method (using coo_matrix) is much faster than my original suggestion, using nonzero. Sven Marnach’s suggestion to use itertools.izip also improves the speed. Current fastest is using_tocoo_izip: import scipy.sparse import random import itertools def using_nonzero(x): rows,cols = x.nonzero() for row,col in zip(rows,cols): ((row,col), x[row,col]) def using_coo(x): cx = scipy.sparse.coo_matrix(x) for i,j,v in zip(cx.row, cx.col, … Read more

LCP with sparse matrix

This problem has a very efficient (linear time) solution, though it requires a bit of discussion… Zeroth: clarifying the problem / LCP Per clarifications in the comments, @FooBar says the original problem is elementwise min; we need to find a z (or v) such that either the left argument is zero and the right argument … Read more

Scipy sparse… arrays?

Use a scipy.sparse format that is row or column based: csc_matrix and csr_matrix. These use efficient, C implementations under the hood (including multiplication), and transposition is a no-op (esp. if you call transpose(copy=False)), just like with numpy arrays. EDIT: some timings via ipython: import numpy, scipy.sparse n = 100000 x = (numpy.random.rand(n) * 2).astype(int).astype(float) # … Read more