Python create an empty sparse matrix

前端 未结 2 453
庸人自扰
庸人自扰 2021-01-20 14:41

I am trying to parse some real data into a .mat object to be loaded in my matlab script.

I am getting this error:

TypeError: \'co

2条回答
  •  不要未来只要你来
    2021-01-20 15:20

    Constructing this matrix with coo_matrix, using the (data, (rows, cols))` parameter format:

    In [2]: from scipy import sparse
    In [3]: from scipy import io
    In [4]: data=np.array([[10,45],[11,12],[4,1]])
    In [5]: data
    Out[5]: 
    array([[10, 45],
           [11, 12],
           [ 4,  1]])
    In [6]: rows = data[:,0]
    In [7]: cols = data[:,1]
    In [8]: data = np.ones(rows.shape, dtype=int)
    In [9]: M = sparse.coo_matrix((data, (rows, cols)), shape=(100,100))
    In [10]: M
    Out[10]: 
    <100x100 sparse matrix of type ''
        with 3 stored elements in COOrdinate format>
    In [11]: print(M)
      (10, 45)  1
      (11, 12)  1
      (4, 1)    1
    

    If you save it to a .mat file for use in MATLAB, it will save it in csc format (having converted it from the coo):

    In [13]: io.savemat('test.mat',{'M':M})
    In [14]: d = io.loadmat('test.mat')
    In [15]: d
    Out[15]: 
    {'M': <100x100 sparse matrix of type ''
        with 3 stored elements in Compressed Sparse Column format>,
     '__globals__': [],
     '__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Mon Aug  7 08:45:12 2017',
     '__version__': '1.0'}
    

    coo format does not implement item assignment. csr and csc do implement it, but will complain. But they are the normal formats for calculation. lil and dok are the best formats for iterative assignment.

提交回复
热议问题