从时间序列到复杂网络:可见图算法
2015-06-01 20:43
483 查看
这篇文章实现的算法来源于PNAS杂志:
PNAS paper:From time series to complex networks:The visibility graph
代码参考:
代码运行结果:
绘图参考链接:
http://networkx.github.io/documentation/latest/examples/drawing/weighted_graph.html
PNAS paper:From time series to complex networks:The visibility graph
代码参考:
# coding: utf-8 import networkx as nx import matplotlib.pyplot as plt from itertools import combinations def visibility_graph(series): visibility_graph_edges=[] # convert list of magnitudes into list of tuples that hold the index tseries = [] n = 1 for magnitude in series: tseries.append( (n, magnitude ) ) n += 1 for a,b in combinations(tseries, 2): # two points, maybe connect (ta, ya) = a #实现(1,2)(1,3)(1,4)(1,5)--(2,3)(2,4)(2,5)--(3,4)(3,5)--(4,5)任意两个边相互比较 (tb, yb) = b connect = True medium=tseries[ta:tb-1]#此处需要多留意,ta是1到k,而tseris是从0下标开始 所以此处不能不是[ta+1:tb] for tc,yc in medium: #print yc,(yb + (ya - yb) * ( float(tb - tc) / (tb - ta) ))#一定要float(tb-tc)/(tb-ta)因为计算机里1/2为0,1.0/2才为0.5 if yc > yb + (ya - yb) * ( float(tb - tc) / (tb - ta) ): connect =False if connect: visibility_graph_edges.append((ta,tb)) return visibility_graph_edges def DrawVisibilityGraph(graphEdges): G=nx.Graph() #G=nx.Graph()无向图 G=nx.DiGraph()有向图 #add the edge to graph G for i in graphEdges: G.add_edge(i[0],i[1]) pos=nx.spring_layout(G) nx.draw_networkx_nodes(G,pos,node_size=700) nx.draw_networkx_edges(G,pos,width=5) nx.draw_networkx_labels(G,pos,font_size=20,font_family='sans-serif') #plt.savefig("test.png") #save the picture plt.show() if __name__=="__main__": #time series series = [0.87,0.49,0.36,0.83,0.87] #get the graph edges graphEdges=visibility_graph(series) #draw the visibility graph DrawVisibilityGraph(graphEdges)
代码运行结果:
绘图参考链接:
http://networkx.github.io/documentation/latest/examples/drawing/weighted_graph.html
相关文章推荐
- 从多层感知器到卷积网络(一)
- TCP三次握手和四次挥手过程以及连接为什么使用三次连接,释放使用四次
- 使用Charles进行网络请求抓包解析
- http响应状态码301和302
- Linux网络编程:原始套接字编程及实例分析(3)
- Linux网络编程:原始套接字编程及实例分析(二)
- DHCP的配置及应用
- tomcat开启https
- MFC中的CAsyncSocket类实现网络通信
- https 的post数据
- 猫猫学iOS(五十五)多线程网络之图片下载框架之SDWebImage
- 卷积神经网络总结
- HttpServletRequest 常用返回值类型
- AsyncTask实现ListView中异步加载网络图片
- C++网络编程概述(精)
- PHP的cURL库:抓取网页,POST数据及其他,HTTP认证 抓取数据
- 网络分析工具
- 基于HTTP 协议认证介绍与实现
- android有关网络连接的方法
- Google网络框架Volley的使用,Cache-Control=no-cache时强制缓存的处理