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

Python中的Networkx的基本使用

 更新時(shí)間:2023年02月14日 16:22:41   作者:酒釀小圓子~  
Networkx是一個(gè)Python的包,可以用來(lái)創(chuàng)建和處理復(fù)雜的圖網(wǎng)絡(luò)結(jié)構(gòu),這篇文章主要介紹了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 導(dǎo)入networkx

import networkx as nx

2.2 創(chuàng)建Graph

G = nx.Graph()          # 無(wú)向圖
G = nx.DiGraph()        # 有向圖
G = nx.MultiGraph()     # 多重?zé)o向圖
G = nx.MultiDigraph()   # 多重有向圖
G.clear()               # 清空?qǐng)D

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

2.3 給Graph添加邊

G.add_edge(1, 2)             # default edge data=1
G.add_edge(2, 3, weight=0.9) # specify edge data
# 如果是邊有許多的權(quán),比如有長(zhǎng)度和寬度的屬性,那么:
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)
 
# 如果給結(jié)點(diǎn)的名稱是其它符號(hào),想離散化成從x開(kāi)始的數(shù)字標(biāo)記,那么:
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é)點(diǎn)idx連接的邊的attr屬性之和
G.in_degree(idx, weight='attr')
 
# 如果想知道某個(gè)結(jié)點(diǎn)相連的某個(gè)邊權(quán)之和:
DG.degree(nodeIdx, weight='weightName')
 
# 獲取結(jié)點(diǎn)或者邊的屬性集合,返回的是元組的列表
G.nodes.data('attrName')
G.edges.data('attrName')
 
# 獲取n1 n2的邊的length權(quán)重,那么:
G[n1][n2]['length']
# 如果是有重邊的圖,選擇n1,n2第一條邊的length權(quán)重,則:
G[n1][n2][0]['length']
 
# 獲取n1結(jié)點(diǎn)的所有鄰居
nx.all_neighbors(G, n1)
 
# 判斷圖中n1到n2是否存在路徑
nx.has_path(G, n1, n2)
# 根據(jù)一個(gè)結(jié)點(diǎn)的list,獲取子圖
subG = nx.subgraph(G, nodeList)

2.5 Graph的繪制

# 最簡(jiǎn)單的繪制
import matplotlib.pyplot as plt
nx.draw(G)
plt.show()
 
# 設(shè)置其他相關(guān)參數(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的其他內(nèi)置算法

# 最短路算法 返回最短路的路徑列表
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())

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

相關(guān)文章

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

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

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

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

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

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

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

    Python中pygame的mouse鼠標(biāo)事件用法實(shí)例

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

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

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

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

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

    python 基于opencv實(shí)現(xiàn)圖像增強(qiáng)

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

    淺談Django學(xué)習(xí)migrate和makemigrations的差別

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

    Python實(shí)現(xiàn)簡(jiǎn)單多線程任務(wù)隊(duì)列

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

    Python中內(nèi)建模塊collections如何使用

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

最新評(píng)論