pandas 数据框到coo矩阵和lil matix [英] pandas dataframe to coo matrix and to lil matix

查看:110
本文介绍了 pandas 数据框到coo矩阵和lil matix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下系列文章:

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

如何将该数据帧转换为.tocoo()矩阵和.tolil()?

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

参考如何从中形成combined列 原始的熊猫数据框:

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具有超过10K的独特功能,col 1具有39个组,col 2仅为1.

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.

推荐答案

Formation of COOrdinate format from original pandas DataFrame

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>

这篇关于 pandas 数据框到coo矩阵和lil matix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