python - stacking sparse and dense matrices -
is possible stack sparse , dense numpy array in python? know can done dense numpy arrays using vstack/hstack. have columns add sparse matrix in order increase number of feature vectors
yes, can use scipy.sparse.vstack
, scipy.sparse.hstack
, in same way use numpy.vstack
, numpy.hstack
dense arrays.
example:
from scipy.sparse import coo_matrix m = coo_matrix(np.array([[0,0,1],[1,0,0],[1,0,0]])) = np.ones(m.shape)
with np.vstack
:
np.vstack((a,m)) #valueerror: input array dimensions except concatenation axis must match
with scipy.sparse.vstack
:
scipy.sparse.vstack((a,m)) #<6x3 sparse matrix of type '<type 'numpy.float64'>' # 12 stored elements in coordinate format>
Comments
Post a Comment