python复杂网络库networkx:基础
http://blog.csdn.net/pipisorry/article/details/49839251
其它复杂网络绘图库
[ArcGIS,Python,网络数据集中查询两点最短路径]
Networkx数据类型
Graph types
NetworkX provides data structures and methods for storing graphs.
All NetworkX graph classes allow (hashable) Python objects as nodes.and any Python object can be assigned as an edge attribute.
The choice of graph class depends on the structure of thegraph you want to represent.
使用哪种图形类
| Graph Type | NetworkX Class |
|---|---|
| Undirected Simple | Graph |
| Directed Simple | DiGraph |
| With Self-loops | Graph, DiGraph |
| With Parallel edges | MultiGraph, MultiDiGraph |
- Graph – Undirected graphs with self loops
- DiGraph - Directed graphs with self loops
- MultiGraph - Undirected graphs with self loops and parallel edges
- MultiDiGraph - Directed graphs with self loops and parallel edges
- Overview
- Adding and Removing Nodes and Edges
- Iterating over nodes and edges
- Information about graph structure
- Making copies and subgraphs
子图subgraphs
Graph.subgraph(nbunch)
参数nbunch指定子图的节点
返回原图上的子图
二分网络
建立二分网络
import networkx
from network.algorithm import bipartite
g.add_edges_from([("nodename1","nodename2"),("nodename3","nodename1")])
判断是否是二分网络
print bi_partite.is_bipartite(g)
得到两端网络
NSet = nx.bipartite.sets(g)
Net1 = nx.project(g,NSet[0])
Net2 = nx.project(g,Nset[1])
networkx的使用
import networkx as nx
使用Python与NetworkX获取数据:基本使用
>>> import networkx as net
>>> import urllib
NetworkX以图(graph)为基本数据结构。图既可以由程序生成,也可以来自在线数据源,还可以从文件与数据库中读取。
>>> g=net.Graph() #创建空图
>>> g.add_edge('a','b') #插入一条连接a,b的边到图中,节点将自动插入
>>> g.add_edge('b','c') #再插入一条连接b,c的边
>>> g.add_edge('c','a') #再插入一条连接c,a的边
>>> net.draw(g) #输出一个三角形的图
你也可以将图的节点与边作为Python列表输出:
>>>> g.nodes() #输出图g的节点值
['a','b','c']
>>>> g.edges() #输出图g的边值
[('a', 'c'), ('a', 'b'), ('c', 'b')]
NetworkX中的图数据结构就像Python的 字典(dict) 一样——一切都能循环,并根据键值读取。
>>> g.node['a']
{}
>>> g.node['a']['size']=1
>>> g.node['a']
{'size' : 1}
节点与边能够存储任意类型字典的属性和任意其他丰富类型的数据:
>>> g['a'] #将临近边及权重作为字典返回输出
{'b': {}, 'c': {}}
>>> g['a']['b'] #返回节点A->B的属性
{}
>>> g['a']['b']['weight']=1 #设置边的属性
>>> g['a']['b']
{'weight' : 1}
多数的计算社会网络指标也返回一个字典,节点ID作为字典键,指标作为字典的值。你可以像操作任何字典一样操作它们。
图Graph
degree(G[, nbunch, weight]) |
Return degree of single node or of nbunch of nodes. |
degree_histogram(G) |
Return a list of the frequency of each degree value. |
density(G) |
Return the density of a graph. |
info(G[, n]) |
Print short summary of information for the graph G or the node n. |
create_empty_copy(G[, with_nodes]) |
Return a copy of the graph G with all of the edges removed. |
is_directed(G) |
Return True if graph is directed. |
节点Nodes
nodes(G) |
Return a copy of the graph nodes in a list. |
number_of_nodes(G) |
Return the number of nodes in the graph. |
nodes_iter(G) |
Return an iterator over the graph nodes. |
all_neighbors(graph, node) |
Returns all of the neighbors of a node in the graph. |
non_neighbors(graph, node) |
Returns the non-neighbors of the node in the graph. |
common_neighbors(G, u, v) |
Return the common neighbors of two nodes in a graph. |
边edges
边相关方法
edges(G[, nbunch]) |
Return list of edges incident to nodes in nbunch. |
number_of_edges(G) |
Return the number of edges in the graph. |
edges_iter(G[, nbunch]) |
Return iterator over edges incident to nodes in nbunch. |
non_edges(graph) |
Returns the non-existent edges in the graph. |
有序边
target_subgraph.edges()返回的边是无序的。
修改成有序:sortEdges = lambda l: [(n1, n2) if n1 <= n2 else (n2, n1) for n1, n2 in l]
G.number_of_edges()
方法其实是图类的方法G.number_of_edges()
number_of_edges(self, u=None, v=None): """Return the number of edges between two nodes
获取属性Attributes
set_node_attributes(G, name, values) |
Set node attributes from dictionary of nodes and values |
get_node_attributes(G, name) |
Get node attributes from graph |
set_edge_attributes(G, name, values) |
Set edge attributes from dictionary of edge tuples and values. |
get_edge_attributes(G, name) |
Get edge attributes from graph |
获取边属性get_edge_attributes(G, name)
返回的是一个key为边的dict
edges_weight_index = nx.get_edge_attributes(target_subgraph, 'weight')
edges_weight_index[(u, v)]
添加边
g=net.Graph() #创建空图
g.add_edge('a','b') #插入一条连接a,b的边到图中,节点将自动插入
批量添加有权边
g.add_weighted_edges_from([(1,2,0.125),(1,3,0.75),(2,4,1.2),(3,4,0.375)])
绘制有权图[Weighted Graph]
[Edges]
from:http://blog.csdn.net/pipisorry/article/details/49839251
ref: [Functions]*
[Networkx Reference]*[NetworkX documentation]*[doc NetworkX Examples]*[NetworkX Home]
[NetworkX sourse code download]
[sciencenet 复杂网络分析库NetworkX学习笔记]*
[networkx笔记系列]
python复杂网络库networkx:基础的更多相关文章
- python复杂网络库networkx:算法
http://blog.csdn.net/pipisorry/article/details/54020333 Networks算法Algorithms 最短路径Shortest Paths shor ...
- python复杂网络库networkx:绘图draw
http://blog.csdn.net/pipisorry/article/details/54291831 networkx使用matplotlib绘制函数 draw(G[, pos, ax, h ...
- Python 并发网络库
Python 并发网络库 Tornado VS Gevent VS Asyncio Tornado:并发网络库,同时也是一个 web 微框架 Gevent:绿色线程(greenlet)实现并发,猴子补 ...
- python面试题库——1Python基础篇
第一部分 Python基础篇(80题) 为什么学习Python? 语言本身简洁,优美,功能超级强大,跨平台,从桌面应用,web开发,自动化测试运维,爬虫,人工智能,大数据处理都能做 Python和Ja ...
- python复杂网络分析库NetworkX
NetworkX是一个用Python语言开发的图论与复杂网络建模工具,内置了常用的图与复杂网络分析算法,可以方便的进行复杂网络数据分析.仿真建模等工作.networkx支持创建简单无向图.有向图和多重 ...
- Python常用的库简单介绍一下
Python常用的库简单介绍一下fuzzywuzzy ,字符串模糊匹配. esmre ,正则表达式的加速器. colorama 主要用来给文本添加各种颜色,并且非常简单易用. Prettytable ...
- Python高级网络编程系列之第一篇
在上一篇中我们简单的说了一下Python中网络编程的基础知识(相关API就不解释了),其中还有什么细节的知识点没有进行说明,如什么是TCP/IP协议有几种状态,什么是TCP三次握手,什么是TCP四次握 ...
- Python爬虫 requests库基础
requests库简介 requests是使用Apache2 licensed 许可证的HTTP库. 用python编写. 比urllib2模块更简洁. Request支持HTTP连接保持和连接池,支 ...
- 使用python网络库下载
下载1000次网页资源 1,普通循环方式下载1000次,非常慢 #!/usr/bin/python # -*- coding: utf-8 -*- import sys import os impor ...
随机推荐
- 数据结构之Trie树
1. 概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie一词来自retrieve,发音为/tr ...
- 使用RedisDesktopManager工具,解决连接失败问题
今天在云服务器上搭建好了redis环境,想用RedisDesktopManager工具去连接一下,结果连接不上,显示如下图: 我确保了服务器防火墙关闭,又在redis配置文件中设置了requirepa ...
- Python中capitalize()与title()的区别
capitalize()与title()都可以实现字符串首字母大写.主要区别在于:capitalize(): 字符串第一个字母大写title(): 字符串内的所有单词的首字母大写 例如: >&g ...
- Oracle中的列转行例子详解
数据如下:name id张三 1,2,3 要求实现:name id张三 1张三 2张三 3 --创建临时表 create table tmp as(select '张三' name, '1,2,3' ...
- 好IT男不能“淫”-谈IT人员目前普遍存在的“A情绪”
<如果当道德无法约束你的时候...那么就让对疾病的恐惧来制约你吧> 前言 在写这篇文章前我的心情无比的沉重.几次提笔欲写,几次又未能完成,可是最终让我"奋笔疾书"的原因 ...
- (译)Objective-C 类属性
翻译自:Objective-C Class Properties 译者:Haley_Wong 由于Swift 3.0 出了太多令人兴奋的新特性,人们很容易忽略 Objective-C中的小改动.苹果展 ...
- Android图表库MPAndroidChart(十三)——简约的底部柱状图
Android图表库MPAndroidChart(十三)--简约的底部柱状图 我们继续上一讲,今天还是说下柱状图,这个图的话应该是用的比较多的,所有拿出来溜溜,先看下效果 我们还是来看下基本实现 一. ...
- Python实现数据库一键导出为Excel表格
依赖 Python2711 xlwt MySQLdb 数据库相关 连接 获取字段信息 获取数据 Excel基础 workbook sheet 案例 封装 封装之后 测试结果 总结 数据库数据导出为ex ...
- 内存数据网格IMDG简介
1 简介 将内存作为首要存储介质不是什么新鲜事儿,我们身边有很多主存数据库(IMDB或MMDB)的例子.在对主存的使用上,内存数据网格(In Memory Data Grid,IMDG)与IMDB类似 ...
- 自定义gradview
http://blog.csdn.net/jdsjlzx/article/details/7525724 虽然Android已自带了GridView,但是,却不够灵活,同时也不能自由添加控件,因此,本 ...