675人阅读 评论(0)

# networkx学习笔记

## 创建一个图graph

import networkx as nx
G = nx.Graph()

## 节点Nodes

G.add_node(1)

G.add_nodes_from([2, 3])

## 边Edges

G.add_edge(1, 2)
e = (2, 3)
G.add_edge(*e)

G.add_edges_from([(1, 2), (2, 3)])

G.clear()

G.add_edges_from([(1,2), (1, 3)])
G.add_node(1)
G.add_edge(1, 2)
G.add_node("spam")
G.add_nodes_from("spam") # adds 4 nodes: 's', 'p', 'a', 'm'

G.number_of_nodes()
8

G.number_of_edges()
2

G.nodes()
[1, 2, 3, 's', 'm', 'a', 'p', 'spam']

G.edges()
[(1, 2), (1, 3)]

G.neighbors(1)
[2, 3]


G.remove_nodes_from("spam")
G.nodes()
[1, 2, 3, 'spam']

G.remove_edge(1, 3)
G.edges()
[(1, 2)]


# What to use as nodes and edges

G[1]
{2: {}, 3: {'color': 'blue'}}

G[1][2]
{}


G.add_edge(1, 3)
G[1][3]['color']='blue'
G[1][2]['weight']='2'
G[1] # information about node 1
{2: {'weight': '2'}, 3: {'color': 'blue'}}

G[2] # information about node 2
{1: {'weight': '2'}}


FG = nx.Graph()
FG.add_weighted_edges_from([(1, 2, 0.125),(1, 3, 0.75),(2, 3, 1.2),(3, 4, 0.275)])
for n,nbrs in FG.adjacency_iter():
for nbr, eattr in nbrs.items():
data = eattr['weight']
if data < 0.5:
print('(%d, %d, %.3f)' % (n, nbr, data))
(1, 2, 0.125)
(2, 1, 0.125)
(3, 4, 0.275)
(4, 3, 0.275)


## 添加图，节点以及边的属性

### 图的属性

G = nx.Graph(day="friday")
G.graph
{'day': 'friday'}


G.graph['day']='Monday'
G.graph
{'day': 'Monday'}


### 节点的属性

G.add_node(1, time='5pm')
G.add_nodes_from([3], time='2pm')
G.node[1]
{'time': '5pm'}

G.node[3]
{'time': '2pm'}

G.node[1]['room'] = 714
G.node[1]
{'room': 714, 'time': '5pm'}

G.nodes(data=True)
[(1, {'room': 714, 'time': '5pm'}), (3, {'time': '2pm'})]

G.node[1]['room']=233 # 修改节点的属性
G.node[1]
{'room': 233, 'time': '5pm'}


### 边的属性

G.add_edge(1, 2, weight=3.5)
G.add_edges_from([(3, 4), (4, 5)], color='red')
G.add_edges_from([(1, 2, {'color':'blue'}), (2, 3, {'weight':8})])
G.edge[1][2]['weight'] = 4.7
G.edge[1][2]['label'] = 'source'
G[1][2]
{'color': 'blue', 'label': 'source', 'weight': 4.7}


## 图生成器和图操作

er=nx.erdos_renyi_graph(100,0.15)
ws=nx.watts_strogatz_graph(30,3,0.1)
ba=nx.barabasi_albert_graph(100,5)
red=nx.random_lobster(100,0.9,0.9)

## 统计分析

G = nx.Graph()
G.add_edges_from([(1, 2), (1, 3)])
G.add_node('spam') # add a node 'spam'
 sorted(nx.degree(G).values())
[0, 1, 1, 2]

nx.clustering(G)
{1: 0.0, 2: 0.0, 3: 0.0, 'spam': 0.0}

 nx.degree(G)
{1: 2, 2: 1, 3: 1, 'spam': 0}

nx.degree(G, 1)
2

G.degree(1)
2

G.degree([1, 2])
{1: 2, 2: 1}


## 绘制

import matplotlib.pyplot as plt
ba=nx.barabasi_albert_graph(30,3)
nx.draw(ba,pos=nx.fruchterman_reingold_layout(ba))
plt.show()

### 例子

import matplotlib.pyplot as plt
import networkx as nx

G=nx.barabasi_albert_graph(30,3)
plt.figure(1)
nx.draw(G, pos=nx.shell_layout(G), node_color='y')

pr=nx.pagerank(G,alpha=0.85)

plt.figure(2)
nx.draw(G, pos=nx.shell_layout(G), node_size=[x * 18000 for x in pr.values()],node_color='r',with_labels=True)
plt.show()

#### networkx网站地址

http://networkx.readthedocs.io/en/networkx-1.11/overview.html

0
0

个人资料
• 访问：219607次
• 积分：3189
• 等级：
• 排名：第12760名
• 原创：95篇
• 转载：0篇
• 译文：0篇
• 评论：65条
最新评论