1 year ago

#325411

test-img

sorour

Finding the nearest point to the cluster centroids in k-means clustering method

I have a set of points in the coordinate system and I am clustering them into 50 clusters using the k-means clustering method. After normalizing coordinates of each point, I find centroids of clusters. Now, I want to choose a data point nearest to the centroid of each cluster. Is there an efficient way to do this? Here is my code:

df = pd.read_excel(r'C:\Users\Sorour\Desktop\CandidatePoint.xlsx', sheet_name='cordinates')
df.dropna(axis=0,how='any',subset=['longitude','latitude'],inplace=True)
# Variable with the Longitude and Latitude
X=df.loc[:,['id','longitude','latitude']]

#using seed in order to prevent different results for clustring
seed = 42
kmeans = KMeans(n_clusters = 50, init ='k-means++', random_state=seed)

X['cluster_label'] = kmeans.fit_predict(X[X.columns[2:3]])
#normalizing 
scaler = MinMaxScaler()

scaler.fit(X[['longitude']])
X['longitude'] = scaler.transform(X[['longitude']])
scaler.fit(X[['latitude']])
X['latitude'] = scaler.transform(X[['latitude']])
#finding centroids of clusters
centers = kmeans.cluster_centers_ # Coordinates of cluster centers.

Thanks

python

k-means

nearest-neighbor

0 Answers

Your Answer

Accepted video resources