欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中的Networkx的基本使用

 更新時間:2023年02月14日 16:22:41   作者:酒釀小圓子~  
Networkx是一個Python的包,可以用來創(chuàng)建和處理復雜的圖網(wǎng)絡結構,這篇文章主要介紹了Python中的Networkx詳解,需要的朋友可以參考下

中文教程: https://www.osgeo.cn/networkx/install.html
英文教程: https://networkx.org/documentation/stable/install.html

1. 安裝Networkx

# 使用pip安裝
pip install networkx

# 使用conda安裝
conda install networkx

2. Networkx的基本使用

2.1 導入networkx

import networkx as nx

2.2 創(chuàng)建Graph

G = nx.Graph()          # 無向圖
G = nx.DiGraph()        # 有向圖
G = nx.MultiGraph()     # 多重無向圖
G = nx.MultiDigraph()   # 多重有向圖
G.clear()               # 清空圖

根據(jù)定義,Graph 是一組節(jié)點(頂點)和已識別的節(jié)點對(稱為邊、鏈接等)的集合。在NetworkX中,節(jié)點可以是任何 hashable 對象,例如文本字符串、圖像、XML對象、另一個圖形、自定義節(jié)點對象等。

2.3 給Graph添加邊

G.add_edge(1, 2)             # default edge data=1
G.add_edge(2, 3, weight=0.9) # specify edge data
# 如果是邊有許多的權,比如有長度和寬度的屬性,那么:
G.add_edge(n1, n2, length=2, width=3)
 
elist = [(1, 2), (2, 3), (1, 4), (4, 2)]
G.add_edges_from(elist)
elist = [('a', 'b', 5.0), ('b', 'c', 3.0), ('a', 'c', 1.0), ('c', 'd', 7.3)]
G.add_weighted_edges_from(elist)
 
# 如果給結點的名稱是其它符號,想離散化成從x開始的數(shù)字標記,那么:
G = nx.convert_node_labels_to_integers(G, first_label=x)

2.4 Graph基本信息獲取

nx.info(G) # 圖信息的概覽
G.number_of_nodes()
G.number_of_edges()
# 獲取和節(jié)點idx連接的邊的attr屬性之和
G.in_degree(idx, weight='attr')
 
# 如果想知道某個結點相連的某個邊權之和:
DG.degree(nodeIdx, weight='weightName')
 
# 獲取結點或者邊的屬性集合,返回的是元組的列表
G.nodes.data('attrName')
G.edges.data('attrName')
 
# 獲取n1 n2的邊的length權重,那么:
G[n1][n2]['length']
# 如果是有重邊的圖,選擇n1,n2第一條邊的length權重,則:
G[n1][n2][0]['length']
 
# 獲取n1結點的所有鄰居
nx.all_neighbors(G, n1)
 
# 判斷圖中n1到n2是否存在路徑
nx.has_path(G, n1, n2)
# 根據(jù)一個結點的list,獲取子圖
subG = nx.subgraph(G, nodeList)

2.5 Graph的繪制

# 最簡單的繪制
import matplotlib.pyplot as plt
nx.draw(G)
plt.show()
 
# 設置其他相關參數(shù)
nx.draw(G,
    with_labels=True,
    pos = nx.sprint_layout(G),
    node_color=color_list,
    edge_color='k',
    node_size=100,
    node_shape='o',
    linewidths=2,
    width=1.0,
    alpha=0.55,
    style='solid',
    font_size=9,
    font_color='k'
)

2.6 Graph的其他內置算法

# 最短路算法 返回最短路的路徑列表
nx.shortest_path(G, n1, n2, method='dijkstra')

# 以及各種圖的算法,比如流,割等等等等,大家可以看文檔探索下

3 其他

3.1 read_edgelist( )

