-
Notifications
You must be signed in to change notification settings - Fork 1
/
weight_clustering1.py
72 lines (59 loc) · 2.24 KB
/
weight_clustering1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 26 10:09:22 2018
@author: xsjxi
"""
import networkx as nx
#此种方法和networkx中默认的加权聚类系数一样
def weight_clustering2(G,z):
weight_cl = 0
edges = nx.edges(G)
edge_weight = {}
edge_one_weight = {}
max_weight = 0
weight_cl = 0
for u, v in edges:
weight = G.get_edge_data(u,v)['weight']
edge_weight[(u, v)] = weight
edge_weight[(v, u)] = weight
if weight >= max_weight:
max_weight = weight
for u, v in edges:
edge_one_weight[(u, v)] = edge_weight[(u, v)]/max_weight
edge_one_weight[(v, u)] = edge_weight[(u, v)]/max_weight
z_degree = nx.degree(G, z)
cl = 0
if z_degree == 1:
return 0.0
else:
for u in nx.neighbors(G, z):
for v in nx.neighbors(G, z):
if (u!=v)&(G.has_edge(u, v)):
cl = cl + (edge_one_weight[(u, v)]*edge_one_weight[u, z]*edge_one_weight[v, z])**(1/3)
weight_cl = (1/(z_degree*(z_degree - 1)))*cl
return weight_cl
# =============================================================================
# #G = nx.read_weighted_edgelist('J:\\Python\\LinkPrediction\\celegansneural.edgelist')
# G = nx.Graph()
# #G.add_weighted_edges_from([(1,4,1),(1,3,1),(1,2,1),(2,5,1),(2,4,1),(5,7,1),(5,6,1),(6,7,1),(6,8,1),(7,8,1)])
# G.add_weighted_edges_from([(1,4,2),(1,3,1),(1,2,2),(2,5,2),(2,4,1),(5,7,1),(5,6,1),(6,7,3),(6,8,1),(7,8,2)])
# nodes = nx.nodes(G)
# edges = nx.edges(G)
#
# for u in nodes:
# print(u)
# print(weight_clustering(G,u))#加权聚类系数
# =============================================================================
# =============================================================================
# G = nx.Graph()
# G.add_weighted_edges_from([(1,2,2.0),(1,3,1.0),(2,4,2.0),(2,5,2.0),(5,6,1.0),(5,7,8.0),(6,7,1.0),(6,8,3.0),(7,8,2.0)])
# edges = nx.edges(G)
# nodes = nx.nodes(G)
# for v in nodes:
# cc_z = nx.clustering(G, v)
# #gt = nx.triangles(G,v)
# print(str(v)+"-----"+str(cc_z))
# #print(str(v)+"-----"+str(gt))
# print(nx.dijkstra_path(G, 3, 6))
#
# =============================================================================