FlowNetwork Python项目
flownetwork的Python项目详细描述
流量网络
用于流网络分析的python包https://pypi.python.org/pypi/flownetwork
安装最新的github版本
pipinstall-Ugit+https://github.com/chengjun/flownetwork.git
安装并升级
打开一个终端,输入:
pipinstallflownetwork
如果您想升级到新版本,只需输入:
pipinstall--upgradeflownetwork
如果要卸载,请输入:
pipuninstallflownetwork
导入
# import packagesimportflownetwork.flownetworkasfnimportnetworkxasnximportmatplotlib.pyplotaspltprint(fn.__version__)
$version = 3.0.9$
流动网络分析
help(fn.constructFlowNetwork)
Help on function constructFlowNetwork in module flownetwork.flownetwork:
constructFlowNetwork(C)
C is an array of two dimentions, e.g.,
C = np.array([[user1, item1],
[user1, item2],
[user2, item1],
[user2, item3]])
Return a balanced flow network
# constructing a flow networkdemo=fn.attention_datagd=fn.constructFlowNetwork(demo)
# drawing a demo networkfig=plt.figure(figsize=(12,8),facecolor='white')pos={0:np.array([0.2,0.8]),2:np.array([0.2,0.2]),1:np.array([0.4,0.6]),6:np.array([0.4,0.4]),4:np.array([0.7,0.8]),5:np.array([0.7,0.5]),3:np.array([0.7,0.2]),'sink':np.array([1,0.5]),'source':np.array([0,0.5])}width=[float(d['weight']*1.2)for(u,v,d)ingd.edges(data=True)]edge_labels=dict([((u,v,),d['weight'])foru,v,dingd.edges(data=True)])nx.draw_networkx_edge_labels(gd,pos,edge_labels=edge_labels,font_size=15,alpha=.5)nx.draw(gd,pos,node_size=3000,node_color='orange',alpha=0.2,width=width,edge_color='orange',style='solid')nx.draw_networkx_labels(gd,pos,font_size=18)plt.show()
nx.info(gd)
'Name: \nType: DiGraph\nNumber of nodes: 9\nNumber of edges: 15\nAverage in degree: 1.6667\nAverage out degree: 1.6667'
# balancing the network# if it is not balancedgh=fn.flowBalancing(gd)nx.info(gh)
'Name: \nType: DiGraph\nNumber of nodes: 9\nNumber of edges: 15\nAverage in degree: 1.6667\nAverage out degree: 1.6667'
# flow matrixm=fn.getFlowMatrix(gd)m
matrix([[ 0., 1., 0., 0., 3., 1., 0., 0., 0.],
[ 0., 0., 3., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 2., 0., 0., 0., 0., 2.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 2.],
[ 0., 0., 0., 0., 0., 1., 0., 0., 2.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 2.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[ 5., 2., 1., 0., 0., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
fn.getMarkovMatrix(m)
array([[ 0. , 0.2 , 0. , 0. , 0.6 ,
0.2 , 0. , 0. , 0. ],
[ 0. , 0. , 1. , 0. , 0. ,
0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0.5 , 0. ,
0. , 0. , 0. , 0.5 ],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 1. ],
[ 0. , 0. , 0. , 0. , 0. ,
0.33333333, 0. , 0. , 0.66666667],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 1. ],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 1. ],
[ 0.55555556, 0.22222222, 0.11111111, 0. , 0. ,
0. , 0.11111111, 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ]])
fn.getUmatrix(gd)
matrix([[ 1. , 0.2 , 0.2 , 0.1 , 0.6 ,
0.4 , 0. ],
[ 0. , 1. , 1. , 0.5 , 0. ,
0. , 0. ],
[ 0. , 0. , 1. , 0.5 , 0. ,
0. , 0. ],
[ 0. , 0. , 0. , 1. , 0. ,
0. , 0. ],
[ 0. , 0. , 0. , 0. , 1. ,
0.33333333, 0. ],
[ 0. , 0. , 0. , 0. , 0. ,
1. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 1. ]])
# return dissipationToSink,totalFlow,flowFromSourcefn.networkDissipate(gd)
defaultdict(<function flownetwork.flownetwork.<lambda>>,
{0: [0, 5, 5],
1: [0, 3, 2],
2: [2, 4, 1],
3: [2, 2, 0],
4: [2, 3, 0],
5: [2, 2, 0],
6: [1, 1, 1]})
# flow distancefn.flowDistanceFromSource(gd)
{0: 1.0,
1: 1.333333333333333,
2: 2.0,
3: 3.0,
4: 2.0,
5: 2.5,
6: 1.0,
'sink': 3.2222222222222214}
fn.outflow(gd,1)
3
fn.inflow(gd,1)
3
fn.averageFlowLength(gd)
3.2222222222222223
# fn.getAverageTimeMatrix(gd)
绘图
fig=plt.figure(figsize=(9,9),facecolor='white')ax=fig.add_subplot(111)fn.plotTree(gd,ax)plt.show()
fromrandomimportrandomx=np.array(range(1,100))y=(x+random()*x)**3plt.plot(x,y)plt.xscale('log');plt.yscale('log')plt.show()
fn.alloRegressPlot(x,y,'r','s','$x$','$y$',loglog=True)
rg=np.array([20.7863444,9.40547933,8.70934714,8.62690145,7.16978087,7.02575052,6.45280959,6.44755478,5.16630287,5.16092884,5.15618737,5.05610068,4.87023561,4.66753197,4.41807645,4.2635671,3.54454372,2.7087178,2.39016885,1.9483156,1.78393238,1.75432688,1.12789787,1.02098332,0.92653501,0.32586582,0.1514813,0.09722761])fn.powerLawExponentialCutOffPlot(rg,'$x$','$p(x)$')
[-0.0099301962503268171,
-0.064764460567964449,
-0.17705123513352666,
0.89999847894045781]
fn.DGBDPlot(rg)
fromnetworkx.utilsimportpowerlaw_sequencepl_sequence=powerlaw_sequence(1000,exponent=2.5)fig=plt.figure(figsize=(4,4),facecolor='white')ax=fig.add_subplot(111)fn.plotPowerlaw(pl_sequence,ax,'r','$x$')
Calculating best minimal value for power law fit
fig=plt.figure(figsize=(4,4),facecolor='white')ax=fig.add_subplot(111)fn.plotCCDF(pl_sequence,ax,'b','$x$')
Calculating best minimal value for power law fit
bins,result,gini_val=fn.gini_coefficient(np.array(pl_sequence))plt.plot(bins,bins,'--',label="perfect")plt.plot(bins,result,label="observed")plt.title("$GINI: %.4f$"%(gini_val))plt.legend(loc=0,frameon=False)plt.show()