简体   繁体   English

如何向 stellargraph 数据集添加新边?

[英]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 .它在内存方面要轻得多,因为pandasStellarGraph都比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.

相关问题 如何向数据集添加新列 - How to add a new column to a dataset 如何将带有(新节点,新边)的子图添加到 python 中的现有图 - how to add subgraph with (new nodes, new edges) to an existed graph in python Tensorflow 数据增强 - 如何将新图像添加到数据集中 - Tensorflow Data Augmentation - How to add new images into the dataset 如何在不更改索引的情况下向数据集添加新行 - How to make possible to add new row to a dataset without changing index 如何合并两个数据框并将列添加到新数据集中? - How to merge two dataframes and add columns to the new dataset? 如何在python的现有pickle文件中添加新数据集 - How to add new dataset in existing pickle file in python 如何使用 groupby 为 xarray 数据集添加新变量并应用? - How to add new variables for an xarray dataset using groupby and apply? 在具有条件的数据集中添加新列? - Add new column in the Dataset with condition? 向 HuggingFace 数据集添加新列 - Add new column to a HuggingFace dataset StellarGraph PaddedGraphGenerator - 如何提供特定的训练、验证和测试集 - StellarGraph PaddedGraphGenerator - how to provide specific training, validation and test sets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM