简体   繁体   English

Python朋友网络可视化

[英]Python friends network visualization

I have hundreds of lists (each list corresponds to 1 person). 我有数百个列表(每个列表对应1个人)。 Each list contains 100 strings, which are the 100 friends of that person. 每个列表包含100个字符串,这是该人的100个朋友。

I want to 3D visualize this people network based on the number of common friends they have. 我想根据他们拥有的普通朋友的数量,对这个人网络进行3D可视化。 Considering any 2 lists, the more same strings they have, the closer they should appear together in this 3D graph. 考虑到任何2个列表,它们具有的字符串越多,它们在这个3D图形中应该越接近一起出现。 I wanted to show each list as a dot on the 3D graph without nodes/connections between the dots. 我想将每个列表显示为3D图形上的点,而点之间没有节点/连接。

For brevity, I have included only 3 people here. 为简洁起见,我在这里只包括3个人。

person1 = ['mike', 'alex', 'arker','locke','dave','david','ross','rachel','anna','ann','darl','carl','karle']

person2 = ['mika', 'adlex', 'parker','ocke','ave','david','rosse','rachel','anna','ann','darla','carla','karle']

person3 = ['mika', 'alex', 'parker','ocke','ave','david','rosse','ross','anna','ann','darla','carla','karle', 'sasha', 'daria']

Gephi Setup steps: Gephi设置步骤:

  • Install Gephi and then start it 安装Gephi然后启动它
  • You probably want to upgrade all the plugins now, see the button in the lower right corner. 您可能想立即升级所有插件,请参阅右下角的按钮。
  • Now create a new project. 现在创建一个新项目。
  • Make sure the current workspace is Workspace1 确保当前工作空间是Workspace1
  • Enable Graph Streaming plugin 启用Graph Streaming插件
  • In Streaming tab that then appears configure server to use http and port 8080 然后在Streaming选项卡中显示configure server以使用http和端口8080
  • start the server (it will then have a green dot underneath it instead of a red dot). 启动服务器(然后它下面会有一个绿点而不是一个红点)。

Python steps: Python步骤:

  • install gephistreamer package ( pip install gephistreamer ) 安装gephistreamer包( pip install gephistreamer

Copy the following python cod to something like friends.py : 将以下python cod复制到friends.py东西:

from gephistreamer import graph
from gephistreamer import streamer
import random as rn

stream = streamer.Streamer(streamer.GephiWS(hostname="localhost",port=8080,workspace="workspace1"))

szfak = 100  # this scales up everything - somehow it is needed
cdfak = 3000

nodedict = {}
def addfnode(fname):
  # grab the node out of the dictionary if it is there, otherwise make a newone
  if (fname in nodedict):
    nnode = nodedict[fname]
  else:
    nnode = graph.Node(fname,size=szfak,x=cdfak*rn.random(),y=cdfak*rn.random(),color="#8080ff",type="f")
    nodedict[fname] = nnode # new node into the dictionary
  return nnode

def addnodes(pname,fnodenamelist):
  pnode = graph.Node(pname,size=szfak,x=cdfak*rn.random(),y=cdfak*rn.random(),color="#ff8080",type="p")
  stream.add_node(pnode)
  for fname in fnodenamelist:
    print(pname+"-"+fname)
    fnode = addfnode(fname)
    stream.add_node(fnode)
    pfedge = graph.Edge(pnode,fnode,weight=rn.random())
    stream.add_edge(pfedge)

person1friends = ['mike','alex','arker','locke','dave','david','ross','rachel','anna','ann','darl','carl','karle']
person2friends = ['mika','adlex','parker','ocke','ave','david','rosse','rachel','anna','ann','darla','carla','karle']
person3friends = ['mika','alex','parker','ocke','ave','david','rosse','ross','anna','ann','darla','carla','karle','sasha','daria']

addnodes("p1",person1friends)
addnodes("p2",person2friends)
addnodes("p3",person3friends)

Run it with the command python friends.py You should see all the nodes appear. 使用命令python friends.py运行它您应该看到所有节点都出现。 There are then lots of ways you can lay it out to make it look better, I am using the Force Atlas layouter here and you can see the parameters I am using on the left. 有很多方法可以让它看起来更好看,我在这里使用Force Atlas layouter,你可以看到我在左边使用的参数。

在此输入图像描述

Some notes: 一些说明:

  • you can get the labels to show or disappear by clicking on the T on the bottom status/control bar. 您可以通过单击底部状态/控制栏上的T来显示或消失标签。
  • View the data in the nodes and edges by opening Window/Data Table . 通过打开“ Window/Data Table查看节点和边缘中的Window/Data Table
  • It is a very rich program, there are more options than you can shake a stick at. 这是一个非常丰富的计划,有比你可以动摇的选择更多的选择。
  • You can set more properties on your nodes and edges in the python code and then they will show up in the data table view and can be used to filter, etc. 您可以在python代码中的节点和边缘上设置更多属性,然后它们将显示在数据表视图中,并可用于过滤等。
  • You want to pay attention to that update button in the bottom right corner of Gephi, there are a lot of bugs to fix. 你想要关注Gephi右下角的更新按钮,还有很多bug需要修复。

This will get you started (as you asked), but for your particular problem: 这将使您开始(如您所知),但针对您的特定问题:

  • you will also need to calculate weights for your persons (the "p" nodes), and link them to each other with those weights 您还需要为您的人员(“p”节点)计算权重,并使用这些权重将它们相互链接
  • Then you need to find a layouter and paramters that positions those nodes the way you want them based on the new weights. 然后你需要找到一个layouter和paramters,根据新的权重,按照你想要的方式定位这些节点。
  • So you don't really need to show the type="f" nodes, you need just the "p" nodes. 所以你真的不需要显示type =“f”节点,你只需要“p”节点。
  • The weight between to "p" nodes should be based on the intersection of the sets of the friend names. “p”节点之间的权重应该基于朋友姓名集的交集。
  • There are also Gephi plugins that can then display this in 3D, but that is actually a completely separate issue, you probably want to get it working in 2D first. 还有一些Gephi插件可以在3D中显示,但这实际上是一个完全独立的问题,你可能希望首先使用2D工作。

This is running on Windows 10 using Anaconda 4.4.1 and Python 3.5.2 and Gephi 0.9.1 . 这是在Windows 10上使用Anaconda 4.4.1Python 3.5.2以及Gephi 0.9.1运行的

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

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