Reputation: 1287
I'm writing a genetic algorithm that tries to chooses a set of the data points to maximize the intercluster distance while keeping the intracluster distance small, between two clusters.
I think some measure of cluster validity such as the Davies-Bouldin index would be a good fitness function, but I am struggling to find an implementation of the algorithm in pseudocode or java code.
Can anybody help me with this?
Thanks.
Upvotes: 1
Views: 2326
Reputation: 377
I have implemented it in Python based on https://en.wikipedia.org/wiki/Davies%E2%80%93Bouldin_index
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
Hope this helps
Upvotes: 1
Reputation: 13789
A quick Google search gave a link to this implemention.This implementation is a part of tinatool (A library for T-invariant related calculations in biological networks at steady state).
Upvotes: 0