• 无标度网络mark


    无标度网络

    无标度网路的度的分布具有幂律性。

    networkx包中有生成无标度网络的方法。

    G=nx.barabasi_albert_graph(100,20)
    

    第一个参数是节点数n,第二个参数是每个节点的连通数m(m<n)。生成一个含有n个节点、每次加入m条边的BA无标度网络。

    验证该网络是否为无标度网络,参考这篇博客 复杂网络学习笔记:验证无标度网络的幂律分布特性

    import random
    import networkx as nx
    import matplotlib.pyplot as plt
    from collections import Counter
    import math
    degree=[]
    for i in G.degree():
        degree.append(i[1])
    Dick_k = Counter(d for d in degree)
    #取了对数的数据
    f=open('result.txt','w')
    try:
        for x in Dick_k:
            y=math.log(Dick_k[x])
            logx=math.log(x)
            if y!=0 and logx!=0:
                f.write(str(logx)+' '+str(y)+'\n')
    finally:
    	f.close()
    	print ('程序执行完毕,可以查看文件了')#提示程序已经执行完毕         
    

    得到的result.txt在excel中打开,插入散点图并在散点图上做回归分析,即可

    在NetworkX中,可以用random_graphs.watts_strogatz_graph(n, k, p)方法生成一个含有n个节点、每个节点有k个邻居、以概率p随机化重连边的WS小世界网络。这里提供一种生成小世界网络的方式:

    #生成一个100节点的图
    def rand_edge(i,j,p=0.2):
    		probability=random.random()
    		if (probability<p):
        			G.add_edge(i,j)
    G=nx.Graph()
    H=nx.path_graph(100)#画出100个节点的简单路径
    G.add_nodes_from(H)
    i=0
    while(i<100):
    	j=0
    	while(j<i):
    		rand_edge(i,j)
    		j+=1
    	i+=1
    

    同时提供图形可视化方法:

    nx.draw(G,with_labels=True)
    plt.show()
    #画degree的散点图
    degree=[]
    for i in G.degree():
        degree.append(i[1])
    counts = Counter(d for d in degree)#生成{度:频数}的字典
    b = [counts.get(i) for i in range(max(counts) + 1)]
    x = range(len(b)) # x轴
    y = [z for z in b] # y轴
    plt.figure(figsize=(5.8, 5.2), dpi=50)
    plt.xlabel("Degree")
    plt.ylabel("Frequency")
    plt.xticks(fontproperties='Times New Roman', size=14)
    plt.yticks(fontproperties='Times New Roman', size=14)
    plt.loglog(x, y, '.')
    plt.show()
  • 相关阅读:
    阅读笔记
    个人总结
    《软件需求(第二版)》阅读笔记02
    《软件需求(第二版)》阅读笔记01
    问题账户需求分析
    2017年秋季个人阅读计划
    阅读笔记一之《软件需求与分析》
    每日总结1
    开发体会(模块3.商品分类管理)
    个人总结
  • 原文地址:https://www.cnblogs.com/ting65536/p/16186674.html
Copyright © 2020-2023  润新知