def runkMeans(X, initial_centroids, max_iters, plot_progress=False): # Initialize values m, n = X.shape K = initial_centroids.shape[0] centroids = initial_centroids previous_centroids = centroids idx = np.zeros((m, 1)) # Run K-Means for i in range(max_iters): # For each example in X, assign it to the closest centroid idx = findClosestCentroids(X, centroids) # Optionally, plot progress here if plot_progress: plotProgresskMeans(X, centroids, previous_centroids, idx, K, i + 1) previous_centroids = centroids # Given the memberships, compute new centroids centroids = computeCentroids(X, idx, K) plt.show() return centroids, idx
def runkMeans(X, initial_centroids, max_iters, plot_progress=False): # RUNKMEANS runs the K - Means algorithm on data matrix X, where each row of X # is a single example # [centroids, idx] = RUNKMEANS(X, initial_centroids, max_iters, ... # plot_progress) runs the K - Means algorithm on data matrix X, where each # row of X is a single example.It uses initial_centroids used as the # initial centroids.max_iters specifies the total number of interactions # of K - Means to execute.plot_progress is a true / false flag that # indicates if the function should also plot its progress as the # learning happens.This is set to false by default.runkMeans returns # centroids, a Kxn matrix of the computed centroids and idx, a m x 1 # vector of centroid assignments(i.e.each entry in range[1..K]) # Initialize values m, n = np.shape(X) K = np.size(initial_centroids, 0) centroids = initial_centroids previous_centroids = centroids idx = np.zeros((m, 1)) # Run K - Means for i in range(max_iters): print('K-Means iteration #', i, ' out of ', max_iters) # For each example in X, assign it to the closest centroid idx = findClosestCentroids(X, centroids) # Optionally, plot progress here if plot_progress: plotProgresskMeans(X, centroids, previous_centroids, idx, K, i) previous_centroids = centroids # Given the memberships, compute new centroids centroids = computeCentroids(X, idx, K) return centroids, idx
def runkMeans(X, initial_centroids, max_iters, plot_progress=False): """runs the K-Means algorithm on data matrix X, where each row of X is a single example. It uses initial_centroids used as the initial centroids. max_iters specifies the total number of interactions of K-Means to execute. plot_progress is a true/false flag that indicates if the function should also plot its progress as the learning happens. This is set to false by default. runkMeans returns centroids, a Kxn matrix of the computed centroids and idx, a m x 1 vector of centroid assignments (i.e. each entry in range [1..K]) """ # Plot the data if we are plotting progress if plot_progress: fig = plt.figure() ax = plt.gca() # Initialize values m, n = X.shape K = len(initial_centroids) centroids = initial_centroids previous_centroids = centroids idx = np.zeros(m) c = itertools.cycle('012') rgb = np.eye(3) # Run K-Means for i in range(max_iters): # Output progress print('K-Means iteration %d/%d...' % (i, max_iters)) # For each example in X, assign it to the closest centroid idx = findClosestCentroids(X, centroids) # Optionally, plot progress here if plot_progress: color = rgb[int(next(c))] plotProgresskMeans(X, np.array(centroids), np.array(previous_centroids), idx, K, i, color, ax) previous_centroids = centroids show() fig.canvas.draw() # Given the memberships, compute new centroids centroids = computeCentroids(X, idx, K) # Hold off if we are plotting progress if plot_progress: pass # hold off return centroids, idx
def runkMeans(X, initial_centroids, max_iters, plot_progress=False): #RUNKMEANS runs the K-Means algorithm on data matrix X, where each row of X #is a single example # centroids, idx = RUNKMEANS(X, initial_centroids, max_iters, plot_progress=false) # runs the K-Means algorithm on data matrix X, where each # row of X is a single example. It uses initial_centroids used as the # initial centroids. max_iters specifies the total number of interactions # of K-Means to execute. plot_progress is a True/False flag that # indicates if the function should also plot its progress as the # learning happens. This is set to False by default. runkMeans returns # centroids, a K x n matrix of the computed centroids and idx, a vector of # size m of centroid assignments (i.e. each entry in range [1..K]) # # Plot the data if we are plotting progress if plot_progress: fig = figure() hold(True) # Initialize values m, n = shape(X) K = size(initial_centroids, 0) centroids = initial_centroids previous_centroids = centroids idx = zeros(m) # Run K-Means for i in range(max_iters): # Output progress print 'K-Means iteration %d/%d...' % (i+1, max_iters) # For each example in X, assign it to the closest centroid idx = findClosestCentroids(X, centroids) # Optionally, plot progress here if plot_progress: plotProgresskMeans(X, centroids, previous_centroids, idx, K, i) previous_centroids = centroids fig.show() print 'Press enter to continue.' raw_input() # Given the memberships, compute new centroids centroids = computeCentroids(X, idx, K) # Hold off if we are plotting progress if plot_progress: hold(True) return centroids, idx
def runkMeans(X, initial_centroids, max_iters, plot_progress=False): #RUNKMEANS runs the K-Means algorithm on data matrix X, where each row of X #is a single example # [centroids, idx] = RUNKMEANS(X, initial_centroids, max_iters, ... # plot_progress) runs the K-Means algorithm on data matrix X, where each # row of X is a single example. It uses initial_centroids used as the # initial centroids. max_iters specifies the total number of interactions # of K-Means to execute. plot_progress is a true/false flag that # indicates if the function should also plot its progress as the # learning happens. This is set to false by default. runkMeans returns # centroids, a Kxn matrix of the computed centroids and idx, a m x 1 # vector of centroid assignments (i.e. each entry in range [1..K]) # # Initialize values m, n = X.shape K = initial_centroids.shape[0] centroids = initial_centroids previous_centroids = centroids idx = np.zeros((m, 1)) # if plotting, set up the space for interactive graphs # http://stackoverflow.com/a/4098938/583834 # http://matplotlib.org/faq/usage_faq.html#what-is-interactive-mode if plot_progress: plt.close() plt.ion() # Run K-Means for i in range(max_iters): # Output progress sys.stdout.write('\rK-Means iteration {:d}/{:d}...'.format(i+1, max_iters)) sys.stdout.flush() # For each example in X, assign it to the closest centroid idx = findClosestCentroids(X, centroids) # Optionally, plot progress here if plot_progress: plotProgresskMeans(X, centroids, previous_centroids, idx, K, i) previous_centroids = centroids input('Program paused. Press <Enter> to continue...') # Given the memberships, compute new centroids centroids = computeCentroids(X, idx, K) # Hold off if we are plotting progress print('\n') return centroids, idx
def runkMeans(X, initial_centroids, max_iters, plot_progress=False): #RUNKMEANS runs the K-Means algorithm on data matrix X, where each row of X #is a single example # centroids, idx = RUNKMEANS(X, initial_centroids, max_iters, plot_progress=false) # runs the K-Means algorithm on data matrix X, where each # row of X is a single example. It uses initial_centroids used as the # initial centroids. max_iters specifies the total number of interactions # of K-Means to execute. plot_progress is a True/False flag that # indicates if the function should also plot its progress as the # learning happens. This is set to False by default. runkMeans returns # centroids, a K x n matrix of the computed centroids and idx, a vector of # size m of centroid assignments (i.e. each entry in range [1..K]) # # Plot the data if we are plotting progress if plot_progress: fig = figure() hold(True) # Initialize values m, n = shape(X) K = size(initial_centroids, 0) centroids = initial_centroids previous_centroids = centroids idx = zeros(m) # Run K-Means for i in range(max_iters): # Output progress print 'K-Means iteration %d/%d...' % (i + 1, max_iters) # For each example in X, assign it to the closest centroid idx = findClosestCentroids(X, centroids) # Optionally, plot progress here if plot_progress: plotProgresskMeans(X, centroids, previous_centroids, idx, K, i) previous_centroids = centroids fig.show() print 'Press enter to continue.' raw_input() # Given the memberships, compute new centroids centroids = computeCentroids(X, idx, K) # Hold off if we are plotting progress if plot_progress: hold(True) return centroids, idx
def runkMeans(X, initial_centroids, max_iters, plot_progress=False): """runs the K-Means algorithm on data matrix X, where each row of X is a single example. It uses initial_centroids used as the initial centroids. max_iters specifies the total number of interactions of K-Means to execute. plot_progress is a true/false flag that indicates if the function should also plot its progress as the learning happens. This is set to false by default. runkMeans returns centroids, a Kxn matrix of the computed centroids and idx, a m x 1 vector of centroid assignments (i.e. each entry in range [1..K]) """ # Plot the data if we are plotting progress if plot_progress: plt.figure() # Initialize values m, n = X.shape K = len(initial_centroids) centroids = initial_centroids previous_centroids = centroids idx = np.zeros(m) c = itertools.cycle('012') rgb = np.eye(3) # Run K-Means for i in range(max_iters): # Output progress print 'K-Means iteration %d/%d...' % (i, max_iters) # For each example in X, assign it to the closest centroid _, idx = findClosestCentroids(X, centroids) # Optionally, plot progress here if plot_progress: color = rgb[int(next(c))] plotProgresskMeans(X, np.array(centroids), np.array(previous_centroids), idx, K, i, color) previous_centroids = centroids # raw_input("Press Enter to continue...") # Given the memberships, compute new centroids centroids = computeCentroids(X, idx, K) # Hold off if we are plotting progress if plot_progress: pass # hold off return centroids, idx
def runkMeans(name, X, initial_centroids, max_iters, plot_progress=False): #RUNKMEANS runs the K-Means algorithm on data matrix X, where each row of X #is a single example # [centroids, idx] = RUNKMEANS(X, initial_centroids, max_iters, ... # plot_progress) runs the K-Means algorithm on data matrix X, where each # row of X is a single example. It uses initial_centroids used as the # initial centroids. max_iters specifies the total number of interactions # of K-Means to execute. plot_progress is a true/false flag that # indicates if the function should also plot its progress as the # learning happens. This is set to false by default. runkMeans returns # centroids, a Kxn matrix of the computed centroids and idx, a m x 1 # vector of centroid assignments (i.e. each entry in range [1..K]) # # Initialize values m, n = X.shape K = initial_centroids.shape[0] centroids = initial_centroids previous_centroids = [] idx = None idx_history = [] # Run K-Means for i in range(max_iters): # Output progress print('K-Means iteration %d/%d...' % (i, max_iters)) # For each example in X, assign it to the closest centroid idx = findClosestCentroids(X, centroids) idx_history.append(idx) previous_centroids.append(centroids) # Optionally, plot progress here if plot_progress: fig = plt.figure() plotProgresskMeans(X, centroids, previous_centroids, idx_history, K, i) plt.savefig('figure%s.%03d.png' % (name, i)) # Given the memberships, compute new centroids centroids = computeCentroids(X, idx, K) #end #end return (centroids, idx)
def runkMeans(X, initial_centroids, max_iters, plot_progress=False): ''' [centroids, idx] = RUNKMEANS(X, initial_centroids, max_iters, ... plot_progress) runs the K-Means algorithm on data matrix X, where each row of X is a single example. It uses initial_centroids used as the initial centroids. max_iters specifies the total number of interactions of K-Means to execute. plot_progress is a true/false flag that indicates if the function should also plot its progress as the learning happens. This is set to false by default. runkMeans returns centroids, a Kxn matrix of the computed centroids and idx, a m x 1 vector of centroid assignments (i.e. each entry in range [1..K]) ''' import numpy as np from findClosestCentroids import findClosestCentroids from plotProgresskMeans import plotProgresskMeans from computeCentroids import computeCentroids import matplotlib.pyplot as plt # Initialize values m, _ = np.shape(X) K = np.size(initial_centroids, 0) centroids = initial_centroids acc_centroids = centroids idx = np.zeros((m, 1)) # Run K-Means for i in range(max_iters): # Output progress print('K-Means iteration %d/%d...\n' % (i, max_iters)) # For each example in X, assign it to the closest centroid idx = findClosestCentroids(X, centroids) # Optionally, plot progress here if plot_progress: plotProgresskMeans(X, acc_centroids, idx, K, i) plt.show() # Given the memberships, compute new centroids centroids = computeCentroids(X, idx, K) acc_centroids = np.append(acc_centroids, centroids, axis=0) return centroids, idx
def runkMeans(X, initial_centroids, max_iters, plot_progress=False): # Initialize values m, n = X.shape K = initial_centroids.shape[0] centroids = initial_centroids previous_centroids = centroids idx = np.zeros((m, 1)) # Run K-Means for i in range(1, max_iters + 1): print('K-Means iteration %d/%d' % (i, max_iters)) idx = findClosestCentroids(X, centroids) if plot_progress: plotProgresskMeans(X, centroids, previous_centroids, idx, K, i) previous_centroids = centroids centroids = computeCentroids(X, idx, K) return centroids, idx
def runkMeans(X, initial_centroids, max_iters, plot_progress=None): # Set default value for plot progress if plot_progress == None: plot_progress = False # Plot the data if we are plotting progress if plot_progress: plt.figure() # Initialize values m = X.shape[0] n = X.shape[1] K = initial_centroids.shape[0] centroids = initial_centroids previous_centroids = centroids idx = np.zeros(m) idx = idx.astype(int) # Run K-Means for i in range(max_iters): # Output progress print('K-Means iteration %d/%d...\n' % (i, max_iters)) # For each example in X, assign it to the closest centroid idx = findClosestCentroids(X, centroids) # For each example in X, assign it to the closest centroid if plot_progress: plotProgresskMeans(X, centroids, previous_centroids, idx, K, i) previous_centroids = centroids.copy() input('Press enter to continue.\n') # Given the memberships, compute new centroids centroids = computeCentroids(X, idx, K) return centroids, idx
def runkMeans(X, initial_centroids, max_iters, plot_progress = False): # RUNKMEANS runs the K - Means algorithm on data matrix X, where each row of X # is a single example # [centroids, idx] = RUNKMEANS(X, initial_centroids, max_iters, ... # plot_progress) runs the K - Means algorithm on data matrix X, where each # row of X is a single example.It uses initial_centroids used as the # initial centroids.max_iters specifies the total number of interactions # of K - Means to execute.plot_progress is a true / false flag that # indicates if the function should also plot its progress as the # learning happens.This is set to false by default.runkMeans returns # centroids, a Kxn matrix of the computed centroids and idx, a m x 1 # vector of centroid assignments(i.e.each entry in range[1..K]) # Initialize values m, n = np.shape(X) K = np.size(initial_centroids, 0) centroids = initial_centroids previous_centroids = centroids idx = np.zeros((m, 1)) # Run K - Means for i in range(max_iters): print('K-Means iteration #', i,' out of ', max_iters) # For each example in X, assign it to the closest centroid idx = findClosestCentroids(X, centroids) # Optionally, plot progress here if plot_progress: plotProgresskMeans(X, centroids, previous_centroids, idx, K, i) previous_centroids = centroids # Given the memberships, compute new centroids centroids = computeCentroids(X, idx, K) return centroids, idx
def runkMeans(X, initial_centroids, max_iters, plot_progress=False): #RUNKMEANS runs the K-Means algorithm on data matrix X, where each row of X #is a single example # [centroids, idx] = RUNKMEANS(X, initial_centroids, max_iters, ... # plot_progress) runs the K-Means algorithm on data matrix X, where each # row of X is a single example. It uses initial_centroids used as the # initial centroids. max_iters specifies the total number of interactions # of K-Means to execute. plot_progress is a true/false flag that # indicates if the function should also plot its progress as the # learning happens. This is set to false by default. runkMeans returns # centroids, a Kxn matrix of the computed centroids and idx, a m x 1 # vector of centroid assignments (i.e. each entry in range [1..K]) # # Set default value for plot progress # (commented out due to pythonic default parameter assignment above) # if not plot_progress: # plot_progress = False # Plot the data if we are plotting progress # if plot_progress: # plt.hold(True) # Initialize values m, n = X.shape K = initial_centroids.shape[0] centroids = initial_centroids previous_centroids = centroids idx = np.zeros((m, 1)) # if plotting, set up the space for interactive graphs # http://stackoverflow.com/a/4098938/583834 # http://matplotlib.org/faq/usage_faq.html#what-is-interactive-mode if plot_progress: plt.close() plt.ion() # Run K-Means for i in xrange(max_iters): # Output progress sys.stdout.write('\rK-Means iteration {:d}/{:d}...'.format(i+1, max_iters)) sys.stdout.flush() # For each example in X, assign it to the closest centroid idx = fcc.findClosestCentroids(X, centroids) # Optionally, plot progress here if plot_progress: ppkm.plotProgresskMeans(X, centroids, previous_centroids, idx, K, i) previous_centroids = centroids raw_input('Press enter to continue.') # Given the memberships, compute new centroids centroids = cc.computeCentroids(X, idx, K) # Hold off if we are plotting progress print('\n') # if plot_progress: # plt.hold(False) return centroids, idx