def runkMeans_sklearn(X, initial_centroids=None, max_iters=0, plot_progress=False, input_K=0): m, n = X.shape if initial_centroids is None: K = input_K else: K = initial_centroids.shape[0] idx = np.zeros((m, 1)) kmeans = None # ============= YOUR CODE HERE ============= # Instructions: Perform K Means with sci-kit library # Initialize with the given points # If initial_centroids is an integer, then use random # =========================================== if kmeans is None: sys.exit('K Means model not initialized') centroids = kmeans.cluster_centers_ idx = kmeans.labels_ if plot_progress: plotProgresskMeans(X, centroids, initial_centroids, idx, K, max_iters) return centroids, idx
def runkMeans_sklearn(X, initial_centroids = None, max_iters= 0, plot_progress = False, input_K = 0): m, n = X.shape if initial_centroids is None: K = input_K else: K = initial_centroids.shape[0] idx = np.zeros((m,1)) kmeans = None # ============= YOUR CODE HERE ============= # Instructions: Perform K Means with sci-kit library # Initialize with the given points # If initial_centroids is an integer, then use random # =========================================== if kmeans is None: sys.exit('K Means model not initialized') centroids = kmeans.cluster_centers_ idx = kmeans.labels_ if plot_progress: plotProgresskMeans(X, centroids, initial_centroids, idx, K, max_iters) return centroids, idx
print(centroids) print('(the centroids should be') print(' [ 2.428301 3.157924 ]') print(' [ 5.813503 2.633656 ]') print(' [ 7.119387 3.616684 ]') raw_input('Program paused. Press enter to continue') # =================== Part 3: K-Means Clustering =================== print('Running K-Means clustering on example dataset.') max_iters = 10 print('K-means starting point') plotProgresskMeans(X, initial_centroids, initial_centroids, idx, K, 0) raw_input('Press enter to continue') centroids, idx = runkMeans_sklearn(X, initial_centroids, max_iters, True) print('K-Means Done.') raw_input('Program paused. Press enter to continue') # =================== Part 4: K-Means Clustering on Pixels =================== print('Running K-Means clustering on pixels from an image.') data_file = '../../data/ex7/bird_small.png' A = mpimg.imread(data_file)
print(centroids); print('(the centroids should be'); print(' [ 2.428301 3.157924 ]'); print(' [ 5.813503 2.633656 ]'); print(' [ 7.119387 3.616684 ]'); raw_input('Program paused. Press enter to continue') # =================== Part 3: K-Means Clustering =================== print('Running K-Means clustering on example dataset.') max_iters = 10 print('K-means starting point') plotProgresskMeans(X, initial_centroids, initial_centroids, idx, K, 0) raw_input('Press enter to continue') centroids, idx = runkMeans_sklearn(X, initial_centroids, max_iters, True) print('K-Means Done.') raw_input('Program paused. Press enter to continue') # =================== Part 4: K-Means Clustering on Pixels =================== print('Running K-Means clustering on pixels from an image.') data_file = '../../data/ex7/bird_small.png' A = mpimg.imread(data_file)