Pypark图的真子图

2024-05-14 07:43:06 发布

您现在位置:Python中文网/ 问答频道 /正文

graphframes是基于PySpark数据帧的网络分析工具。以下代码是教程子图示例的修改版本:

from graphframes.examples import Graphs
import graphframes
g = Graphs(sqlContext).friends()  # Get example graph
# Select subgraph of users older than 30
v2 = g.vertices.filter("age > 30")
g2 = graphframes.GraphFrame(v2, g.edges)

与原始图g2相比,新的图g2将包含更少的节点和更少的边。 然而,情况并非如此:

^{pr2}$

给出输出:

(6, 7)
(7, 4)

很明显,结果图包含不存在节点的边。 更令人不安的是g.degrees和{}是相同的。这意味着至少有些图形功能会忽略节点信息。有没有一个好方法可以确保GraphFrame创建 只使用提供的nodesedges参数的交集的图?在


Tags: 工具数据代码import示例节点教程网络分析
2条回答

我用来给一个graphframe子图的方法是使用motif:

motifs = g.find("(a)-[e]->(b)").filter(<conditions for a,b or e>)
new_vertices = sqlContext.createDataFrame(motifs.map(lambda row: row.a).union(motifs.map(lambda row: row.b)).distinct())
new_edges = sqlContext.createDataFrame(motifs.map(lambda row:row.e).distinct())
new_graph = GraphFrame(new_vertices,new_edges)

虽然这看起来更复杂,可能需要更长的运行时间,但对于更复杂的图形查询,这很适合作为单个实体而不是作为单独的顶点和边与graphframe交互。因此,对顶点进行过滤也会影响graphframe中左侧的边。在

有意思。。我看不出结果:

>>> from graphframes.examples import Graphs
>>> import graphframes
>>> g = Graphs(sqlContext).friends()  # Get example graph
>>> # Select subgraph of users older than 30
... v2 = g.vertices.filter("age > 30")
>>> g2 = graphframes.GraphFrame(v2, g.edges)
>>> print(g.vertices.count(), g.edges.count())
(6, 7)
>>> print(g2.vertices.count(), g2.edges.count())
(4, 7)

到现在为止,GraphFrames不检查图是否有效-也就是说,在图形构建时,所有的边都连接到顶点等等。但是在过滤器之后顶点的数量似乎是正确的?在

相关问题 更多 >

    热门问题