pandas 如何在已经创建的星图中添加新的节点属性?

ghg1uchk  于 2023-06-28  发布在  其他
关注(0)|答案(1)|浏览(88)

我有一个图表

StellarGraph: Undirected multigraph
 Nodes: 4, Edges: 10

 Node types:
  default: [4]
    Features: float32 vector, length 2
    Edge types: default-default->default

 Edge types:
    default-default->default: [10]
        Weights: all 1 (default)
        Features: float32 vector, length 2
StellarGraph: Undirected multigraph
 Nodes: 4, Edges: 10

我想从数组new_feature=[2,3,5,6]中添加一个新的节点属性(即节点1为2,节点2为3,依此类推)。如何将其添加到已退出的stellargraph graph?
是否有像networkx中那样的直接函数?

0md85ypi

0md85ypi1#

我认为这是不推荐的,但你可以手动修改你的图表:
输入信息:

>>> print(G.info())

StellarGraph: Undirected multigraph
 Nodes: 4, Edges: 10

 Node types:
  default: [4]
    Features: float32 vector, length 2  # <- 2 features
    Edge types: default-default->default

 Edge types:
    default-default->default: [10]
        Weights: all 1 (default)
        Features: none

修改default类型:

new_feature = np.array([2, 3, 5, 6]).reshape(-1, 1)
G._nodes._features['default'] = np.hstack([G._nodes._features['default'], new_feature])

输出信息:

>>> print(G.info())

StellarGraph: Undirected multigraph
 Nodes: 4, Edges: 10

 Node types:
  default: [4]
    Features: float64 vector, length 3  # <- 3 features now
    Edge types: default-default->default

 Edge types:
    default-default->default: [10]
        Weights: all 1 (default)
        Features: none

>>> G.node_features()
array([[ 1.        , -0.2       ,  2.        ],
       [ 2.        ,  0.30000001,  3.        ],
       [ 3.        ,  0.        ,  5.        ],
       [ 4.        , -0.5       ,  6.        ]])

相关问题