Read a graph from a list of edges.
函數(shù)定義如下:

 read_edgelist(path, comments='#', delimiter=None, create_using=None, nodetype=None, data=True, edgetype=None, encoding='utf-8'):
    '''
    Read a graph from a list of edges.
    
    Parameters :	
    path : file or string
        File or filename to write. If a file is provided, it must be opened in ‘rb' mode. Filenames ending in .gz or .bz2 will be uncompressed.

    comments : string, optional
        The character used to indicate the start of a comment.
        
    delimiter : string, optional
        The string used to separate values. The default is whitespace.
        
    create_using : Graph container, optional,
        Use specified container to build graph. The default is networkx.Graph, an undirected graph.

    nodetype : int, float, str, Python type, optional
        Convert node data from strings to specified type

    data : bool or list of (label,type) tuples
        Tuples specifying dictionary key names and types for edge data

    edgetype : int, float, str, Python type, optional OBSOLETE
        Convert edge data from strings to specified type and use as ‘weight'

    encoding: string, optional
        Specify which encoding to use when reading file.

    Returns :	
    G : graph
        A networkx Graph or other type specified with create_using
    '''

樣例:

nx.write_edgelist(nx.path_graph(4), "test.edgelist")
G=nx.read_edgelist("test.edgelist")

fh=open("test.edgelist", 'rb')
G=nx.read_edgelist(fh)
fh.close()

G=nx.read_edgelist("test.edgelist", nodetype=int) G=nx.read_edgelist("test.edgelist",create_using=nx.DiGraph())

到此這篇關于Python中的Networkx詳解的文章就介紹到這了,更多相關Python中Networkx內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • pytest用例間參數(shù)傳遞的兩種實現(xiàn)方式示例

    pytest用例間參數(shù)傳遞的兩種實現(xiàn)方式示例

    pytest提供了許多運行命令以供定制化運行某一類測試用例或者某個測試用例等,下面這篇文章主要給大家介紹了關于pytest用例間參數(shù)傳遞的兩種實現(xiàn)方式,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-12-12
  • pytorch查看網(wǎng)絡參數(shù)顯存占用量等操作

    pytorch查看網(wǎng)絡參數(shù)顯存占用量等操作

    這篇文章主要介紹了pytorch查看網(wǎng)絡參數(shù)顯存占用量等操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • 巧妙使用Python裝飾器處理if...elif...else

    巧妙使用Python裝飾器處理if...elif...else

    大家好,今天在 Github 閱讀 EdgeDB[1] 的代碼,發(fā)現(xiàn)它在處理大量if…elif…else的時候,巧妙地使用了裝飾器,方法設計精巧,分享給大家一下,歡迎收藏學習,喜歡點贊支持
    2021-11-11
  • Python中pygame的mouse鼠標事件用法實例

    Python中pygame的mouse鼠標事件用法實例

    這篇文章主要介紹了Python中pygame的mouse鼠標事件用法,以完整實例形式詳細分析了pygame響應鼠標事件的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • PyQt5中QSpinBox計數(shù)器的實現(xiàn)

    PyQt5中QSpinBox計數(shù)器的實現(xiàn)

    這篇文章主要介紹了PyQt5中QSpinBox計數(shù)器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • 詳解如何使用python實現(xiàn)猜數(shù)字游戲

    詳解如何使用python實現(xiàn)猜數(shù)字游戲

    “猜數(shù)字”游戲是一款簡單而有趣的小游戲,玩家需要在給定的范圍內猜出一個由計算機隨機生成的數(shù)字,本文將使用Python語言來實現(xiàn)這款游戲,并詳細介紹其實現(xiàn)過程,文中有詳細的代碼示例供大家參考,需要的朋友可以參考下
    2024-04-04
  • python 基于opencv實現(xiàn)圖像增強

    python 基于opencv實現(xiàn)圖像增強

    這篇文章主要介紹了python 基于opencv實現(xiàn)圖像增強的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • 淺談Django學習migrate和makemigrations的差別

    淺談Django學習migrate和makemigrations的差別

    這篇文章主要介紹了淺談Django學習migrate和makemigrations的差別,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Python實現(xiàn)簡單多線程任務隊列

    Python實現(xiàn)簡單多線程任務隊列

    本文給大家介紹的是使用很簡單的代碼實現(xiàn)的多線程任務隊列,給大家一個思路,希望對大家學習python能夠有所幫助
    2016-02-02
  • Python中內建模塊collections如何使用

    Python中內建模塊collections如何使用

    在本篇內容里小編給大家整理的是關于Python中內建模塊collections的用法,有需要的朋友們可以參考下。
    2020-05-05

最新評論