我正在使用python3.6实现一个层次聚类算法(具有相似性),下面的工作基本上是构建新的空图,并保持递归地连接原始图上相似性最大的组(这里用列表表示)
在代码的第1位的代码,我想返回最好的分区,但是函数返回与comminity_list
完全相同,它看起来像best_partition = comminity_list.
使best_partition
指向'community\u list'的地址。为什么会发生这种情况,我这里错了什么?我该怎么修
def pearson_clustering(G):
H = nx.create_empty_copy(G). # build a empty copy of G(no vetices)
best = 0 #for current modularity
current =0 #for best modualarty
A = nx.adj_matrix(G). #get adjacent matrix
org_deg =deg_dict(A, G.nodes()) # degree of G
org_E = G.number_of_edges(). # number of edges of G
comminity_list = intial_commnity_list(G) # function return a list of lists here
best_partition = None
p_table =pearson_table(G) #pearson_table return a dictionary of each pair Pearson correlation
l = len(comminity_list)
while True:
if(l == 2): break
current = modualratiry(H,org_deg,org_E) #find current modularity
l = len(comminity_list)
p_build_cluster(p_table,H,G,comminity_list) #building clustering on H
if(best < current):
best_partition = comminity_list. #postion1
best = current #find the clustering with largest modularity
return best_partition #postion2
这只是python的隐式赋值行为。当您执行“best\u partition=community\u list”时,您只需将community\u list分配给与best\u partition相同的地址
如果要显式复制列表,可以使用以下命令(用community\u列表替换list best\u分区):
或者copy function。如果community\u list有子列表,您将需要deepcopy函数,而不是来自同一模块(否则您将获得原始列表的副本,但子列表仍然只是地址引用)
相关问题 更多 >
编程相关推荐