NetworkX系列教程(11)-graph和其他數據格式轉換


學過線性代數的都了解矩陣,在矩陣上的文章可做的很多,什么特征矩陣,單位矩陣等.grpah存儲可以使用矩陣,比如graph的鄰接矩陣,權重矩陣等,這節主要是在等到graph后,如何快速得到這些信息.詳細官方文檔在這里

目錄:


注意:如果代碼出現找不庫,請返回第一個教程,把庫文件導入.

12.graph和其他數據格式轉換

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. #圖轉換為字典 
  12. 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.  
  5. G1 = nx.from_dict_of_lists(dol) #或G=nx.Graph(dol) 
  6. G2=nx.from_edgelist(edgelist) 
  7.  
  8. #顯示graph 
  9. plt.subplots(1,2,figsize=(15,3)) 
  10. plt.subplot(121
  11. nx.draw(G1, with_labels=True, font_weight='bold'
  12. plt.axis('on'
  13. plt.xticks([]) 
  14. plt.yticks([]) 
  15. plt.subplot(122
  16. nx.draw(G2, with_labels=True, font_weight='bold'
  17. plt.axis('on'
  18. plt.xticks([]) 
  19. plt.yticks([]) 
  20. plt.show() 
  21.  
  22. #graph轉list 
  23. print(nx.to_dict_of_lists(G1)) 
  24. 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.  
  11. #graph返回numpy 
  12. G=nx.Graph() 
  13. G.add_edge(1, 2, weight=7.0, cost=5
  14. A1 = nx.to_numpy_matrix(G) 
  15. A2 = nx.to_numpy_recarray(G, dtype=[('weight', float), ('cost', int)]) 
  16. 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.  
  12. #graph返回scipy 
  13. A = nx.to_scipy_sparse_matrix(G) 
  14. 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.  
  12. #graph返回scipy 
  13. df = nx.to_pandas_adjacency(G) 
  14. print(df) 

graph與Pandas示例
graph與Pandas示例

輸出:

  1. 0 1 
  2. 0 1.0 2.0 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM