No, there is no built-in way to do this in scipy.sparse . Easy solution
np.maximum(XA, YA)
but it will obviously be very memory intensive when the matrices are large, and this can lead to the failure of your machine. An effective (but far from fast) memory solution -
# convert to COO, if necessary X = X.tocoo() Y = Y.tocoo() Xdict = dict(((i, j), v) for i, j, v in zip(X.row, X.col, X.data)) Ydict = dict(((i, j), v) for i, j, v in zip(Y.row, Y.col, Y.data)) keys = list(set(Xdict.iterkeys()).union(Ydict.iterkeys())) XmaxY = [max(Xdict.get((i, j), 0), Ydict.get((i, j), 0)) for i, j in keys] XmaxY = coo_matrix((XmaxY, zip(*keys)))
Note that in this case, pure Python is used instead of vectorized idioms. You can try to reduce it from time to time by vectorizing parts of it.
source share