使用 NetworkX 和新的库,用于社交网络分析查询。通过查询,我的意思是通过两个边节点的属性选择/创建子图,其中边创建路径,节点包含属性。该图使用形式为 MultiDiGraph
G2 = nx.MultiDiGraph()
G2.add_node( "UserA", { "type" :"Cat" } )
G2.add_node( "UserB", { "type" :"Dog" } )
G2.add_node( "UserC", { "type" :"Mouse" } )
G2.add_node( "Likes", { "type" :"Feeling" } )
G2.add_node( "Hates", { "type" :"Feeling" } )
G2.add_edge( "UserA", 'Hates' , statementid="1" )
G2.add_edge( "Hates", 'UserB' , statementid="1" )
G2.add_edge( "UserC", 'Hates' , statementid="2" )
G2.add_edge( "Hates", 'UserA' , statementid="2" )
G2.add_edge( "UserB", 'Hates' , statementid="3" )
G2.add_edge( "Hates", 'UserA' , statementid="3" )
G2.add_edge( "UserC", 'Likes' , statementid="3" )
G2.add_edge( "Likes", 'UserB' , statementid="3" )
查询
for node,data in G2.nodes_iter(data=True):
if ( data['type'] == "Cat" ):
# get all edges out from these nodes
#then recursively follow using a filter for a specific statement_id
#or get all edges with a specific statement id
# look for with a node attribute of "cat"
有没有更好的查询方式?还是创建自定义迭代来创建子图的最佳实践?
或者(和一个单独的问题),可以简化图表,但我没有使用下图,因为“讨厌”类型的对象将有前身。这会使查询更简单吗?似乎更容易迭代节点
G3 = nx.MultiDiGraph()
G3.add_node( "UserA", { "type" :"Cat" } )
G3.add_node( "UserB", { "type" :"Dog" } )
G3.add_edge( "UserA", 'UserB' , statementid="1" , label="hates")
G3.add_edge( "UserA", 'UserB' , statementid="2" , label="hates")
其他注意事项:
- 也许
add_path
为创建的路径添加一个标识符? - iGraph 有一个很好的查询功能
g.vs.select()