def computeNetworkAccuracy(matching,train_cluster_inverse, num_clusters):
"""
Takes in the matching for the clusters
takes the computed clusters
computes the average F1 score over the network
"""
threshold = 1e-2
f1 = 0
for cluster in xrange(num_clusters):
true_cluster_cov = np.loadtxt("Inverse Covariance cluster ="+ str(cluster) +".csv", delimiter = ",")
matched_cluster = matching[cluster]
matched_cluster_cov = train_cluster_inverse[matched_cluster]
(nrow,ncol) = true_cluster_cov.shape
out_true = np.zeros([nrow,ncol])
for i in xrange(nrow):
for j in xrange(ncol):
if np.abs(true_cluster_cov[i,j]) > threshold:
out_true[i,j] = 1
out_matched = np.zeros([nrow,ncol])
for i in xrange(nrow):
for j in xrange(ncol):
if np.abs(matched_cluster_cov[i,j]) > threshold:
out_matched[i,j] = 1
np.savetxt("Network_true_cluster=" +str(cluster) + ".csv",true_cluster_cov, delimiter = ",")
np.savetxt("Network_matched_cluster=" + str(matched_cluster)+".csv",matched_cluster_cov, delimiter = ",")
##compute the confusion matrix
confusion_matrix = np.zeros([2,2])
for i in xrange(nrow):
for j in xrange(ncol):
confusion_matrix[out_true[i,j],out_matched[i,j]] += 1
f1 += computeF1_macro(confusion_matrix, [0,1],2)
return f1/num_clusters
############
评论列表
文章目录