1
$\begingroup$

I am trying to calculate BIC in python. In python, there is no inbuilt library for computing BIC. I referenced the following link to compute variance and BIC further:- Using BIC to estimate the number of k in KMEANS The variance formula given in the highest voted answer is not working for my code design as centers is a two-dimensional array and in my case it's a single dimensional array. So I converted the variance formula given in the link to calculate but I learned it's not the correct way. Here is my python code:-

def compute_variance_grayscale(self, clusters, centroids, gImage, k):
    # compute variance
    variance = 0

    for i in range(len(clusters)): #iterate over the available clusters
        cluster = clusters[i]
        sum = 0;
        centroid = centroids[i]
        for j in cluster: #for each data point in cluster j compute the sum
            sum += (gImage.getImageValueAt(j[0], j[1]) - centroid) ** 2

        variance += sum / (len(cluster) - k); #add the sum to the given variance

    return variance

        def compute_variance_rgb(self, clusters, centroids, cImage, k):
    # compute variance
    variance = 0
    for i in range(len(clusters)):
        cluster = clusters[i]
        sum = 0;
        centroid = centroids[i]
        for j in cluster:
            b,g,r = cImage.getImageValueAt(j[0], j[1])
            b_e = (b - centroid[0]) ** 2
            g_e = (g - centroid[1]) ** 2
            r_e = (r - centroid[2]) ** 2

            sum += (b_e + g_e + r_e)

        variance += sum / (len(cluster) - k);

    return variance

Can anyone please point to the right way to calculate variance? Also, it would be great if someone could explain to me the computation of BIC and use of const terms as mentioned in the highest voted answer in the above link:-

np.sum([n[i] * np.log(n[i]) -
               n[i] * np.log(N) -
             ((n[i] * d) / 2) * np.log(2*np.pi*cl_var) -
             ((n[i] - 1) * d/ 2) for i in range(m)]) - const_term
$\endgroup$
2
  • $\begingroup$ I would look into the pdfs and original paper $\endgroup$
    – Chris
    Commented Jul 11, 2018 at 16:04
  • $\begingroup$ The BIC is based on standard likelihood theory, which doesn't apply to k-means (as k-means estimates cluster memberships on top of the cluster means, which invalidates standard likelihood theory). So it is in fact not designed to be used with k-means. It can be adapted, but there is more than one way of doing this, and to my knowledge no such adaptation has a theoretical basis. $\endgroup$ Commented Aug 17, 2022 at 14:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.