使用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"
有没有更好的方法来查询? 或者是创建自定义的迭代创建子图的最佳实践?
可替换地(和一个单独的问题)时,图形可被简化,但我不使用从下面的图,因为“讨厌”类型的对象将具有predcessors。 这会使得查询更简单? 似乎更容易遍历节点
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()
建立在@ ARIC的答案 ,你可以找到红色的鱼是这样的:
red_fish = set(n for u,v,d in G.edges_iter(data=True)
if d['color']=='red'
for n in (u, v)
if G.node[n]['label']=='fish')
print(red_fish)
# set([2])
这是非常简单的写一行代码,使各节点列表或发电机一个特定的属性(如图所示发电机)
import networkx as nx
G = nx.Graph()
G.add_node(1, label='one')
G.add_node(2, label='fish')
G.add_node(3, label='two')
G.add_node(4, label='fish')
# method 1
fish = (n for n in G if G.node[n]['label']=='fish')
# method 2
fish2 = (n for n,d in G.nodes(data=True) if d['label']=='fish')
print(list(fish))
print(list(fish2))
G.add_edge(1,2,color='red')
G.add_edge(2,3,color='blue')
red = ((u,v) for u,v,d in G.edges(data=True) if d['color']=='red')
print(list(red))
如果你的图形很大,固定和你想要做快速查找你可以做这样的属性的“反向词典”
labels = {}
for n, d in G.nodes(data=True):
l = d['label']
labels[l] = labels.get(l, [])
labels[l].append(n)
print labels
为了选择基于边沿和节点属性的边缘,你可能想要做这样的事情,使用您的图形,G2:
def select(G2, query):
'''Call the query for each edge, return list of matches'''
result = []
for u,v,d in G2.edges(data=True):
if query(u,v,d):
result.append([(u,v)])
return result
# Example query functions
# Each assumes that it receives two nodes (u,v) and
# the data (d) for an edge
def dog_feeling(u, v, d):
return (d['statementid'] == "3"
and G2.node[u]['type'] == "Dog"
or G2.node[u]['type'] == "Dog")
def any_feeling(u,v,d):
return (d['statementid'] == "3"
and G2.node[u]['type'] == "Feeling"
or G2.node[u]['type'] == "Feeling")
def cat_feeling(u,v,d):
return (G2.node[u]['type'] == "Cat"
or G2.node[v]['type'] == "Cat")
# Using the queries
print select(G2, query = dog_feeling)
print select(G2, query = any_feeling)
print select(G2, query = cat_feeling)
这种抽象了迭代过程到select()
函数,你可以写你的查询作为单独的,可测试的功能。