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 plotBoundary(X, y, svm): #Plot Boundary u = linspace(min(X[:, 0]), max(X[:, 0]), 200) v = linspace(min(X[:, 1]), max(X[:, 1]), 200) z = zeros(shape=(len(u), len(v))) for i in range(len(u)): for j in range(len(v)): z[i, j] = svm.predict(gaussianKernel(array([[u[i], v[j]]]), X)) plot(X[:, 0][y == 1], X[:, 1][y == 1], 'ro', label="c1") plot(X[:, 0][y == 0], X[:, 1][y == 0], 'b+', label="c2") contour(u, v, z.T, [0]) xlabel('Microchip Test 1') ylabel('Microchip Test 2') legend(['y = 1', 'y = 0', 'Decision boundary'], numpoints=1) show()
def dataset3Params(X, y, Xval, yval): choice = np.array([0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30]).reshape(-1, 1) minError = np.inf curC = np.inf cur_sigma = np.inf for i in range(choice.shape[0]): for j in range(choice.shape[0]): func = lambda a, b: gaussianKernel(a, b, choice[j]) func.__name__ = 'gaussianKernel' model = svmTrain(X, y, choice[i], func) predictions = svmPredict(model, Xval) error = np.mean(np.double(np.not_equal(predictions, yval))) if error < minError: minError = error curC = choice[i] cur_sigma = choice[j] C = curC sigma = cur_sigma return C, sigma
def dataset3Params(X, y, Xval, yval): choice = np.array([0.01,0.03,0.1,0.3,1,3,10,30]).reshape(-1,1) minError = np.inf curC = np.inf cur_sigma = np.inf for i in range(choice.shape[0]): for j in range(choice.shape[0]): func = lambda a, b: gaussianKernel(a, b, choice[j]) func.__name__ = 'gaussianKernel' model = svmTrain(X, y, choice[i], func) predictions = svmPredict(model, Xval) error = np.mean(np.double(np.not_equal(predictions,yval))) if error < minError: minError = error curC = choice[i] cur_sigma = choice[j] C = curC sigma = cur_sigma return C, sigma
# # The first two columns contains the exam scores and the third column # # contains the label. data = loadtxt('data2.txt', delimiter=',') X = data[:, 0:2] y = data[:, 2] # # Plot data plt.plot(X[:, 0][y == 1], X[:, 1][y == 1], 'r+', label="c1") plt.plot(X[:, 0][y == 0], X[:, 1][y == 0], 'bo', label="c2") plt.legend(['y = 1', 'y = 0'], numpoints=1) plt.show() sigma = 0.2 # Gaussian kernel variance # We calculate the Gaussian kernel between the instances/samples K = gaussianKernel(X, X, sigma) C = 5.0 # SVM regularization parameter # We create an instance of SVM and fit out data. We do not scale our # data since we want to plot the support vectors svc = SVC(C=C, kernel="precomputed") svc.fit(K, y) # Plot the decision boundary u = linspace(min(X[:, 0]), max(X[:, 0]), 200) v = linspace(min(X[:, 1]), max(X[:, 1]), 200) z = zeros(shape=(len(u), len(v))) for i in range(len(u)): for j in range(len(v)): z[i, j] = svc.predict(gaussianKernel(array([[u[i], v[j]]]), X, sigma))
clf = svm.SVC(C=C, kernel='linear', tol=1e-3, max_iter=200) model = clf.fit(X, y) visualizeBoundaryLinear(X, y, model) raw_input("Program paused. Press Enter to continue...") ## =============== Part 3: Implementing Gaussian Kernel =============== # You will now implement the Gaussian kernel to use # with the SVM. You should complete the code in gaussianKernel.m # print 'Evaluating the Gaussian Kernel ...' x1 = np.array([1, 2, 1]) x2 = np.array([0, 4, -1]) sigma = 2 sim = gaussianKernel(x1, x2, sigma) print 'Gaussian Kernel between x1 = [1 2 1], x2 = [0 4 -1], sigma = %0.5f : ' \ '\t%f\n(this value should be about 0.324652)\n' % (sigma, sim) raw_input("Program paused. Press Enter to continue...") ## =============== Part 4: Visualizing Dataset 2 ================ # The following code will load the next dataset into your environment and # plot the data. # print 'Loading and Visualizing Data ...' # Load from ex6data2: # You will have X, y in your environment
#visualizeBoundaryLinear(X, y, clf) #fprintf('Program paused. Press enter to continue.\n'); #% =============== Part 3: Implementing Gaussian Kernel =============== # You will now implement the Gaussian kernel to use # with the SVM. You should complete the code in gaussianKernel.m # print('\nEvaluating the Gaussian Kernel ...\n') x1 = np.array([1, 2, 1]) x2 = np.array([0, 4, -1]) sigma = 2 sim = gaussianKernel(x1, x2, sigma) print('Gaussian Kernel between x1 = [1; 2; 1], x2 = [0; 4; -1], sigma = 0.5 :' , '\n\t%f\n(this value should be about 0.324652)\t', sim) #%% =============== Part 4: Visualizing Dataset 2 ================ #% The following code will load the next dataset into your environment and #% plot the data. # Load from ex6data2: # You will have X, y in your environment #load('ex6data2.mat');
model = svmTrain(X, y, C, linearKernel, 1e-3, 20) print(model) visualizeBoundaryLinear(X, y, model) input('Program paused. Press enter to continue.\n') ## =============== Part 3: Implementing Gaussian Kernel =============== # You will now implement the Gaussian kernel to use # with the SVM. You should complete the code in gaussianKernel.m # print('\nEvaluating the Gaussian Kernel ...\n') x1 = np.array([1, 2, 1]) x2 = np.array([0, 4, -1]) sigma = 2 sim = gaussianKernel(x1, x2, sigma) print('Gaussian Kernel between x1 = [1; 2; 1], x2 = [0; 4; -1], sigma = 0.5 :' \ '\n\t%lf\n(this value should be about 0.324652)\n', sim) input('Program paused. Press enter to continue.\n') ## =============== Part 4: Visualizing Dataset 2 ================ # The following code will load the next dataset into your environment and # plot the data. # print('Loading and Visualizing Data ...\n') # Load from ex6data2: # You will have X, y in your environment
visualizeBoundaryLinear(X, y, model) fig.show() print 'Program paused. Press enter to continue.' raw_input() ## =============== Part 3: Implementing Gaussian Kernel =============== # You will now implement the Gaussian kernel to use # with the SVM. You should complete the code in gaussianKernel.m # print '\nEvaluating the Gaussian Kernel ...' x1 = array([1, 2, 1]) x2 = array([0, 4, -1]) sigma = 2.0 sim = gaussianKernel(x1, x2, sigma) print 'Gaussian Kernel between x1 = [1, 2, 1], x2 = [0, 4, -1], sigma = 0.5 :' print '\t%f\n(this value should be about 0.324652)' % sim print 'Program paused. Press enter to continue.' raw_input() ## =============== Part 4: Visualizing Dataset 2 ================ # The following code will load the next dataset into your environment and # plot the data. # print 'Loading and Visualizing Data ...' # Load from ex6data2:
def ex6(): # Machine Learning Online Class # Exercise 6 | Support Vector Machines # # 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: 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. # print('Loading and Visualizing Data ...') # Load from ex6data1: # You will have X, y in your environment mat = scipy.io.loadmat('ex6data1.mat') X = mat['X'].astype(float) y = mat['y'][:, 0] # Plot training data plotData(X, y) plt.savefig('figure1.png') print('Program paused. Press enter to continue.') #pause; # ==================== Part 2: Training Linear SVM ==================== # The following code will train a linear SVM on the dataset and plot the # decision boundary learned. # # Load from ex6data1: # You will have X, y in your environment mat = scipy.io.loadmat('ex6data1.mat') X = mat['X'].astype(float) y = mat['y'][:, 0] print('\nTraining Linear SVM ...') # You should try to change the C value below and see how the decision # boundary varies (e.g., try C = 1000) C = 1 model = svmTrain(X, y, C, linearKernel, 1e-3, 20) visualizeBoundaryLinear(X, y, model) plt.savefig('figure2.png') print('Program paused. Press enter to continue.') #pause; # =============== Part 3: Implementing Gaussian Kernel =============== # You will now implement the Gaussian kernel to use # with the SVM. You should complete the code in gaussianKernel.m # print('\nEvaluating the Gaussian Kernel ...') x1 = np.array([ 1, 2, 1, ]) x2 = np.array([0, 4, -1]) sigma = 2 sim = gaussianKernel(x1, x2, sigma) print( 'Gaussian Kernel between x1 = [1; 2; 1], x2 = [0; 4; -1], sigma = 0.5 :\n\t%f\n(this value should be about 0.324652)' % sim) print('Program paused. Press enter to continue.') #pause; # =============== Part 4: Visualizing Dataset 2 ================ # The following code will load the next dataset into your environment and # plot the data. # fig = plt.figure() print('Loading and Visualizing Data ...') # Load from ex6data2: # You will have X, y in your environment mat = scipy.io.loadmat('ex6data2.mat') X = mat['X'].astype(float) y = mat['y'][:, 0] # Plot training data plotData(X, y) plt.savefig('figure3.png') print('Program paused. Press enter to continue.') #pause; # ========== Part 5: Training SVM with RBF Kernel (Dataset 2) ========== # After you have implemented the kernel, we can now use it to train the # SVM classifier. # print('\nTraining SVM with RBF Kernel (this may take 1 to 2 minutes) ...') # Load from ex6data2: # You will have X, y in your environment mat = scipy.io.loadmat('ex6data2.mat') X = mat['X'].astype(float) y = mat['y'][:, 0] # SVM Parameters C = 1 sigma = 0.1 # We set the tolerance and max_passes lower here so that the code will run # faster. However, in practice, you will want to run the training to # convergence. model = svmTrain(X, y, C, gaussianKernel, args=(sigma, )) visualizeBoundary(X, y, model) plt.savefig('figure4.png') print('Program paused. Press enter to continue.') #pause; # =============== Part 6: Visualizing Dataset 3 ================ # The following code will load the next dataset into your environment and # plot the data. # fig = plt.figure() print('Loading and Visualizing Data ...') # Load from ex6data3: # You will have X, y in your environment mat = scipy.io.loadmat('ex6data3.mat') X = mat['X'].astype(float) y = mat['y'][:, 0] # Plot training data plotData(X, y) plt.savefig('figure5.png') print('Program paused. Press enter to continue.') #pause; # ========== Part 7: Training SVM with RBF Kernel (Dataset 3) ========== # This is a different dataset that you can use to experiment with. Try # different values of C and sigma here. # # Load from ex6data3: # You will have X, y in your environment mat = scipy.io.loadmat('ex6data3.mat') X = mat['X'].astype(float) y = mat['y'][:, 0] Xval = mat['Xval'].astype(float) yval = mat['yval'][:, 0] # Try different SVM Parameters here C, sigma = dataset3Params(X, y, Xval, yval) # Train the SVM model = svmTrain(X, y, C, gaussianKernel, args=(sigma, )) visualizeBoundary(X, y, model) plt.savefig('figure6.png') print('Program paused. Press enter to continue.')
visualizeBoundaryLinear(X, y, model) fig.show() print 'Program paused. Press enter to continue.' raw_input() ## =============== Part 3: Implementing Gaussian Kernel =============== # You will now implement the Gaussian kernel to use # with the SVM. You should complete the code in gaussianKernel.m # print '\nEvaluating the Gaussian Kernel ...' x1 = array([1, 2, 1]) x2 = array([0, 4, -1]) sigma = 2.0 sim = gaussianKernel(x1, x2, sigma) print 'Gaussian Kernel between x1 = [1, 2, 1], x2 = [0, 4, -1], sigma = 0.5 :' print '\t%f\n(this value should be about 0.324652)' % sim print 'Program paused. Press enter to continue.' raw_input() ## =============== Part 4: Visualizing Dataset 2 ================ # The following code will load the next dataset into your environment and # plot the data. # print 'Loading and Visualizing Data ...' # Load from ex6data2:
yval = dataeval[:, 2] # # Plot data plt.plot(X[:, 0][y == 1], X[:, 1][y == 1], 'r+', label="c1") plt.plot(X[:, 0][y == 0], X[:, 1][y == 0], 'bo', label="c2") plt.legend(['y = 1', 'y = 0'], numpoints=1) plt.show() sigma = 0.2 # Gaussian kernel variance C = 0.0 # SVM regularization parameter # we create an instance of SVM and fit out data. We do not scale our # data since we want to plot the support vectors svc = SVC(C=C, kernel="precomputed") svc.fit(gaussianKernel(X, X, sigma), y) # Plot the decision boundary u = linspace(min(X[:, 0]), max(X[:, 0]), 200) v = linspace(min(X[:, 1]), max(X[:, 1]), 200) z = zeros(shape=(len(u), len(v))) for i in range(len(u)): for j in range(len(v)): z[i, j] = svc.predict(gaussianKernel(array([[u[i], v[j]]]), X, sigma)) plot(X[:, 0][y == 1], X[:, 1][y == 1], 'r+', label="c1") plot(X[:, 0][y == 0], X[:, 1][y == 0], 'bo', label="c2") contour(u, v, z.T, [0]) legend(['y = 1', 'y = 0', 'Decision boundary'], numpoints=1) show()
# boundary varies (e.g., try C = 1000) C = 1 model = svmTrain(X, y, C, linearKernel, 1e-3, 20) print(model) visualizeBoundaryLinear(X, y, model) input('Program paused. Press enter to continue.\n') ## =============== Part 3: Implementing Gaussian Kernel =============== # You will now implement the Gaussian kernel to use # with the SVM. You should complete the code in gaussianKernel.m # print('\nEvaluating the Gaussian Kernel ...\n') x1 = np.array([1,2,1]); x2 = np.array([0,4,-1]); sigma = 2 sim = gaussianKernel(x1, x2, sigma) print('Gaussian Kernel between x1 = [1; 2; 1], x2 = [0; 4; -1], sigma = 0.5 :' \ '\n\t%lf\n(this value should be about 0.324652)\n', sim) input('Program paused. Press enter to continue.\n') ## =============== Part 4: Visualizing Dataset 2 ================ # The following code will load the next dataset into your environment and # plot the data. # print('Loading and Visualizing Data ...\n') # Load from ex6data2: # You will have X, y in your environment