Building and updating a sparse matrix in python using scipy

后端 未结 3 590
北海茫月
北海茫月 2020-12-24 14:39

I\'m trying to build and update a sparse matrix as I read data from file. The matrix is of size 100000X40000

What is the most efficient way of updating

3条回答
  •  清酒与你
    2020-12-24 15:04

    import scipy.sparse
    
    rows = [2, 236, 246, 389, 1691]
    cols = [117, 3, 34, 2757, 74, 1635, 52]
    prod = [(x, y) for x in rows for y in cols] # combinations
    r = [x for (x, y) in prod] # x_coordinate
    c = [y for (x, y) in prod] # y_coordinate
    data = [1] * len(r)
    m = scipy.sparse.coo_matrix((data, (r, c)), shape=(100000, 40000))
    

    I think it works well and doesn't need loops. I am directly following the doc

    <100000x40000 sparse matrix of type ''
        with 35 stored elements in COOrdinate format>
    

提交回复
热议问题