I am trying to cluster and divide my lat long data into 12 different areas, however the kmeans algorithm is messing up big time. I tried just 2 clusters and it broke so badly (picture attached) it didnt even work well for 12. I know the kmeans is senstive to noise and i cleaned that out as well
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from collections import Counter
df = pd.read_csv("all.csv");
df = df.dropna()
df = df.loc[ ~(df["area"]=="FarEast")]
df["Latitude"] = df["Latitude"].astype(float)
df["Longitude"] = df["Longitude"].astype(float)
df = df.drop(df.nsmallest(4,"Longitude").index)
X=df.loc[:,['Latitude','Longitude']]
X = X.reset_index()
id_n=2
kmeans = KMeans(n_clusters=id_n, random_state=0).fit(X)
id_label=kmeans.labels_
#plot result
ptsymb = np.array(['b.','r.','m.','g.','c.','k.','b*','r*','m*','r^']);
plt.figure(figsize=(12,12))
plt.ylabel('Longitude', fontsize=12)
plt.xlabel('Latitude', fontsize=12)
# import itertools
# marker = itertools.cycle((',', '+', '.', 'o', '*'))
for i in range(id_n):
cluster=np.where(id_label==i)[0]
plt.plot(X.Latitude[cluster].values,X.Longitude[cluster].values,ptsymb[i])
plt.show()