pandas dataframe to coo matrix and to lil matix

♀尐吖头ヾ 提交于 2020-01-25 06:50:10

问题


I have following series:

groups['combined'] 

0            (28, 1)  1
1           (32, 1)  1
2           (36, 1)  1
3           (37, 1)  1
4           (84, 1)  1

....
Name: combined, Length: 14476, dtype: object

How can I convert this dataframe into .tocoo() matrix and .tolil()?

Reference how combined column is formed from Original Pandas DataFrame:

import pandas as pd pd.DataFrame ({0:[28,32,36,37,84],1: [1,1,1,1,1], 2: [1,1,1,1,1]}). col 0 has over 10K unique features, col 1 has 39 groups and col 2 is just 1.


回答1:


Formation of COOrdinate format from original pandas DataFrame

    import scipy.sparse as sps

    groups.set_index([0, 1], inplace=True)
    sps.coo_matrix((groups[2], (groups.index.labels[0], groups.index.labels[1])))

-------------Results to---------

<10312x39 sparse matrix of type '<class 'numpy.int64'>'
    with 14476 stored elements in COOrdinate format>



回答2:


In regards to lil matrix

print(len(networks[0]), len(networks[1]), networks[0].nunique(), networks[1].nunique())
667966 667966 10312 10312
networks[:5]

    0   1
0   176 1
1   233 1
2   283 1
3   371 1
4   394 1


# make row and col labels
rows = networks[0]
cols = networks[1]

# crucial third array in python
networks.set_index([0, 1], inplace=True)
Ntw= sps.coo_matrix((networks[2], (networks.index.labels[0], 
networks.index.labels[1])))


d=Ntw.tolil()
d

generates

   <10312x10312 sparse matrix of type '<class 'numpy.int64'>'
    with 667966 stored elements in LInked List format>


来源:https://stackoverflow.com/questions/59654843/pandas-dataframe-to-coo-matrix-and-to-lil-matix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!