本文整理汇总了Python中numpy.ndarray.count方法的典型用法代码示例。如果您正苦于以下问题:Python ndarray.count方法的具体用法?Python ndarray.count怎么用?Python ndarray.count使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.ndarray
的用法示例。
在下文中一共展示了ndarray.count方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: c_int_ext
# 需要导入模块: from numpy import ndarray [as 别名]
# 或者: from numpy.ndarray import count [as 别名]
def c_int_ext(k: int, aff: np.ndarray, adj_mat: np.ndarray, threads_nb=1) -> float:
"""
This function calculates the inter/intra-cluster density
as defined in Santo Fortunato, Community Detection in Graphs, Physics Reports, 486, 75-174(2010)
Parameters
----------
k : int
The number of clusters
aff : np.ndarray
A 1-D array contains the affectation of nodes to their clusters
adj_mat : np.ndarray
Adjacency matrix
Returns
-------
float, float
The value of sum(sigma_int), sum(sigma_ext)
which is the quality of the clustering.
"""
global int_sigmas
global ext_sigmas
# initiate to zeros
int_sigmas = np.zeros(k)
ext_sigmas = np.zeros(k)
# Get the number of nodes
n = len(aff)
# Calculates the internal and external edges
# for each cluster
threads = [] # type: list[Thread]
# if threads number is too large then update it
if n / 10 < threads_nb:
threads_nb = int(n / 10)
# create threads instances
for i in range(threads_nb):
from_i = int(i * (n / threads_nb))
to_i = int((i + 1) * (n / threads_nb))
t = Thread(target=calculate_int_ext_edges, args=(adj_mat, aff, from_i, to_i))
threads.append(t)
threads[i].start()
# Wait fo threads to finish
for t in threads:
t.join()
# Transform aff from np.ndarray to list
# to be able to use the count function
aff = aff.tolist() # type: list
# Calculates the density for each cluster
for i in range(k):
nb_c_i = aff.count(i)
if nb_c_i <= 1:
int_sigmas[i] = 0
ext_sigmas[i] = 0
else:
int_sigmas[i] /= (nb_c_i * (nb_c_i - 1) / 2)
ext_sigmas[i] /= (nb_c_i * (n - nb_c_i))
# Return the density for all the clusters
return sum(int_sigmas) / k, sum(ext_sigmas) / k