[英]How to add new edges to the stellargraph dataset?
I need to add some extra edges to Cora dataset using stellargraph.我需要使用 stellargraph 向 Cora 数据集添加一些额外的边。 Is there ane way to add edges to the current dataset in stellargraph library?有没有办法将边添加到 stellargraph 库中的当前数据集?
import stellargraph as sg
dataset = sg.datasets.Cora()
For example in NetworkX, we can add some edges to the existing graph using add_edges_from(edgelist)
.例如在 NetworkX 中,我们可以使用add_edges_from(edgelist)
向现有图形添加一些边。
I recently ran into a similar scenario and converting back-and-forth to networkx
format was not possible.我最近遇到了类似的情况,无法来回转换为networkx
格式。 Specifically, since StellarGraph
is supposed to be capable of storing graphs much larger than networkx
, there will be a point at which converting would not be possible.具体来说,由于StellarGraph
应该能够存储比networkx
大得多的图,因此会有一个点无法进行转换。
To get around this, I used the numpy
loading capabilities of StellarGraph
1.2.1 .为了解决这个问题,我使用了StellarGraph
1.2.1 的numpy
加载功能。
With StellarGraph
, you can dump the edge array with edge_arrays()
into pandas
, then concatenate any desired edges onto that.使用StellarGraph
,您可以使用edge_arrays()
将边数组转储到pandas
中,然后将任何需要的边连接到它上面。 It is much lighter memory-wise, since pandas
and StellarGraph
both perform better than networkx
.它在内存方面要轻得多,因为pandas
和StellarGraph
都比networkx
表现更好。
Here is a short example:这是一个简短的例子:
import pandas as pd
from stellargraph import IndexedArray, StellarGraph
#### original data / graph
nodes = IndexedArray(index=['a', 'b', 'c', 'd'])
original_edges = pd.DataFrame(
{
'source' : [0, 1, 2, 3, 0],
'target' : [1, 2, 3, 0, 2]
}
)
original_graph = StellarGraph(
nodes,
original_edges
)
#### new data
new_edges = pd.DataFrame(
{
'source' : [3, 3],
'target' : [1, 2]
}
)
#### new graph
new_graph = StellarGraph(
nodes,
pd.concat(
[
original_edges,
new_edges
],
ignore_index=True
)
)
You can't do it directly from stellargraph since version 0.9.从 0.9 版开始,您不能直接从 stellargraph 执行此操作。 You'll have to use .to.networkx()
to convert it back to.networkX format, add you edges and then convert it back to stellargraph.您必须使用.to.networkx()
将其转换回 .networkX 格式,添加边,然后将其转换回 stellargraph。
from stellargraph import StellarGraph
import networkx as nx
g = StellarGraph.to_networkx(dataset)
g.add_edges_from(edgelist)
new_dataset = StellarGraph.from_networkx(g)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.