Calculate Akaike Information Criteria (AIC) by hand in Python

Viewed 15837

As far as I know, there is no AIC package in Python. Therefore, I am trying to calculate it by hand to find the optimal number of clusters in my dataset (I'm using K-means for clustering)

I'm following the equation on Wiki:

AIC = 2k - 2ln(maximum likelihood)

Below is my current code:

range_n_clusters = range(2, 10)
for n_clusters in range_n_clusters:
    model = cluster.KMeans(n_clusters=n_clusters, init='k-means++', n_init=10, max_iter=300, tol=0.0001,
                           precompute_distances='auto', verbose=0, random_state=None, copy_x=True, n_jobs=1)
    model.fit(X)
    centers = model.cluster_centers_
    labels = model.labels_
    likelihood = ?????
    aic = 2 * len(X.columns) - 2 * likelihood
    print(aic)

Any pointers on how to calculate the likelihood value?

// UPDATED: Using Gaussian Mixture Model to calculate AIC:

enter image description here

Isn't it supposed to look like a curve? (instead of a straight line)

My plotting code:

def aic(X):
  range_n_clusters = range(2, 10)
  aic_list = []
  for n_clusters in range_n_clusters:
     model = mixture.GaussianMixture(n_components=n_clusters, init_params='kmeans')
     model.fit(X)
     aic_list.append(model.aic(X))
  plt.plot(range_n_clusters, aic_list, marker='o')
  plt.show()
1 Answers
Related