相关文章推荐
没读研的足球  ·  SciPy ...·  1 月前    · 
强健的冰淇淋  ·  Windows日志取证 - ...·  2 年前    · 

学过线性代数的都了解矩阵,在矩阵上的文章可做的很多,什么特征矩阵,单位矩阵等.grpah存储可以使用矩阵,比如graph的 邻接矩阵 , 权重矩阵 等,这节主要是在等到graph后,如何快速得到这些信息.详细官方文档在 这里

  • 12.graph和其他数据格式转换
  • 12.1graph与字典(Dict)
  • 12.2graph与列表(List)
  • 12.3graph与numpy
  • 12.4graph与Scipy
  • 12.5graph与Pandas
  • 12.1graph与字典(Dict)

    1. #从字典生成图
    2. dod = { 0 : { 1 : { 'weight' : 1 }}}
    3. G = nx.from_dict_of_dicts(dod) #或G=nx.Graph(dpl)
    4. plt.subplots( 1 , 1 ,figsize=( 6 , 3 ))
    5. nx.draw(G, with_labels= True , font_weight= 'bold' )
    6. plt.axis( 'on' )
    7. plt.xticks([])
    8. plt.yticks([])
    9. plt.show()
    10. #图转换为字典
    11. print(nx.to_dict_of_dicts(G))

    graph与字典(Dict)示例
    graph与字典(Dict)示例

    {0: {1: {'weight': 1}}, 1: {0: {'weight': 1}}}

    12.2graph与列表(List)

    1. #从列表中创建graph
    2. dol = { 0 : [ 1 , 2 , 3 ]}
    3. edgelist = [( 0 , 1 ),( 0 , 3 ),( 2 , 3 )]
    4. G1 = nx.from_dict_of_lists(dol) #或G=nx.Graph(dol)
    5. G2=nx.from_edgelist(edgelist)
    6. #显示graph
    7. plt.subplots( 1 , 2 ,figsize=( 15 , 3 ))
    8. plt.subplot( 121 )
    9. nx.draw(G1, with_labels= True , font_weight= 'bold' )
    10. plt.axis( 'on' )
    11. plt.xticks([])
    12. plt.yticks([])
    13. plt.subplot( 122 )
    14. nx.draw(G2, with_labels= True , font_weight= 'bold' )
    15. plt.axis( 'on' )
    16. plt.xticks([])
    17. plt.yticks([])
    18. plt.show()
    19. #graph转list
    20. print(nx.to_dict_of_lists(G1))
    21. print(nx.to_edgelist(G1))

    graph与列表(List)示例
    graph与列表(List)示例

    {0: [1, 2, 3], 1: [0], 2: [0], 3: [0]}
    [(0, 1, {}), (0, 2, {}), (0, 3, {})]

    12.3graph与numpy

    1. #从numpy创建graph
    2. import numpy as np
    3. a = np.reshape(np.random.random_integers( 0 , 1 , size= 100 ), ( 10 , 10 ))
    4. D = nx.DiGraph(a)
    5. nx.draw(D, with_labels= True , font_weight= 'bold' )
    6. plt.axis( 'on' )
    7. plt.xticks([])
    8. plt.yticks([])
    9. plt.show()
    10. #graph返回numpy
    11. G=nx.Graph()
    12. G.add_edge( 1 , 2 , weight= 7.0 , cost= 5 )
    13. A1 = nx.to_numpy_matrix(G)
    14. A2 = nx.to_numpy_recarray(G, dtype=[( 'weight' , float), ( 'cost' , int)])
    15. print(A1,A2)

    graph与numpy示例
    graph与numpy示例

    1. [[0. 7.]
    2. [7. 0.]] [[(0., 0) (7., 5)]
    3. [(7., 5) (0., 0)]]

    12.4graph与Scipy

    1. #从scipy创建graph
    2. G.clear()
    3. import scipy as sp
    4. A = sp.sparse.eye( 2 , 2 , 1 )
    5. G = nx.from_scipy_sparse_matrix(A)
    6. nx.draw(D, with_labels= True , font_weight= 'bold' )
    7. plt.axis( 'on' )
    8. plt.xticks([])
    9. plt.yticks([])
    10. plt.show()
    11. #graph返回scipy
    12. A = nx.to_scipy_sparse_matrix(G)
    13. print(A.todense())

    graph与Scipy示例
    graph与Scipy示例

    1. [[0. 1.]
    2. [1. 0.]]

    12.5graph与Pandas

    1. #从pandas创建graph
    2. G.clear()
    3. import pandas as pd
    4. df = pd.DataFrame([[ 1 , 1 ], [ 2 , 1 ]])
    5. G = nx.from_pandas_adjacency(df)
    6. nx.draw(D, with_labels= True , font_weight= 'bold' )
    7. plt.axis( 'on' )
    8. plt.xticks([])
    9. plt.yticks([])
    10. plt.show()
    11. #graph返回scipy
    12. df = nx.to_pandas_adjacency(G)
    13. print(df)

    graph与Pandas示例
    graph与Pandas示例

    1. 0 1
    2. 0 1.0 2.0