#生成10个节点
import pandas as pd
import itertools
u_list = list(range(0,10))
print(u_list)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#构建可能的连接
com = itertools.combinations(u_list, 2)
com_list = list(com)
print(len(com_list))
print(com_list)
45
[(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7),
(0, 8), (0, 9), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6),
(1, 7), (1, 8), (1, 9), (2, 3), (2, 4), (2, 5), (2, 6),
(2, 7), (2, 8), (2, 9), (3, 4), (3, 5), (3, 6), (3, 7),
(3, 8), (3, 9), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9),
(5, 6), (5, 7), (5, 8), (5, 9), (6, 7), (6, 8), (6, 9),
(7, 8), (7, 9), (8, 9)]
import math
sum=0
for i in range(0,45):
sum=sum+math.comb(45,i)
sum
35184372088831
import math
math.comb(45, 1)
45
# 生成边的组合
com = itertools.combinations(u_list, 2)
com_list = list(com)
# 数据->图结构整理
import matplotlib.pyplot as plt
import networkx as nx
#设置显示图片大小
fig = plt.figure(figsize=(10,18),dpi=500)
fig.subplots_adjust(wspace=0.02, hspace=0.02)
# 绘图函数的构建
for i in range(1,len(com_list)+1):
G = nx.Graph()
G.add_nodes_from(u_list)
G.add_edges_from([com_list[i-1]])
#节点大小设置,与度关联
node_size = [G.degree(i)**0.5*80+80 for i in G.nodes()]
#设置颜色 随机来点
colors = ['#43CD80','DeepPink','orange','#008B8B','purple','#63B8FF','#BC8F8F','#3CB371','b','orange','y','c','#838B8B','purple','olive','#A0CBE2','#4EEE94']*200
colors = colors[0:len(G.nodes())]
# import random
# colors = random.sample(colors, len(G.nodes()))
ax = plt.subplot(9,5,i)
ax.spines['top'].set_linewidth(0.2)
ax.spines['right'].set_linewidth(0.2)
ax.spines['bottom'].set_linewidth(0.2)
ax.spines['left'].set_linewidth(0.2)
#可以替换两种不同的布局看看效果 kamada_kawai_layout spring_layout #pos = nx.kamada_kawai_layout(G_i,scale=1),
nx.draw_networkx(G,
pos = nx.spring_layout(G,iterations=2),
node_color = colors,
edge_color = '#2E8B57',
font_size = 6,
node_size = node_size,
alpha = 0.92,
width = 0.4
)
#plt.axis('off')
#plt.savefig( "keyword.png", format="PNG")
plt.show()
# 生成边的组合
com = itertools.combinations(u_list, 2)
com_list = list(com)
#在从边的组合里面抽取n个边连通
from random import shuffle
shuffle(com_list)
iters = itertools.combinations(com_list,3)
edges = []
flag=0
for i in iters:
if flag<=200:
edges.append(i)
flag+=1
else:
break
# 数据->图结构整理
import matplotlib.pyplot as plt
import networkx as nx
#设置显示图片大小
fig = plt.figure(figsize=(18,18),dpi=500)
fig.subplots_adjust(wspace=0.02, hspace=0.02)
# 绘图函数的构建
for i in range(1,50):
G = nx.Graph()
G.add_nodes_from(u_list)
G.add_edges_from(list(edges[i-1]))
#节点大小设置,与度关联
node_size = [G.degree(i)**0.85*200+100 for i in G.nodes()]
#设置颜色 随机来点
colors = ['#43CD80','DeepPink','orange','#008B8B','purple','#63B8FF','#BC8F8F','#3CB371','b','orange','y','c','#838B8B','purple','olive','#A0CBE2','#4EEE94']*200
colors = colors[0:len(G.nodes())]
# import random
# colors = random.sample(colors, len(G.nodes()))
ax = plt.subplot(7,7,i)
ax.spines['top'].set_linewidth(0.2)
ax.spines['right'].set_linewidth(0.2)
ax.spines['bottom'].set_linewidth(0.2)
ax.spines['left'].set_linewidth(0.2)
#可视化
nx.draw_networkx(G,
pos = nx.spring_layout(G,iterations=12),
#pos = nx.kamada_kawai_layout(G),
node_color = colors,
edge_color = '#2E8B57',
font_size = 6,
node_size = node_size,
alpha = 0.98,
width = 0.4
)
#plt.axis('off')
#plt.savefig( "keyword.png", format="PNG")
plt.show()
往期精彩:
SynchroTrap-基于松散行为相似度的欺诈账户检测算法