Python create an empty sparse matrix

前端 未结 2 465
庸人自扰
庸人自扰 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:07

    Use a sparse format, which supports efficient indexing, like dok_matrix

    This is an efficient structure for constructing sparse matrices incrementally.

    ...

    Allows for efficient O(1) access of individual elements. Duplicates are not allowed. Can be efficiently converted to a coo_matrix once constructed.

    The last sentence can be generalized to: can be efficiently converted to all the other common formats if needed.

    from scipy.sparse import dok_matrix
    
    M = dok_matrix((100, 100))  # extra brackets needed as mentioned in comments
                                # thanks Daniel!
    M[0,3] = 5
    

提交回复
热议问题