I have a satellite image which i converted into a data frame.
I want to perform clustering but find the number of clusters in k means clustering automatically. Below is the python code that I have tried-
# imports
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from joblib import Parallel, delayed
def kMeansRes(scaled_data, k, alpha_k=0.02):
'''
Parameters
----------
scaled_data: matrix
scaled data. rows are samples and columns are features for clustering
k: int
current k for applying KMeans
alpha_k: float
manually tuned factor that gives penalty to the number of clusters
Returns
-------
scaled_inertia: float
scaled inertia value for current k
'''
inertia_o = np.square((scaled_data - np.mean(scaled_data)))
# fit k-means
kmeans = KMeans(n_clusters=k, random_state=0).fit(scaled_data)
scaled_inertia = kmeans.inertia_ / inertia_o + alpha_k * k
return scaled_inertia
def chooseBestKforKMeans(scaled_data, k_range):
ans = []
for k in k_range:
scaled_inertia = kMeansRes(scaled_data, k)
ans.append((k, scaled_inertia))
results = pd.DataFrame(ans, columns = ['k','Scaled Inertia']).set_index('k')
best_k = results.idxmin()[0]
return best_k, results
kMeansRes(df, 4, alpha_k=0.02)
chooseBestKforKMeansParallel(df,range(0,10))
I am getting the below warning and error as- cannot convert float infinity to integer