Skip to content

Instantly share code, notes, and snippets.

@alinazhanguwo
Created January 6, 2020 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alinazhanguwo/7b77704de7aba9cba70add46aa56e06e to your computer and use it in GitHub Desktop.
Save alinazhanguwo/7b77704de7aba9cba70add46aa56e06e to your computer and use it in GitHub Desktop.
# set up the parameters
n_init = 12
max_iter = 225
tol = 0.0001
random_state = 42
n_jobs = -1
n_clusters = 3
t0 = dt.now()
print("========= Start training ... ")
# Initialize the clusterer with n_clusters value and a random generator
# seed of 42 for reproducibility.
clusterer = KMeans(n_clusters=n_clusters, random_state=random_state)
cluster_labels = clusterer.fit_predict(X_train)
# inertia
inertia = clusterer.inertia_
# The silhouette_score gives the average value for all the samples.
# This gives a perspective into the density and separation of the formed
# clusters
silhouette_avg = silhouette_score(X_train, cluster_labels)
# Compute the silhouette scores for each sample
sample_silhouette_values = silhouette_samples(X, cluster_labels)
print("For n_clusters =", n_clusters,
", the inertia is :", inertia,
", the average silhouette_score is :", silhouette_avg)
t1 = dt.now()-t0
print("========= Finished in ",t1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment