python networkx 已創建的圖怎麼查看它的所有邊

>>>G=nx.Graph()#orDiGraph,MultiGraph,MultiDiGraph,etc
>>>G.add_path([0,1,2])
>>>G.add_edge(2,3,weight=5)
>>>G.edges()
[(0,1),(1,2),(2,3)]
>>>G.edges(data=True)#defaultedgedatais{}(emptydictionary)
[(0,1,{}),(1,2,{}),(2,3,{'weight':5})]
>>>list(G.edges_iter(data='weight',default=1))
[(0,1,1),(1,2,1),(2,3,5)]
>>>G.edges([0,3])
[(0,1),(3,2)]
>>>G.edges(0)
[(0,1)]

㈡ 如何在Windows操作系統下安裝Python和Networkx

Networkx是一套基於Python的多種網路構造庫。因為之前沒有學過Python,因此一點點上手,這一篇講一講如何在Windows環境下安裝Python2.7和Networkx。

首先要澄清一下,如果是想深入系統學習Python的同學,還是盡早換Linux系統,因為Windows底下的庫安裝非常麻煩;而Linux底下只需要運行命令行(Terminal):
sudo apt-get install python-matplotlib

就可以了。

由於僅僅是使用Networkx構造數據的關系,以下簡單說明如何在Windows底下快速地安裝和使用Python2.7。

0. 先留個記號:Python的初學者指南
https://wiki.python.org/moin/BeginnersGuide

1. 下載Python 2.7,雙擊安裝
https://www.python.org/downloads/windows/
添加路徑變數:在<開始>菜單 - Control Panel - System and Security - System - Advanced System Settings - (Advanced Tab) - Environmental Variables - 找到『Path』,雙擊打開 - 添加路徑『C:\Python27』(系統安裝文件夾),一路確定。

確認安裝:在<開始>菜單 - 運行cmd - 進入命令行,輸入『python』,顯示
Python 2.7.8 (default, Jun 30 2014, 16:08:48)
即安裝成功。
(輸入exit()退出Python)

2. 安裝networkx之前,需要下載並安裝setuptools,下載地址:
https://pypi.python.org/pypi/setuptools
放到Python27的文件夾下,雙擊自動安裝。

3. 下載networkx,解壓文件夾,復制到Python27的文件夾下:
https://pypi.python.org/pypi/networkx/
在cmd窗口命令行下進入networkx的文件夾,輸入『python setup.py install』 安裝networkx庫。
安裝程序完成。

4. 測試程序:
在cmd任何路徑下進入python,使用小測試程序確認安裝成功!
http://networkx.github.io/examples.html
程序如下:
>>> import networkx as nx
>>> G=nx.Graph()
>>> G.add_node("spam")
>>> G.add_edge(1,2)
>>> print(G.nodes())
[1, 2, 'spam']
>>> print(G.edges())
[(1, 2)]

5. 附networkx的Tutorial:
https://networkx.github.io/documentation/latest/overview.html
networkx網路生成函數:
http://networkx.lanl.gov/reference/generators.html#mole-networkx.generators.random_graphs

㈢ python networkx報錯AttributeError:

你把代碼發上來看看,下面一個例子


importnetworkxasnx

G=nx.Graph()#建立一個空的無向圖G
G.add_node(1)#添加一個節點1
G.add_edge(2,3)#添加一條邊2-3(隱含著添加了兩個節點2、3)
G.add_edge(3,2)#對於無向圖,邊3-2與邊2-3被認為是一條邊
print"nodes:",G.nodes()#輸出全部的節點:[1,2,3]
print"edges:",G.edges()#輸出全部的邊:[(2,3)]
print"numberofedges:",G.number_of_edges()#輸出邊的數量:1

㈣ python的networkx怎麼給邊加註釋

應該是不可以的
#!/usr/bin/env python
"""
Draw a graph with matplotlib.
You must have matplotlib for this to work.
"""
__author__ = """Aric Hagberg ([email protected])"""
try:
import matplotlib.pyplot as plt
except:
raise
import networkx as nx
G=nx.house_graph()
# explicitly set positions
pos={0:(0,0),
1:(1,0),
2:(0,1),
3:(1,1),
4:(0.5,2.0)}
nx.draw_networkx_nodes(G,pos,node_size=2000,nodelist=[4])
nx.draw_networkx_nodes(G,pos,node_size=3000,nodelist=[0,1,2,3],node_color='b')
nx.draw_networkx_edges(G,pos,alpha=0.5,width=6)
plt.axis('off')
plt.savefig("house_with_colors.png") # save as png
plt.show() # display

㈤ python networkx模塊裡面計算最短路徑時,如何處理等價路徑我怎麼測試只能顯示1條路徑,請大神賜教。

if source is None: if target is None: ## Find paths between all pairs. if weight is None: paths=nx.all_pairs_shortest_path(G) else: paths=nx.all_pairs_dijkstra_path(G,weight=weight) else: ## Find paths from all nodes co-accessible to the target. directed = G.is_directed() if directed: G.reverse(=False) if weight is None: paths=nx.single_source_shortest_path(G,target) else: paths=nx.single_source_dijkstra_path(G,target,weight=weight) # Now flip the paths so they go from a source to the target. for target in paths: paths[target] = list(reversed(paths[target])) if directed: G.reverse(=False) else: if target is None: ## Find paths to all nodes accessible from the source. if weight is None: paths=nx.single_source_shortest_path(G,source) else: paths=nx.single_source_dijkstra_path(G,source,weight=weight) else: ## Find shortest source-target path. if weight is None: paths=nx.bidirectional_shortest_path(G,source,target) else: paths=nx.dijkstra_path(G,source,target,weight)

㈥ python networkx 程序運行

有這句了嗎
import matplotlib.pyplot as plt
因為從代碼上看不到plt的的相關說明

㈦ python networkx 都可以干什麼

1 import networkx
2 _of_edges()
3 #建圖
4 G = networkx.Graph()
5 #節點數:
6 len(G)
7 #邊數
8 G.number_of_edges()
9 #節點表
10 G.nodes()
11 #邊表
12 G.edges()
13 #網路直徑
14 diameter(G)
15 #所有節點間的最短*路徑*,列表存儲
16 networkx.all_pairs_shortest_path(G)

㈧ 怎樣基於python networkx實現社區發現

k_clique_communities的input是G,networkx的graph的數據結構。 所以原鏈接的test.txt文件應該是包涵一個graph的文件。

networkx可以讀取的graph文件種類如鏈接所示。Reading and writing graphs

常見的類型有edgelist (usually stored as a text file)和GML。如果我們用Network data 的dolphins social network (which is stored as a GML file)做例子的話,運行如下的code:

import networkx as nx import matplotlib.pyplot as plt G = nx.read_gml('dolphins.gml')klist = list(nx.k_clique_communities(G,3)) #list of k-cliques in the network. each element contains the nodes that consist the clique.#plottingpos = nx.spring_layout(G)plt.clf()nx.draw(G,pos = pos, with_labels=False)nx.draw(G,pos = pos, nodelist = klist[0], node_color = 'b')nx.draw(G,pos = pos, nodelist = klist[1], node_color = 'y')plt.show()

我們的到如下結果:


&lt;img src="https://pic3.mg.com/50/v2-_hd.png" data-rawwidth="800" data-rawheight="600" class="origin_image zh-lightbox-thumb" width="800" data-original="https://pic3.mg.com/v2-_r.png"&gt;

which gives us four clique communities.

㈨ 使用Python調用networkx時出現這個問題怎麼解決

這是基礎都還沒學就開始人工智慧 神經網路了嗎

㈩ python networkx 使用import community 報錯沒有community這個模塊

from networkx.algorithms import community