将非数字元组转换为numpy矩阵 [英] Convert non-numeric tuples to numpy matrix

查看:184
本文介绍了将非数字元组转换为numpy矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过一个非常大的元组列表来表示一个邻接矩阵.如何使用numpy矩阵或scipy.sparse矩阵表示此列表,以便使用igraph或networkx之类的包?

I'm trying to represent an adjacency matrix via a very large list of tuples. How would I represent this list in a numpy matrix or scipy.sparse matrix so as to use a package like igraph or networkx?

[('b', 'c'),
 ('b', 'a'),
 ('c', 'd'),
 ('c', 'a'),
 ('c', 'b'),
 ('a', 'b'),
 ('a', 'c')]

如果这是重复的,我深表歉意,但是我找不到任何有关如何将非数字元组转换为邻接矩阵的文档.

I apologize if this is duplicate, but I can't find any documentation on how to convert non-numeric tuples into adjacency matrices.

推荐答案

NetworkX可以具有非数字节点.如果我正确理解了您的问题,则可以像下面这样轻松地在networkx图形中使用它:

NetworkX can have non-numeric nodes. If I'm understanding your question correctly, you can use it in a networkx graph as easily as this:

import networkx as nx
g = nx.Graph([('a', 'b'), ('c', 'd'), ...])

您是在问如何将其转换为scipy.sparsenumpy矩阵吗?还是要与graph和networkx一起使用?

Are you asking how to convert that to a scipy.sparse or numpy matrix? Or do you want it for use with graph and networkx?

这篇关于将非数字元组转换为numpy矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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