简体   繁体   English

如何在Python中从Facebook创建相互友谊的邻接矩阵

[英]How to create an adjacency matrix of mutual friendships from facebook in python

I am doing a project on Social Network Analysis of Facebook Network. 我正在做一个有关Facebook网络的社交网络分析的项目。 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. 例如:A和B是朋友,A和C是朋友,但B和C不是朋友。 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. 因为我已经在python中获得了ID和邻接列表,所以我也应该在python中进行矩阵运算,因此,如果您有任何想法或基本算法如何输入1和0,我将不胜感激。

我解决了这个问题,只需要2个for循环就可以进入列表并比较用户ID是否在邻接列表中,如果是这种情况,则将该条目设为1,否则设为0。

I think this structure is better implemented as a graph . 我认为最好将这种结构实现为图形 For example, take a look at NetworkX . 例如,看一下NetworkX

Anyway, if you really need matrices, a matrix can simply be implement as a list of lists, like this: 无论如何,如果您确实需要矩阵,则可以将矩阵简单地实现为列表列表,如下所示:

m = [[0, 1, 1],
     [1, 0, 0],
     [1, 0, 0],]

But if you intend to do any matrix manipulation, you should check out the numpy library. 但是,如果您打算进行任何矩阵操作,则应签出numpy库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM