Created
June 17, 2019 05:55
-
-
Save khyatimahendru/6bf974c16bb9ce2bb8e8deedac03b01d to your computer and use it in GitHub Desktop.
This file contains hidden or 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.cluster import KMeans | |
| # function returns WSS score for k values from 1 to kmax | |
| def calculate_WSS(points, kmax): | |
| sse = [] | |
| for k in range(1, kmax+1): | |
| kmeans = KMeans(n_clusters = k).fit(points) | |
| centroids = kmeans.cluster_centers_ | |
| pred_clusters = kmeans.predict(points) | |
| curr_sse = 0 | |
| # calculate square of Euclidean distance of each point from its cluster center and add to current WSS | |
| for i in range(len(points)): | |
| curr_center = centroids[pred_clusters[i]] | |
| curr_sse += (points[i, 0] - curr_center[0]) ** 2 + (points[i, 1] - curr_center[1]) ** 2 | |
| sse.append(curr_sse) | |
| return sse |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment