BA网络构造原理

import numpy as np
import random
unnormalized_probs = []  #node id
node = [1,5,3,7,9]
snode = sorted(node)  # weights for nodes
a=[2,2,2,2,2]
norm_const = sum(a)
normalized_probs = [float(u_prob)/norm_const for u_prob in a]
b=np.cumsum(normalized_probs)
y=random.random()
c=np.where(b>y)[0]  #返回b中值首次大于y的索引
print(snode)
print(normalized_probs)
print(b)
print(y)
print(c[0])
print(snode[c[0]])

 

 

 

def BA(pathfile):
            import random
            import numpy as np
            import xlrd  #xlrd(读excel操作),xlwt(写excel操作)
            import networkx as nx


            # 数据导入,预处理
            def open_excel(pathfile):      #打开excel
                try:
                    sheet_data = xlrd.open_workbook(pathfile)  #打开Excel文件读取数据
                    return sheet_data
                    print("excel打开成功")
                except Exception as e:
                    print(str(e))


            List = []
            def praise_excel(pathfile):
                sheet_data = open_excel(pathfile)
                sheet = sheet_data.sheets()[0]    #通过索引顺序获取一个sheet
                my_queues0 = sheet.col_values(0)   #读取1列
                my_queues1 = sheet.col_values(1)   #读取2列
                nrows = sheet.nrows #获取行数
                for i, j in zip(my_queues0,my_queues1):
                    x = (int(i),int(j))
                    List.append(x)
                # print("边数据,如(10,3)为源节点为10,目标节点为3:",List)


            praise_excel(r"C:\Users\WBL\Desktop\DeepWalk\karate.xlsx")


            temp1 = sorted(List)  # 列表中有元组的,默认对元组的第一列进行排序
            temp2 = sorted(List, key=lambda x:x[1])  # 补充:列表中,使用tuple对第二列元素进行排序
            # print('按源节点排序:',temp1)
            # print("按目标节点排序:",temp2)


            matrix_temp1 = np.array(temp1)  # print('matrix_temp1: \n',matrix_temp1)
            temp1_nrows = matrix_temp1.shape[0]
            # print(temp1_nrows)

            G=nx.Graph(List)
            # pos = nx.spring_layout(G)
            node_k = G.degree()  #节点度,元组形式,第一个表示节点,第二个表示度,(10,2)表示节点10的度为2
            # print("节点度,形如(11,3)表示为节点10的度为3:",node_k)

            temp3 = sorted(node_k)   #对节点排序,(1,16)节点1度为16,(2,9)节点2度为9
            # print("对节点排序,(1,16)节点1的度为16,(2,9)节点2的度为9:",temp3)

            node_id = []  #节点id
            for id in range(len(temp3)):
                node_id.append(temp3[id][0])
            # print("节点id",node_id)


            node_k1=[]  #节点的度
            for id in range(len(temp3)):
                node_k1.append(temp3[id][1])
            # print("节点的度",node_k1)



            norm_const = sum(node_k1)
            normalized_probs = [float(u_prob)/norm_const for u_prob in node_k1]
            b=np.cumsum(normalized_probs)  #累加
            y=random.random()   #random.random()
            c=np.where(b>y)[0]  #返回b中值首次大于y的b的值的索引



            # print(normalized_probs)
            # print(b)
            # print(y)
            # print(c[0])
            return(node_id[c[0]])




from BA import *
print(BA(r"C:\Users\WBL\Desktop\DeepWalk\karate.xlsx"))

 

参考:https://www.zhihu.com/people/yezhonglin/posts

你可能感兴趣的:(BA网络构造原理)