java - Java 中的 Davies-Bouldin 指数

标签 java algorithm cluster-analysis

我正在编写一种遗传算法,尝试选择一组数据点以最大化簇间距离,同时保持两个簇之间的簇内距离较小。

我认为某些聚类有效性度量(例如 Davies-Bouldin 指数)将是一个很好的适应度函数,但我正在努力寻找用伪代码或 Java 代码实现的算法。

谁能帮我解决这个问题?

谢谢。

最佳答案

我已经基于https://en.wikipedia.org/wiki/Davies%E2%80%93Bouldin_index用Python实现了它

def davies_bouldin(X, labels, cluster_ctr):
    #get the cluster assignemnts
    clusters = set(labels)
    #get the number of clusters
    num_clusters = len(clusters)
    #array to hold the number of items for each cluster, indexed by cluster number
    num_items_in_clusters = [0]*num_clusters
    #get the number of items for each cluster
    for i in range(len(labels)):
        num_items_in_clusters[labels[i]] += 1
    max_num = -9999
    for i in range(num_clusters):
        s_i = intra_cluster_dist(X, labels, clusters[i], num_items_in_clusters[i], cluster_ctr[i])
    for j in range(num_clusters):
        if(i != j):
            s_j = intra_cluster_dist(X, labels, clusters[j], num_items_in_clusters[j], cluster_ctr[j])
            m_ij = np.linalg.norm(cluster_ctr[clusters[i]]-cluster_ctr[clusters[j]])
            r_ij = (s_i + s_j)/m_ij
            if(r_ij > max_num):
                max_num = r_ij
return max_num

def intra_cluster_dist(X, labels, cluster, num_items_in_cluster, centroid):
    total_dist = 0
    #for every item in cluster j, compute the distance the the center of cluster j, take average
    for k in range(num_items_in_cluster):
        dist = np.linalg.norm(X[labels==cluster]-centroid)
        total_dist = dist + total_dist
return total_dist/num_items_in_cluster

希望对你有帮助

关于java - Java 中的 Davies-Bouldin 指数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4919962/

相关文章:

java - 使用字符串 - 我怎样才能像在真实书籍中那样在上部制作 "small numbers"- 这可能吗?

algorithm - 近似编辑距离树 - 精确编辑路径

algorithm - 最佳聚类算法? (简单解释)

algorithm - 修改 Euler Totient 函数

c++ - 如何确定数据点的两个分区(聚类)是否相同?

r - 用正确的组号标记 R 树状图分支

java - 将动态 JSON 字符串转换为 HashMap

java - 单击按钮更改新 Activity 中的颜色 -android studio

java - Gradle 7.4 构建解决错误 : The server may not support the client's requested TLS protocol versions

java - 插入和删除最大堆java