如何在Python中创建来自facebook的相互友谊的邻接矩阵
我正在做一个关于 Facebook 网络社交网络分析的项目。我必须让我所有的朋友以及我的朋友中的哪些人彼此成为朋友,在我的网络中建立相互的友谊。我这样做了,我得到了我的朋友和邻接关系的所有 id,现在我必须形成一个邻接矩阵来指示我的两个朋友是否是朋友。例如: A和B是朋友,A和C是朋友,但B和C不是朋友。这看起来像这样:
A B C
A 0 1 1
B 1 0 0
C 1 0 0
因为我已经在 python 中有了 id 和邻接列表,所以我也应该在 python 中做矩阵,所以如果您有任何想法或基本算法如何输入 1 和 0,我将不胜感激。
I am doing a project on Social Network Analysis of Facebook Network. I had to get all my friends and who of my friends are friends with each other, mutual frindships inside my network. I did that, I got all id's of my friends and adjacencies and now I have to form an adjacency matric which indicates if 2 of my friends are friends. For example:
A and B are friends, A and C are friends, but B and C are not friends. This would look like this:
A B C
A 0 1 1
B 1 0 0
C 1 0 0
Because I have the list of id's and adjacencies already in python, I should also do the matrix in python, so if you have any ideas or a basic algorithm how to enter 1's and 0's I would appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我解决了这个问题,只需要2个for循环来遍历列表并比较用户id是否在邻接列表中,如果是这样,则将该条目设置为1,否则为0。
I solved the problem, it just required 2 for loops to go thorugh the list and compare whether the users id is in the adjacency list, if that is the case, make that entry 1, otherwise 0.
我认为这种结构最好以 图表 的形式实现。例如,查看 NetworkX。
无论如何,如果你确实需要矩阵,矩阵可以简单地实现为列表列表,如下所示:
但是如果你打算进行任何矩阵操作,你应该查看 numpy 库。
I think this structure is better implemented as a graph. For example, take a look at NetworkX.
Anyway, if you really need matrices, a matrix can simply be implement as a list of lists, like this:
But if you intend to do any matrix manipulation, you should check out the numpy library.