python - concatenating scipy matrices -


i want concatenate 2 csr_matrix, each shape=(1,n).

i know should use scipy.sparse.vstack:

from scipy.sparse import csr_matrix,vstack c1 = csr_matrix([[1, 2]])  c2 = csr_matrix([[3, 4]])  print c1.shape,c2.shape print vstack([c1, c2], format='csr')  #prints: (1, 2) (1, 2)   (0, 0)    1   (0, 1)    2   (1, 0)    3   (1, 1)    4 

however, code fails:

from scipy.sparse import csr_matrix,vstack import numpy np y_train = np.array([1, 0, 1, 0, 1, 0]) x_train = csr_matrix([[1, 1], [-1, 1], [1, 0], [-1, 0], [1, -1], [-1, -1]])  c0 = x_train[y_train == 0].mean(axis=0) c1 = x_train[y_train == 1].mean(axis=0)  print c0.shape, c1.shape #prints (1l, 2l) (1l, 2l) print c0,c1 #prints [[-1.  0.]] [[ 1.  0.]] print vstack([c0,c1], format='csr') 

the last line raises exception -

file "c:\anaconda\lib\site-packages\scipy\sparse\construct.py", line 484, in vstack
return bmat([[b] b in blocks], format=format, dtype=dtype)

file "c:\anaconda\lib\site-packages\scipy\sparse\construct.py", line 533, in bmat
raise valueerror('blocks must 2-d') valueerror: blocks must 2-d

i guess using mean has out. ideas?

taking mean of sparse matrix returns numpy matrix (which not sparse). c0 , c1 matrices:

in [76]: type(c0) out[76]: numpy.matrixlib.defmatrix.matrix  in [89]: sparse.issparse(c0) out[94]: false 

vstack expects first argument sequence of sparse matrices. make (at least) first matrix sparse matrix:

in [31]: vstack([coo_matrix(c0), c1]) out[31]:  <2x2 sparse matrix of type '<type 'numpy.float64'>'     2 stored elements in coordinate format>  in [32]: vstack([coo_matrix(c0), c1]).todense() out[32]:  matrix([[-1.,  0.],         [ 1.,  0.]]) 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -