def plotProgresskMeans(X, centroids, previous, idx, K, i, color): """plots the data points with colors assigned to each centroid. With the previous centroids, it also plots a line between the previous locations and current locations of the centroids. """ # Plot the examples plotDataPoints(X, idx) # Plot the centroids as black x's plt.scatter(centroids[:, 0], centroids[:, 1], marker='x', s=60, lw=3, edgecolor='k') # Plot the history of the centroids with lines for j in range(len(centroids)): plt.plot([centroids[j, 0], previous[j, 0]], [centroids[j, 1], previous[j, 1]], c=color) # Title plt.title('Iteration number %d' % i) show() raw_input("Program paused. Press Enter to continue...")
def displayData(X): """displays 2D data stored in X in a nice grid. It returns the figure handle h and the displayed array if requested.""" # Compute rows, cols m, n = X.shape example_width = round(np.sqrt(n)) example_height = (n / example_width) # Compute number of items to display display_rows = np.floor(np.sqrt(m)) display_cols = np.ceil(m / display_rows) # Between images padding pad = 1 # Setup blank display display_array = -np.ones( (pad + display_rows * (example_height + pad), pad + display_cols * (example_width + pad))) # Copy each example into a patch on the display array curr_ex = 0 for j in np.arange(display_rows): for i in np.arange(display_cols): if curr_ex > m: break # Get the max value of the patch max_val = np.max(np.abs(X[curr_ex, :])) rows = [ pad + j * (example_height + pad) + x for x in np.arange(example_height + 1) ] cols = [ pad + i * (example_width + pad) + x for x in np.arange(example_width + 1) ] display_array[min(rows):max(rows), min(cols):max(cols)] = X[curr_ex, :].reshape( example_height, example_width) / max_val curr_ex = curr_ex + 1 if curr_ex > m: break # Display Image display_array = display_array.astype('float32') plt.imshow(display_array.T) plt.set_cmap('gray') # Do not show axis plt.axis('off') show()
def visualizeFit(X, mu, sigma2): """ This visualization shows you the probability density function of the Gaussian distribution. Each example has a location (x1, x2) that depends on its feature values. """ n = np.linspace(0, 35, 71) X1 = np.meshgrid(n, n) Z = multivariateGaussian( np.column_stack((X1[0].T.flatten(), X1[1].T.flatten())), mu, sigma2) Z = Z.reshape(X1[0].shape) plt.plot(X[:, 0], X[:, 1], 'bx') # Do not plot if there are infinities if not isinf(np.sum(Z)): plt.contour(X1[0], X1[1], Z, 10.0**np.arange(-20, 0, 3).T) show()
def plotDataPoints(X, idx): """plots data points in X, coloring them so that those with the same index assignments in idx have the same color """ pass # Create palette # palette = hsv(K + 1) # colors = palette(idx, :) # # # Plot the data # c = dict(enumerate(np.eye(3))) # colors=idx map = plt.get_cmap("jet") idxn = idx.astype('float') / max(idx.astype('float')) colors = map(idxn) plt.scatter(X[:, 0], X[:, 1], 15, edgecolors=colors, marker='o', facecolors='none', lw=0.5) show()
# Y is a 1682x943 matrix, containing ratings (1-5) of 1682 movies on # 943 users # # R is a 1682x943 matrix, where R(i,j) = 1 if and only if user j gave a # rating to movie i # From the matrix, we can compute statistics like average rating. print('Average rating for movie 1 (Toy Story): %f / 5' % np.mean(Y[0, R[0, :]])) # We can "visualize" the ratings matrix by plotting it with imagesc plt.figure() plt.imshow(Y, aspect='equal', origin='upper', extent=(0, Y.shape[1], 0, Y.shape[0]/2.0)) plt.ylabel('Movies') plt.xlabel('Users') show() input("Program paused. Press Enter to continue...") ## ============ Part 2: Collaborative Filtering Cost Function =========== # You will now implement the cost function for collaborative filtering. # To help you debug your cost function, we have included set of weights # that we trained on that. Specifically, you should complete the code in # cofiCostFunc.m to return J. # Load pre-trained weights (X, Theta, num_users, num_movies, num_features) data = scipy.io.loadmat('ex8_movieParams.mat') X = data['X'] Theta = data['Theta'] num_users = data['num_users'] num_movies = data['num_movies']