def multi_sampling(): f = FaceData() G = f.create_graph() # 获取原始数据 _degree = dict() # 分析重组数据 chushu = 0 for i in xrange(38): mhrw = common.metropolis_hastings_random_walk(G, None, 10000, "unique") # impore = improve_MH.impore_02(G, None, 10000, "unique") for i in mhrw: _degree[G.degree(i)] = _degree.get(G.degree(i), 0) + 1 chushu += len(mhrw) print "step " # 处理数据,取平均的度分布 x = sorted(_degree.iterkeys()) #生成x轴序列,从1到最大度 num = chushu y = [] for i in x: y.append(float(_degree[i]) / num) # 保存数据 f = open("../compare/degree_plot/dup_Twitter_Mh.txt", "w") try: for i in x: f.write(str(i) + " " + str(float(_degree[i]) / num) + "\n") finally: f.close() ################## _degree = dict() # 分析重组数据 chushu = 0 for i in xrange(38): # mhrw = common.metropolis_hastings_random_walk(G,None,10000,"unique") impore = improve_MH.impore_02(G, None, 10000, "unique") for i in impore: _degree[G.degree(i)] = _degree.get(G.degree(i), 0) + 1 chushu += len(impore) print "step " # 处理数据,取平均的度分布 x = sorted(_degree.iterkeys()) #生成x轴序列,从1到最大度 num = chushu y = [] for i in x: y.append(float(_degree[i]) / num) # 保存数据 f = open("../compare/degree_plot/dup_Twitter_Ud.txt", "w") try: for i in x: f.write(str(i) + " " + str(float(_degree[i]) / num) + "\n") finally: f.close()
def ori_avg_degree(): f = FaceData() G = f.create_graph() # 获取原始数据 rw = common.random_walk(G, None, 16000, "unique") mhrw = common.metropolis_hastings_random_walk(G, None, 16000, "unique") bfs = common.BFS(G, None, 16000, "unique") print "原始网络的平均度为: ", degree.avg_degree(G) print "RW抽样得到的平均度为: ", degree.avg_degree(G, rw) print "MHRW抽样得到的平均度为: ", degree.avg_degree(G, mhrw) print "BFS抽样得到的平均度为: ", degree.avg_degree(G, bfs)
def duplicate_epinions_Mhrw(): t = FaceData() G = t.create_graph() # 获取原始数据 f = open("../compare/avg_degree/dup_Epinion_Mhrw.txt", "w") try: for i in xrange(100, 10000, 100): mhrw = common.metropolis_hastings_random_walk(G, None, i, "unique") f.write(str(i) + " " + str(mhrw) + "\n") finally: f.close()
def compare_press(): f = FaceData() G = f.create_graph() # 获取原始数据 rw = common.random_walk(G, None, 10000, "unique") mhrw = common.metropolis_hastings_random_walk(G, None, 10000, "unique") # print round(1/impore - 1, 4) # 保存数据 f = open("../compare/alpha_dup/com_dup.txt", "w") try: f.write(str("rw") + " " + str(rw) + "\n") f.write(str("mhrw") + " " + str(mhrw) + "\n") finally: f.close()
def ego_degree(): f = FaceData() G = f.create_graph() # 获取原始数据 # rw = common.random_walk(G,None,10000,"unique") mhrw = common.metropolis_hastings_random_walk(G, None, 10000, "unique") # bfs = common.BFS(G,None,8000,"unique") impore = improve_MH.impore_03(G, None, 10000, "unique") _plt = degree.degree(G, plt, "Original", "k-") # 绘制原始网络的度分布 # _plt = degree.ego_degree(G,bfs,_plt,"ego-bfs","c*") # _plt = degree.ego_degree(G,rw,_plt,"ego-RW","k-") # _plt = degree.ego_degree(G,mhrw,_plt,"MHRW","b-.") _plt = degree.ego_degree(G, mhrw, _plt, "MHRW", "bv-") # _plt = degree.ego_degree(G,impore,_plt,"UD","r:") _plt = degree.ego_degree(G, impore, _plt, "UD", "rh-") _plt.ylabel(r'$P(k_\upsilon=k) $') _plt.xlabel(u'node degree k') # _plt.xlim(0,100) _plt.legend(loc="upper right") # 加入图例 _plt.show()
def record_con(self, sample="MHRW", lengend=False): # 在G中抽取节点 if sample == "MHRW": nodes = common.metropolis_hastings_random_walk(self.G, None, 10000, "total") elif sample == "RW": nodes = common.random_walk(self.G, None, 10000, "total") elif sample == "BFS": nodes = common.BFS(self.G, 10000) else: nodes = improve_MH.impore_03(self.G, None, 10000, "total") x = range(self.x) # 横坐标 y = [] numerator = 0 # 分子 denominator = 0 # 分母 for i in nodes[0:self.x]: denominator += 1 if self.G.degree(i) <= self.limit: numerator += 1 y.append(float(numerator)/denominator) if sample == "MHRW": if lengend is True: self.plt.plot(x, y, "b--", label="MHRW", linewidth=3.0) else: self.plt.plot(x, y, "b--", linewidth=3.0) elif sample == "RW": if lengend is True: self.plt.plot(x, y, "g:", label="RW", linewidth=3.0) else: self.plt.plot(x, y, "g:", linewidth=3.0) elif sample == "BFS": if lengend is True: self.plt.plot(x, y, "c-.", label="BFS", linewidth=3.0) else: self.plt.plot(x, y, "c-.", linewidth=3.0) else: if lengend is True: self.plt.plot(x, y, "r-", label="UD", linewidth=3.0) else: self.plt.plot(x, y, "r-", linewidth=3.0)
def record_con(self, sample="MHRW", lengend=False): # 在G中抽取节点 if sample == "MHRW": nodes = common.metropolis_hastings_random_walk(self.G, None, self.x, "total") elif sample == "RW": nodes = common.random_walk(self.G, None, self.x, "total") elif sample == "BFS": nodes = common.BFS(self.G, self.x) else: nodes = improve_MH.impore_03(self.G, None, self.x, "total") x = range(self.x) # 横坐标 y = [] list_x = [] for i in nodes: list_x.append(self.G.degree(i)) x,y = self.Z_test(list_x) if sample == "MHRW": if lengend is True: self.plt.plot(x, y, c='b',marker='v', label="MHRW", linewidth=1.0) else: self.plt.plot(x, y, c='b',marker='v', linewidth=1.0) elif sample == "RW": if lengend is True: self.plt.plot(x, y, c='m',marker='*', label="RW", linewidth=1.0) else: self.plt.plot(x, y, c='m',marker='*', linewidth=1.0) elif sample == "BFS": if lengend is True: self.plt.plot(x, y, c='g',marker='s', label="BFS", linewidth=1.0) else: self.plt.plot(x, y, c='g',marker='s', linewidth=1.0) else: if lengend is True: self.plt.plot(x, y, c='r',marker='o', label="UD", linewidth=1.0) else: self.plt.plot(x, y, c='r',marker='o', linewidth=1.0)