Created
April 24, 2020 10:32
-
-
Save ShayanRiyaz/821b4178b3986cbc658986d6d5f723f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.metrics import silhouette_score | |
la_grouped_clustering = la_grouped.drop('Neighbourhood', 1) | |
for n_cluster in range(2, 12): | |
kmeans = KMeans(n_clusters=n_cluster).fit(la_grouped_clustering) | |
label = kmeans.labels_ | |
sil_coeff = silhouette_score(la_grouped_clustering, label, metric='euclidean') | |
print("For n_clusters={}, The Silhouette Coefficient is {}".format(n_cluster, sil_coeff)) | |
# set number of clusters | |
kclusters = 4 | |
la_grouped_clustering = la_grouped.drop('Neighbourhood', 1) | |
# run k-means clustering | |
kmeans = KMeans(n_clusters=kclusters, random_state=0).fit(la_grouped_clustering) | |
# check cluster labels generated for each row in the data frame | |
kmeans.labels_ | |
# add clustering labels | |
Neighbourhoods_venues_sorted.insert(0, 'Cluster Label', kmeans.labels_.astype(int)) | |
# Neighbourhoods_venues_sorted['Cluster Label']=kmeans.labels_.astype(int) | |
la_merged = nhoods | |
# merge la_grouped with nhoods to add latitude/longitude for each Neighbourhood | |
la_merged = la_merged.join(Neighbourhoods_venues_sorted.set_index('Neighbourhood'), on='Neighbourhood') | |
la_merged.dropna(inplace=True) | |
la_merged['Cluster Label'] = la_merged['Cluster Label'].astype(int) | |
la_merged.head() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment