csv to sparse matrix in python

后端 未结 3 982
长发绾君心
长发绾君心 2020-12-09 05:53

I have a big csv file which lists connections between nodes in a graph. example:

0001,95784
0001,98743
0002,00082
0002,00091

So this means that

3条回答
  •  心在旅途
    2020-12-09 06:45

    Example using lil_matrix (list of list matrix) of scipy.

    Row-based linked list matrix.

    This contains a list (self.rows) of rows, each of which is a sorted list of column indices of non-zero elements. It also contains a list (self.data) of lists of these elements.

    $ cat 1938894-simplified.csv
    0,32
    1,21
    1,23
    1,32
    2,23
    2,53
    2,82
    3,82
    4,46
    5,75
    7,86
    8,28
    

    Code:

    #!/usr/bin/env python
    
    import csv
    from scipy import sparse
    
    rows, columns = 10, 100
    matrix = sparse.lil_matrix( (rows, columns) )
    
    csvreader = csv.reader(open('1938894-simplified.csv'))
    for line in csvreader:
        row, column = map(int, line)
        matrix.data[row].append(column)
    
    print matrix.data
    

    Output:

    [[32] [21, 23, 32] [23, 53, 82] [82] [46] [75] [] [86] [28] []]
    

提交回复
热议问题