def output(partId): # Random Test Cases X = np.stack([ np.ones(20), np.exp(1) * np.sin(np.arange(1, 21)), np.exp(0.5) * np.cos(np.arange(1, 21)) ], axis=1) y = (np.sin(X[:, 0] + X[:, 1]) > 0).astype(float) Xm = np.array([[-1, -1], [-1, -2], [-2, -1], [-2, -2], [1, 1], [1, 2], [2, 1], [2, 2], [-1, 1], [-1, 2], [-2, 1], [-2, 2], [1, -1], [1, -2], [-2, -1], [-2, -2]]) ym = np.array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]) t1 = np.sin(np.reshape(np.arange(1, 25, 2), (4, 3), order='F')) t2 = np.cos(np.reshape(np.arange(1, 41, 2), (4, 5), order='F')) if partId == '1': J, grad = lrCostFunction(np.array([0.25, 0.5, -0.5]), X, y, 0.1) out = formatter('%0.5f ', J) out += formatter('%0.5f ', grad) elif partId == '2': out = formatter('%0.5f ', oneVsAll(Xm, ym, 4, 0.1)) elif partId == '3': out = formatter('%0.5f ', predictOneVsAll(t1, Xm)) elif partId == '4': out = formatter('%0.5f ', predict(t1, t2, Xm)) return out
def output(partId): # Random Test Cases x1 = np.sin(np.arange(1, 11)) x2 = np.cos(np.arange(1, 11)) ec = 'the quick brown fox jumped over the lazy dog' wi = np.abs(np.round(x1 * 1863)).astype(int) wi = np.concatenate([wi, wi]) if partId == '1': sim = gaussianKernel(x1, x2, 2) out = formatter('%0.5f ', sim) elif partId == '2': mat = scipy.io.loadmat('ex6data3.mat') X = mat['X'] y = mat['y'].ravel() Xval = mat['Xval'] yval = mat['yval'].ravel() C, sigma = dataset3Params(X, y, Xval, yval) out = formatter('%0.5f ', C) out += formatter('%0.5f ', sigma) elif partId == '3': word_indices = processEmail(ec) + 1 out = formatter('%d ', word_indices) elif partId == '4': x = emailFeatures(wi) out = formatter('%d ', x) return out
def output(partId): # Random Test Cases X = np.reshape(3 * np.sin(np.arange(1, 31)), (3, 10), order='F') Xm = np.reshape(np.sin(np.arange(1, 33)), (16, 2), order='F') / 5 ym = 1 + np.arange(1, 17) % 4 t1 = np.sin(np.reshape(np.arange(1, 25, 2), (4, 3), order='F')) t2 = np.cos(np.reshape(np.arange(1, 41, 2), (4, 5), order='F')) t = np.concatenate([t1.ravel(), t2.ravel()], axis=0) if partId == '1': J, _ = nnCostFunction(t, 2, 4, 4, Xm, ym, 0) out = formatter('%0.5f ', J) elif partId == '2': J, _ = nnCostFunction(t, 2, 4, 4, Xm, ym, 1.5) out = formatter('%0.5f ', J) elif partId == '3': out = formatter('%0.5f ', sigmoidGradient(X)) elif partId == '4': J, grad = nnCostFunction(t, 2, 4, 4, Xm, ym, 0) out = formatter('%0.5f ', J) out += formatter('%0.5f ', grad) elif partId == '5': J, grad = nnCostFunction(t, 2, 4, 4, Xm, ym, 1.5) out = formatter('%0.5f ', J) out += formatter('%0.5f ', grad) return out
def output(partId): # Random Test Cases X1 = np.column_stack( (np.ones(20), np.exp(1) + np.exp(2) * np.linspace(0.1, 2, 20))) Y1 = X1[:, 1] + np.sin(X1[:, 0]) + np.cos(X1[:, 1]) X2 = np.column_stack((X1, X1[:, 1]**0.5, X1[:, 1]**0.25)) Y2 = np.power(Y1, 0.5) + Y1 if partId == '1': out = formatter('%0.5f ', warmUpExercise()) elif partId == '2': out = formatter('%0.5f ', computeCost(X1, Y1, np.array([0.5, -0.5]))) elif partId == '3': out = formatter( '%0.5f ', gradientDescent(X1, Y1, np.array([0.5, -0.5]), 0.01, 10)) elif partId == '4': out = formatter('%0.5f ', featureNormalize(X2[:, 1:4])) elif partId == '5': out = formatter( '%0.5f ', computeCostMulti(X2, Y2, np.array([0.1, 0.2, 0.3, 0.4]))) elif partId == '6': out = formatter( '%0.5f ', gradientDescentMulti(X2, Y2, np.array([-0.1, -0.2, -0.3, -0.4]), 0.01, 10)) elif partId == '7': out = formatter('%0.5f ', normalEqn(X2, Y2)) return out
def output(partId): # Random Test Cases X = np.stack([ np.ones(20), np.exp(1) * np.sin(np.arange(1, 21)), np.exp(0.5) * np.cos(np.arange(1, 21)) ], axis=1) y = np.reshape((np.sin(X[:, 0] + X[:, 1]) > 0).astype(float), (20, 1)) if partId == '1': out = formatter('%0.5f ', sigmoid(X)) elif partId == '2': out = formatter( '%0.5f ', costFunction(np.reshape(np.array([0.25, 0.5, -0.5]), (3, 1)), X, y)) elif partId == '3': cost, grad = costFunction( np.reshape(np.array([0.25, 0.5, -0.5]), (3, 1)), X, y) out = formatter('%0.5f ', grad) elif partId == '4': out = formatter( '%0.5f ', predict(np.reshape(np.array([0.25, 0.5, -0.5]), (3, 1)), X)) elif partId == '5': out = formatter( '%0.5f ', costFunctionReg(np.reshape(np.array([0.25, 0.5, -0.5]), (3, 1)), X, y, 0.1)) elif partId == '6': cost, grad = costFunctionReg( np.reshape(np.array([0.25, 0.5, -0.5]), (3, 1)), X, y, 0.1) out = formatter('%0.5f ', grad) return out
def output(partId): # Random Test Cases X = np.vstack([ np.ones(10), np.sin(np.arange(1, 15, 1.5)), np.cos(np.arange(1, 15, 1.5)) ]).T y = np.sin(np.arange(1, 31, 3)) Xval = np.vstack([ np.ones(10), np.sin(np.arange(0, 14, 1.5)), np.cos(np.arange(0, 14, 1.5)) ]).T yval = np.sin(np.arange(1, 11)) if partId == '1': J, _ = linearRegCostFunction(X, y, np.array([0.1, 0.2, 0.3]), 0.5) out = formatter('%0.5f ', J) elif partId == '2': J, grad = linearRegCostFunction(X, y, np.array([0.1, 0.2, 0.3]), 0.5) out = formatter('%0.5f ', grad) elif partId == '3': error_train, error_val = learningCurve(X, y, Xval, yval, 1) out = formatter( '%0.5f ', np.concatenate([error_train.ravel(), error_val.ravel()])) elif partId == '4': X_poly = polyFeatures(X[1, :].T, 8) out = formatter('%0.5f ', X_poly) elif partId == '5': lambda_vec, error_train, error_val = validationCurve(X, y, Xval, yval) out = formatter( '%0.5f ', np.concatenate( [lambda_vec.ravel(), error_train.ravel(), error_val.ravel()])) return out
def output(partId): # Random Test Cases X = np.sin(np.arange(1, 166)).reshape(15, 11, order='F') Z = np.cos(np.arange(1, 122)).reshape(11, 11, order='F') C = Z[:5, :] idx = np.arange(1, 16) % 3 if partId == '1': idx = findClosestCentroids(X, C) + 1 out = formatter('%0.5f ', idx.ravel('F')) elif partId == '2': centroids = computeCentroids(X, idx, 3) out = formatter('%0.5f ', centroids.ravel('F')) elif partId == '3': U, S = pca(X) out = formatter( '%0.5f ', np.abs(np.hstack([U.ravel('F'), np.diag(S).ravel('F')]))) elif partId == '4': X_proj = projectData(X, Z, 5) out = formatter('%0.5f ', X_proj.ravel('F')) elif partId == '5': X_rec = recoverData(X[:, :5], Z, 5) out = formatter('%0.5f ', X_rec.ravel('F')) return out
def output(partId): # Random Test Cases n_u = 3 n_m = 4 n = 5 X = np.sin(np.arange(1, 1 + n_m * n)).reshape(n_m, n, order='F') Theta = np.cos(np.arange(1, 1 + n_u * n)).reshape(n_u, n, order='F') Y = np.sin(np.arange(1, 1 + 2 * n_m * n_u, 2)).reshape(n_m, n_u, order='F') R = Y > 0.5 pval = np.concatenate([abs(Y.ravel('F')), [0.001], [1]]) Y = Y * R yval = np.concatenate([R.ravel('F'), [1], [0]]) params = np.concatenate([X.ravel(), Theta.ravel()]) if partId == '1': mu, sigma2 = estimateGaussian(X) out = formatter('%0.5f ', mu.ravel()) out += formatter('%0.5f ', sigma2.ravel()) elif partId == '2': bestEpsilon, bestF1 = selectThreshold(yval, pval) out = formatter('%0.5f ', bestEpsilon.ravel()) out += formatter('%0.5f ', bestF1.ravel()) elif partId == '3': J, _ = cofiCostFunc(params, Y, R, n_u, n_m, n, 0) out = formatter('%0.5f ', J.ravel()) elif partId == '4': J, grad = cofiCostFunc(params, Y, R, n_u, n_m, n, 0) X_grad = grad[:n_m * n].reshape(n_m, n) Theta_grad = grad[n_m * n:].reshape(n_u, n) out = formatter( '%0.5f ', np.concatenate([X_grad.ravel('F'), Theta_grad.ravel('F')])) elif partId == '5': J, _ = cofiCostFunc(params, Y, R, n_u, n_m, n, 1.5) out = formatter('%0.5f ', J.ravel()) elif partId == '6': J, grad = cofiCostFunc(params, Y, R, n_u, n_m, n, 1.5) X_grad = grad[:n_m * n].reshape(n_m, n) Theta_grad = grad[n_m * n:].reshape(n_u, n) out = formatter( '%0.5f ', np.concatenate([X_grad.ravel('F'), Theta_grad.ravel('F')])) return out
def ex7(): ## Machine Learning Online Class # Exercise 7 | Principle Component Analysis and K-Means Clustering # # Instructions # ------------ # # This file contains code that helps you get started on the # exercise. You will need to complete the following functions: # # pca.m # projectData.m # recoverData.m # computeCentroids.m # findClosestCentroids.m # kMeansInitCentroids.m # # For this exercise, you will not need to change any code in this file, # or any other files other than those mentioned above. # ## Initialization #clear ; close all; clc ## ================= Part 1: Find Closest Centroids ==================== # To help you implement K-Means, we have divided the learning algorithm # into two functions -- findClosestCentroids and computeCentroids. In this # part, you shoudl complete the code in the findClosestCentroids function. # print('Finding closest centroids.\n') # Load an example dataset that we will be using mat = scipy.io.loadmat('ex7data2.mat') X = mat['X'] # Select an initial set of centroids K = 3 # 3 Centroids initial_centroids = np.array([[3, 3], [6, 2], [8, 5]]) # Find the closest centroids for the examples using the # initial_centroids idx = findClosestCentroids(X, initial_centroids) print('Closest centroids for the first 3 examples: ') print(formatter(' %d', idx[:3] + 1)) print('\n(the closest centroids should be 1, 3, 2 respectively)') print('Program paused. Press enter to continue.') #pause ## ===================== Part 2: Compute Means ========================= # After implementing the closest centroids function, you should now # complete the computeCentroids function. # print('\nComputing centroids means.\n') # Compute means based on the closest centroids found in the previous part. centroids = computeCentroids(X, idx, K) print('Centroids computed after initial finding of closest centroids: ') print(centroids) print('\n(the centroids should be') print(' [ 2.428301 3.157924 ]') print(' [ 5.813503 2.633656 ]') print(' [ 7.119387 3.616684 ]\n') print('Program paused. Press enter to continue.') #pause ## =================== Part 3: K-Means Clustering ====================== # After you have completed the two functions computeCentroids and # findClosestCentroids, you have all the necessary pieces to run the # kMeans algorithm. In this part, you will run the K-Means algorithm on # the example dataset we have provided. # print('\nRunning K-Means clustering on example dataset.\n') # Load an example dataset mat = scipy.io.loadmat('ex7data2.mat') X = mat['X'] # Settings for running K-Means K = 3 max_iters = 10 # For consistency, here we set centroids to specific values # but in practice you want to generate them automatically, such as by # settings them to be random examples (as can be seen in # kMeansInitCentroids). initial_centroids = np.array([[3, 3], [6, 2], [8, 5]]) # Run K-Means algorithm. The 'true' at the end tells our function to plot # the progress of K-Means centroids, idx = runkMeans('1', X, initial_centroids, max_iters, True) print('\nK-Means Done.\n') print('Program paused. Press enter to continue.') #pause ## ============= Part 4: K-Means Clustering on Pixels =============== # In this exercise, you will use K-Means to compress an image. To do this, # you will first run K-Means on the colors of the pixels in the image and # then you will map each pixel on to it's closest centroid. # # You should now complete the code in kMeansInitCentroids.m # print('\nRunning K-Means clustering on pixels from an image.\n') # Load an image of a bird A = matplotlib.image.imread('bird_small.png') # If imread does not work for you, you can try instead # load ('bird_small.mat') A = A / 255 # Divide by 255 so that all values are in the range 0 - 1 # Size of the image #img_size = size(A) # Reshape the image into an Nx3 matrix where N = number of pixels. # Each row will contain the Red, Green and Blue pixel values # This gives us our dataset matrix X that we will use K-Means on. X = A.reshape(-1, 3) # Run your K-Means algorithm on this data # You should try different values of K and max_iters here K = 16 max_iters = 10 # When using K-Means, it is important the initialize the centroids # randomly. # You should complete the code in kMeansInitCentroids.m before proceeding initial_centroids = kMeansInitCentroids(X, K) # Run K-Means centroids, idx = runkMeans('2', X, initial_centroids, max_iters) print('Program paused. Press enter to continue.') #pause ## ================= Part 5: Image Compression ====================== # In this part of the exercise, you will use the clusters of K-Means to # compress an image. To do this, we first find the closest clusters for # each example. After that, we print('\nApplying K-Means to compress an image.\n') # Find closest cluster members idx = findClosestCentroids(X, centroids) # Essentially, now we have represented the image X as in terms of the # indices in idx. # We can now recover the image from the indices (idx) by mapping each pixel # (specified by it's index in idx) to the centroid value X_recovered = centroids[idx,:].reshape(A.shape) # Reshape the recovered image into proper dimensions X_recovered = X_recovered.reshape(A.shape) fig, ax = plt.subplots(1, 2, figsize=(8, 4)) # Display the original image ax[0].imshow(A * 255) ax[0].grid(False) ax[0].set_title('Original') # Display compressed image side by side ax[1].imshow(X_recovered * 255) ax[1].grid(False) ax[1].set_title('Compressed, with %d colors' % K) plt.savefig('figure3.png') print('Program paused. Press enter to continue.\n')
def ex7_pca(): ## Machine Learning Online Class # Exercise 7 | Principle Component Analysis and K-Means Clustering # # Instructions # ------------ # # This file contains code that helps you get started on the # exercise. You will need to complete the following functions: # # pca.m # projectData.m # recoverData.m # computeCentroids.m # findClosestCentroids.m # kMeansInitCentroids.m # # For this exercise, you will not need to change any code in this file, # or any other files other than those mentioned above. # ## Initialization #clear ; close all; clc ## ================== Part 1: Load Example Dataset =================== # We start this exercise by using a small dataset that is easily to # visualize # print('Visualizing example dataset for PCA.\n') # The following command loads the dataset. You should now have the # variable X in your environment mat = scipy.io.loadmat('ex7data1.mat') X = mat['X'] # Visualize the example dataset plt.plot(X[:, 0], X[:, 1], 'wo', ms=10, mec='b', mew=1) plt.axis([0.5, 6.5, 2, 8]) plt.savefig('figure1.png') print('Program paused. Press enter to continue.') #pause ## =============== Part 2: Principal Component Analysis =============== # You should now implement PCA, a dimension reduction technique. You # should complete the code in pca.m # print('\nRunning PCA on example dataset.\n') # Before running PCA, it is important to first normalize X X_norm, mu, sigma = featureNormalize(X) # Run PCA U, S = pca(X_norm) # Compute mu, the mean of the each feature # Draw the eigenvectors centered at mean of data. These lines show the # directions of maximum variations in the dataset. #hold on print(S) print(U) drawLine(mu, mu + 1.5 * np.dot(S[0], U[:,0].T)) drawLine(mu, mu + 1.5 * np.dot(S[1], U[:,1].T)) #hold off plt.savefig('figure2.png') print('Top eigenvector: ') print(' U(:,1) = %f %f ' % (U[0,0], U[1,0])) print('\n(you should expect to see -0.707107 -0.707107)') print('Program paused. Press enter to continue.') #pause ## =================== Part 3: Dimension Reduction =================== # You should now implement the projection step to map the data onto the # first k eigenvectors. The code will then plot the data in this reduced # dimensional space. This will show you what the data looks like when # using only the corresponding eigenvectors to reconstruct it. # # You should complete the code in projectData.m # print('\nDimension reduction on example dataset.\n\n') # Plot the normalized dataset (returned from pca) fig = plt.figure() plt.plot(X_norm[:, 0], X_norm[:, 1], 'bo') # Project the data onto K = 1 dimension K = 1 Z = projectData(X_norm, U, K) print('Projection of the first example: %f' % Z[0]) print('\n(this value should be about 1.481274)\n') X_rec = recoverData(Z, U, K) print('Approximation of the first example: %f %f' % (X_rec[0, 0], X_rec[0, 1])) print('\n(this value should be about -1.047419 -1.047419)\n') # Draw lines connecting the projected points to the original points plt.plot(X_rec[:, 0], X_rec[:, 1], 'ro') for i in range(X_norm.shape[0]): drawLine(X_norm[i,:], X_rec[i,:]) #end plt.savefig('figure3.png') print('Program paused. Press enter to continue.\n') #pause ## =============== Part 4: Loading and Visualizing Face Data ============= # We start the exercise by first loading and visualizing the dataset. # The following code will load the dataset into your environment # print('\nLoading face dataset.\n\n') # Load Face dataset mat = scipy.io.loadmat('ex7faces.mat') X = mat['X'] # Display the first 100 faces in the dataset displayData(X[:100, :]) plt.savefig('figure4.png') print('Program paused. Press enter to continue.\n') #pause ## =========== Part 5: PCA on Face Data: Eigenfaces =================== # Run PCA and visualize the eigenvectors which are in this case eigenfaces # We display the first 36 eigenfaces. # print('\nRunning PCA on face dataset.\n(this mght take a minute or two ...)\n') # Before running PCA, it is important to first normalize X by subtracting # the mean value from each feature X_norm, mu, sigma = featureNormalize(X) # Run PCA U, S = pca(X_norm) # Visualize the top 36 eigenvectors found displayData(U[:, :36].T) plt.savefig('figure5.png') print('Program paused. Press enter to continue.') #pause ## ============= Part 6: Dimension Reduction for Faces ================= # Project images to the eigen space using the top k eigenvectors # If you are applying a machine learning algorithm print('\nDimension reduction for face dataset.\n') K = 100 Z = projectData(X_norm, U, K) print('The projected data Z has a size of: ') print(formatter('%d ', Z.shape)) print('\n\nProgram paused. Press enter to continue.') #pause ## ==== Part 7: Visualization of Faces after PCA Dimension Reduction ==== # Project images to the eigen space using the top K eigen vectors and # visualize only using those K dimensions # Compare to the original input, which is also displayed print('\nVisualizing the projected (reduced dimension) faces.\n') K = 100 X_rec = recoverData(Z, U, K) # Display normalized data #subplot(1, 2, 1) displayData(X_norm[:100,:]) plt.gcf().suptitle('Original faces') #axis square plt.savefig('figure6.a.png') # Display reconstructed data from only k eigenfaces #subplot(1, 2, 2) displayData(X_rec[:100,:]) plt.gcf().suptitle('Recovered faces') #axis square plt.savefig('figure6.b.png') print('Program paused. Press enter to continue.') #pause ## === Part 8(a): Optional (ungraded) Exercise: PCA for Visualization === # One useful application of PCA is to use it to visualize high-dimensional # data. In the last K-Means exercise you ran K-Means on 3-dimensional # pixel colors of an image. We first visualize this output in 3D, and then # apply PCA to obtain a visualization in 2D. #close all; close all; clc # Re-load the image from the previous exercise and run K-Means on it # For this to work, you need to complete the K-Means assignment first A = matplotlib.image.imread('bird_small.png') # If imread does not work for you, you can try instead # load ('bird_small.mat') A = A / 255 X = A.reshape(-1, 3) K = 16 max_iters = 10 initial_centroids = kMeansInitCentroids(X, K) centroids, idx = runkMeans('7', X, initial_centroids, max_iters) # Sample 1000 random indexes (since working with all the data is # too expensive. If you have a fast computer, you may increase this. sel = np.random.choice(X.shape[0], size=1000) # Setup Color Palette #palette = hsv(K) #colors = palette(idx(sel), :) # Visualize the data and centroid memberships in 3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(X[sel, 0], X[sel, 1], X[sel, 2], cmap='rainbow', c=idx[sel], s=8**2) ax.set_title('Pixel dataset plotted in 3D. Color shows centroid memberships') plt.savefig('figure8.png') print('Program paused. Press enter to continue.') #pause ## === Part 8(b): Optional (ungraded) Exercise: PCA for Visualization === # Use PCA to project this cloud to 2D for visualization # Subtract the mean to use PCA X_norm, mu, sigma = featureNormalize(X) # PCA and project the data to 2D U, S = pca(X_norm) Z = projectData(X_norm, U, 2) # Plot in 2D fig = plt.figure() plotDataPoints(Z[sel, :], [idx[sel]], K, 0) plt.title('Pixel dataset plotted in 2D, using PCA for dimensionality reduction') plt.savefig('figure9.png') print('Program paused. Press enter to continue.\n')
def ex5(): ## Machine Learning Online Class # Exercise 5 | Regularized Linear Regression and Bias-Variance # # Instructions # ------------ # # This file contains code that helps you get started on the # exercise. You will need to complete the following functions: # # linearRegCostFunction.m # learningCurve.m # validationCurve.m # # For this exercise, you will not need to change any code in this file, # or any other files other than those mentioned above. # ## Initialization #clear ; close all; clc ## =========== Part 1: Loading and Visualizing Data ============= # We start the exercise by first loading and visualizing the dataset. # The following code will load the dataset into your environment and plot # the data. # # Load Training Data print('Loading and Visualizing Data ...') # Load from ex5data1: # You will have X, y, Xval, yval, Xtest, ytest in your environment mat = scipy.io.loadmat('ex5data1.mat') X = mat['X'] y = mat['y'].ravel() Xval = mat['Xval'] yval = mat['yval'].ravel() Xtest = mat['Xtest'] ytest = mat['ytest'].ravel() # m = Number of examples m = X.shape[0] # Plot training data plt.plot(X, y, marker='x', linestyle='None', ms=10, lw=1.5) plt.xlabel('Change in water level (x)') plt.ylabel('Water flowing out of the dam (y)') plt.savefig('figure1.png') print('Program paused. Press enter to continue.') #pause; ## =========== Part 2: Regularized Linear Regression Cost ============= # You should now implement the cost function for regularized linear # regression. # theta = np.array([1, 1]) J, _ = linearRegCostFunction(np.concatenate([np.ones((m, 1)), X], axis=1), y, theta, 1) print( 'Cost at theta = [1 ; 1]: %f \n(this value should be about 303.993192)' % J) print('Program paused. Press enter to continue.') #pause; ## =========== Part 3: Regularized Linear Regression Gradient ============= # You should now implement the gradient for regularized linear # regression. # theta = np.array([1, 1]) J, grad = linearRegCostFunction( np.concatenate([np.ones((m, 1)), X], axis=1), y, theta, 1) print( 'Gradient at theta = [1 ; 1]: [%f; %f] \n(this value should be about [-15.303016; 598.250744])' % (grad[0], grad[1])) print('Program paused. Press enter to continue.') #pause; ## =========== Part 4: Train Linear Regression ============= # Once you have implemented the cost and gradient correctly, the # trainLinearReg function will use your cost function to train # regularized linear regression. # # Write Up Note: The data is non-linear, so this will not give a great # fit. # fig = plt.figure() # Train linear regression with lambda = 0 lambda_value = 0 theta = trainLinearReg(np.concatenate([np.ones((m, 1)), X], axis=1), y, lambda_value) # Plot fit over the data plt.plot(X, y, marker='x', linestyle='None', ms=10, lw=1.5) plt.xlabel('Change in water level (x)') plt.ylabel('Water flowing out of the dam (y)') plt.plot(X, np.dot(np.concatenate([np.ones((m, 1)), X], axis=1), theta), '--', lw=2) plt.savefig('figure2.png') print('Program paused. Press enter to continue.') #pause; ## =========== Part 5: Learning Curve for Linear Regression ============= # Next, you should implement the learningCurve function. # # Write Up Note: Since the model is underfitting the data, we expect to # see a graph with "high bias" -- slide 8 in ML-advice.pdf # fig = plt.figure() lambda_value = 0 error_train, error_val = learningCurve( np.concatenate([np.ones((m, 1)), X], axis=1), y, np.concatenate([np.ones((yval.size, 1)), Xval], axis=1), yval, lambda_value) plt.plot(np.arange(1, m + 1), error_train, np.arange(1, m + 1), error_val) plt.title('Learning curve for linear regression') plt.legend(['Train', 'Cross Validation']) plt.xlabel('Number of training examples') plt.ylabel('Error') plt.axis([0, 13, 0, 150]) print('# Training Examples\tTrain Error\tCross Validation Error') for i in range(m): print(' \t%d\t\t%f\t%f' % (i, error_train[i], error_val[i])) plt.savefig('figure3.png') print('Program paused. Press enter to continue.') #pause; ## =========== Part 6: Feature Mapping for Polynomial Regression ============= # One solution to this is to use polynomial regression. You should now # complete polyFeatures to map each example into its powers # p = 8 # Map X onto Polynomial Features and Normalize X_poly = polyFeatures(X, p) X_poly, mu, sigma = featureNormalize(X_poly) # Normalize X_poly = np.concatenate([np.ones((m, 1)), X_poly], axis=1) # Add Ones # Map X_poly_test and normalize (using mu and sigma) X_poly_test = polyFeatures(Xtest, p) X_poly_test -= mu X_poly_test /= sigma X_poly_test = np.concatenate( [np.ones((X_poly_test.shape[0], 1)), X_poly_test], axis=1) # Add Ones # Map X_poly_val and normalize (using mu and sigma) X_poly_val = polyFeatures(Xval, p) X_poly_val -= mu X_poly_val /= sigma X_poly_val = np.concatenate( [np.ones((X_poly_val.shape[0], 1)), X_poly_val], axis=1) # Add Ones print('Normalized Training Example 1:') print(formatter(' %f \n', X_poly[0, :])) print('\nProgram paused. Press enter to continue.') #pause; ## =========== Part 7: Learning Curve for Polynomial Regression ============= # Now, you will get to experiment with polynomial regression with multiple # values of lambda. The code below runs polynomial regression with # lambda = 0. You should try running the code with different values of # lambda to see how the fit and learning curve change. # fig = plt.figure() lambda_value = 0 theta = trainLinearReg(X_poly, y, lambda_value) # Plot training data and fit plt.plot(X, y, marker='x', ms=10, lw=1.5) plotFit(np.min(X), np.max(X), mu, sigma, theta, p) plt.xlabel('Change in water level (x)') plt.ylabel('Water flowing out of the dam (y)') plt.title('Polynomial Regression Fit (lambda = %f)' % lambda_value) plt.figure() error_train, error_val = learningCurve(X_poly, y, X_poly_val, yval, lambda_value) plt.plot(np.arange(1, 1 + m), error_train, np.arange(1, 1 + m), error_val) plt.title('Polynomial Regression Learning Curve (lambda = %f)' % lambda_value) plt.xlabel('Number of training examples') plt.ylabel('Error') plt.axis([0, 13, 0, 100]) plt.legend(['Train', 'Cross Validation']) print('Polynomial Regression (lambda = %f)\n' % lambda_value) print('# Training Examples\tTrain Error\tCross Validation Error') for i in range(m): print(' \t%d\t\t%f\t%f' % (i, error_train[i], error_val[i])) plt.savefig('figure4.png') print('Program paused. Press enter to continue.') #pause; ## =========== Part 8: Validation for Selecting Lambda ============= # You will now implement validationCurve to test various values of # lambda on a validation set. You will then use this to select the # "best" lambda value. # fig = plt.figure() lambda_vec, error_train, error_val = validationCurve( X_poly, y, X_poly_val, yval) plt.plot(lambda_vec, error_train, lambda_vec, error_val) plt.legend(['Train', 'Cross Validation']) plt.xlabel('lambda') plt.ylabel('Error') print('lambda\t\tTrain Error\tValidation Error') for i in range(lambda_vec.size): print(' %f\t%f\t%f' % (lambda_vec[i], error_train[i], error_val[i])) plt.savefig('figure5.png') print('Program paused. Press enter to continue.')
def ex6_spam(): ## Machine Learning Online Class # Exercise 6 | Spam Classification with SVMs # # Instructions # ------------ # # This file contains code that helps you get started on the # exercise. You will need to complete the following functions: # # gaussianKernel.m # dataset3Params.m # processEmail.m # emailFeatures.m # # For this exercise, you will not need to change any code in this file, # or any other files other than those mentioned above. # ## Initialization #clear ; close all; clc ## ==================== Part 1: Email Preprocessing ==================== # To use an SVM to classify emails into Spam v.s. Non-Spam, you first need # to convert each email into a vector of features. In this part, you will # implement the preprocessing steps for each email. You should # complete the code in processEmail.m to produce a word indices vector # for a given email. print('\nPreprocessing sample email (emailSample1.txt)') # Extract Features file_contents = readFile('emailSample1.txt') word_indices = processEmail(file_contents) # Print Stats print('Word Indices: ') print(formatter(' %d', np.array(word_indices) + 1)) print('\n') print('Program paused. Press enter to continue.') #pause; ## ==================== Part 2: Feature Extraction ==================== # Now, you will convert each email into a vector of features in R^n. # You should complete the code in emailFeatures.m to produce a feature # vector for a given email. print('\nExtracting features from sample email (emailSample1.txt)') # Extract Features file_contents = readFile('emailSample1.txt') word_indices = processEmail(file_contents) features = emailFeatures(word_indices) # Print Stats print('Length of feature vector: %d' % features.size) print('Number of non-zero entries: %d' % np.sum(features > 0)) print('Program paused. Press enter to continue.') #pause; ## =========== Part 3: Train Linear SVM for Spam Classification ======== # In this section, you will train a linear classifier to determine if an # email is Spam or Not-Spam. # Load the Spam Email dataset # You will have X, y in your environment mat = scipy.io.loadmat('spamTrain.mat') X = mat['X'].astype(float) y = mat['y'][:, 0] print('\nTraining Linear SVM (Spam Classification)\n') print('(this may take 1 to 2 minutes) ...\n') C = 0.1 model = svmTrain(X, y, C, linearKernel) p = svmPredict(model, X) print('Training Accuracy: %f' % (np.mean(p == y) * 100)) ## =================== Part 4: Test Spam Classification ================ # After training the classifier, we can evaluate it on a test set. We have # included a test set in spamTest.mat # Load the test dataset # You will have Xtest, ytest in your environment mat = scipy.io.loadmat('spamTest.mat') Xtest = mat['Xtest'].astype(float) ytest = mat['ytest'][:, 0] print('\nEvaluating the trained Linear SVM on a test set ...\n') p = svmPredict(model, Xtest) print('Test Accuracy: %f\n' % (np.mean(p == ytest) * 100)) #pause; ## ================= Part 5: Top Predictors of Spam ==================== # Since the model we are training is a linear SVM, we can inspect the # weights learned by the model to understand better how it is determining # whether an email is spam or not. The following code finds the words with # the highest weights in the classifier. Informally, the classifier # 'thinks' that these words are the most likely indicators of spam. # # Sort the weights and obtin the vocabulary list idx = np.argsort(model['w']) top_idx = idx[-15:][::-1] vocabList = getVocabList() print('\nTop predictors of spam: ') for word, w in zip(np.array(vocabList)[top_idx], model['w'][top_idx]): print(' %-15s (%f)' % (word, w)) #end print('\n') print('\nProgram paused. Press enter to continue.') #pause; ## =================== Part 6: Try Your Own Emails ===================== # Now that you've trained the spam classifier, you can use it on your own # emails! In the starter code, we have included spamSample1.txt, # spamSample2.txt, emailSample1.txt and emailSample2.txt as examples. # The following code reads in one of these emails and then uses your # learned SVM classifier to determine whether the email is Spam or # Not Spam # Set the file to be read in (change this to spamSample2.txt, # emailSample1.txt or emailSample2.txt to see different predictions on # different emails types). Try your own emails as well! filename = 'spamSample1.txt' # Read and predict file_contents = readFile(filename) word_indices = processEmail(file_contents) x = emailFeatures(word_indices) p = svmPredict(model, x.ravel()) print('\nProcessed %s\n\nSpam Classification: %d' % (filename, p)) print('(1 indicates spam, 0 indicates not spam)\n')
def ex4(): ## Machine Learning Online Class - Exercise 4 Neural Network Learning # Instructions # ------------ # # This file contains code that helps you get started on the # linear exercise. You will need to complete the following functions # in this exericse: # # sigmoidGradient.m # randInitializeWeights.m # nnCostFunction.m # # For this exercise, you will not need to change any code in this file, # or any other files other than those mentioned above. # ## Initialization #clear ; close all; clc ## Setup the parameters you will use for this exercise input_layer_size = 400 # 20x20 Input Images of Digits hidden_layer_size = 25 # 25 hidden units num_labels = 10 # 10 labels, from 1 to 10 # (note that we have mapped "0" to label 10) ## =========== Part 1: Loading and Visualizing Data ============= # We start the exercise by first loading and visualizing the dataset. # You will be working with a dataset that contains handwritten digits. # # Load Training Data print('Loading and Visualizing Data ...') mat = scipy.io.loadmat('ex4data1.mat') X = mat['X'] y = mat['y'].ravel() m = X.shape[0] # Randomly select 100 data points to display sel = np.random.choice(m, 100, replace=False) displayData(X[sel, :]) plt.savefig('figure1.png') print('Program paused. Press enter to continue.') #pause; ## ================ Part 2: Loading Parameters ================ # In this part of the exercise, we load some pre-initialized # neural network parameters. print('\nLoading Saved Neural Network Parameters ...') # Load the weights into variables Theta1 and Theta2 mat = scipy.io.loadmat('ex4weights.mat') Theta1 = mat['Theta1'] Theta2 = mat['Theta2'] # Unroll parameters nn_params = np.concatenate([Theta1.ravel(), Theta2.ravel()]) ## ================ Part 3: Compute Cost (Feedforward) ================ # To the neural network, you should first start by implementing the # feedforward part of the neural network that returns the cost only. You # should complete the code in nnCostFunction.m to return cost. After # implementing the feedforward to compute the cost, you can verify that # your implementation is correct by verifying that you get the same cost # as us for the fixed debugging parameters. # # We suggest implementing the feedforward cost *without* regularization # first so that it will be easier for you to debug. Later, in part 4, you # will get to implement the regularized cost. # print('\nFeedforward Using Neural Network ...') # Weight regularization parameter (we set this to 0 here). lambda_value = 0 J = nnCostFunction(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lambda_value)[0] print( 'Cost at parameters (loaded from ex4weights): %f \n(this value should be about 0.287629)' % J) print('\nProgram paused. Press enter to continue.') #pause; ## =============== Part 4: Implement Regularization =============== # Once your cost function implementation is correct, you should now # continue to implement the regularization with the cost. # print('\nChecking Cost Function (w/ Regularization) ... ') # Weight regularization parameter (we set this to 1 here). lambda_value = 1 J = nnCostFunction(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lambda_value)[0] print( 'Cost at parameters (loaded from ex4weights): %f \n(this value should be about 0.383770)' % J) print('Program paused. Press enter to continue.') #pause; ## ================ Part 5: Sigmoid Gradient ================ # Before you start implementing the neural network, you will first # implement the gradient for the sigmoid function. You should complete the # code in the sigmoidGradient.m file. # print('\nEvaluating sigmoid gradient...') g = sigmoidGradient(np.array([1, -0.5, 0, 0.5, 1])) print('Sigmoid gradient evaluated at [1 -0.5 0 0.5 1]:') print(formatter('%f ', g)) print('\n') print('Program paused. Press enter to continue.') #pause; ## ================ Part 6: Initializing Pameters ================ # In this part of the exercise, you will be starting to implment a two # layer neural network that classifies digits. You will start by # implementing a function to initialize the weights of the neural network # (randInitializeWeights.m) print('\nInitializing Neural Network Parameters ...') initial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size) initial_Theta2 = randInitializeWeights(hidden_layer_size, num_labels) # Unroll parameters initial_nn_params = np.concatenate( [initial_Theta1.ravel(), initial_Theta2.ravel()]) ## =============== Part 7: Implement Backpropagation =============== # Once your cost matches up with ours, you should proceed to implement the # backpropagation algorithm for the neural network. You should add to the # code you've written in nnCostFunction.m to return the partial # derivatives of the parameters. # print('\nChecking Backpropagation... ') # Check gradients by running checkNNGradients checkNNGradients() print('\nProgram paused. Press enter to continue.') #pause; ## =============== Part 8: Implement Regularization =============== # Once your backpropagation implementation is correct, you should now # continue to implement the regularization with the cost and gradient. # print('\nChecking Backpropagation (w/ Regularization) ... ') # Check gradients by running checkNNGradients lambda_value = 3 checkNNGradients(lambda_value) # Also output the costFunction debugging values debug_J = nnCostFunction(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lambda_value)[0] print( '\n\nCost at (fixed) debugging parameters (w/ lambda = 10): %f \n(this value should be about 0.576051)\n\n' % debug_J) print('Program paused. Press enter to continue.') #pause; ## =================== Part 8: Training NN =================== # You have now implemented all the code necessary to train a neural # network. To train your neural network, we will now use "fmincg", which # is a function which works similarly to "fminunc". Recall that these # advanced optimizers are able to train our cost functions efficiently as # long as we provide them with the gradient computations. # print('\nTraining Neural Network... ') # After you have completed the assignment, change the MaxIter to a larger # value to see how more training helps. options = {'maxiter': 50} # You should also try different values of lambda lambda_value = 1 # Create "short hand" for the cost function to be minimized costFunction = lambda p: nnCostFunction( p, input_layer_size, hidden_layer_size, num_labels, X, y, lambda_value) # Now, costFunction is a function that takes in only one argument (the # neural network parameters) res = optimize.minimize(costFunction, initial_nn_params, jac=True, method='TNC', options=options) nn_params = res.x # Obtain Theta1 and Theta2 back from nn_params Theta1 = nn_params[0:hidden_layer_size * (input_layer_size + 1)].reshape( hidden_layer_size, input_layer_size + 1) Theta2 = nn_params[hidden_layer_size * (input_layer_size + 1):].reshape( num_labels, hidden_layer_size + 1) print('Program paused. Press enter to continue.') #pause; ## ================= Part 9: Visualize Weights ================= # You can now "visualize" what the neural network is learning by # displaying the hidden units to see what features they are capturing in # the data. print('\nVisualizing Neural Network... ') displayData(Theta1[:, 1:]) plt.savefig('figure2.png') print('\nProgram paused. Press enter to continue.') #pause; ## ================= Part 10: Implement Predict ================= # After training the neural network, we would like to use it to predict # the labels. You will now implement the "predict" function to use the # neural network to predict the labels of the training set. This lets # you compute the training set accuracy. pred = predict(Theta1, Theta2, X) print('\nTraining Set Accuracy: %f' % (np.mean( (pred == y).astype(int)) * 100))