Skip to content

Commit e03f543

Browse files
solves programming exercise
1 parent d0d52e5 commit e03f543

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+6187
-2
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ __Instructor__: Andrew Ng.
9999
- [Support Vector Machines (SVM)](week7/support-vector-machines-quiz.md)
100100

101101
### Programming Exercises
102+
- [Questions and Explanations](week7/ex6.pdf)
103+
- [Exercise 6: Support Vector Machines](week7/ex6)
104+
- [Gaussian Kernel for SVM](week7/ex6/gaussianKernel.m)
105+
- [Parameters to Use for Dataset 3](week7/ex6/dataset3Params.m)
106+
- [Email Preprocessing](week7/ex6/processEmail.m)
107+
- [Feature Extraction From Email](week7/ex6/emailFeatures.m)
102108

103109
## Week 8
104110
### Quizzes

week7/ex6.pdf

328 KB
Binary file not shown.

week7/ex6/dataset3Params.m

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function [C, sigma] = dataset3Params(X, y, Xval, yval)
2+
%DATASET3PARAMS returns your choice of C and sigma for Part 3 of the exercise
3+
%where you select the optimal (C, sigma) learning parameters to use for SVM
4+
%with RBF kernel
5+
% [C, sigma] = DATASET3PARAMS(X, y, Xval, yval) returns your choice of C and
6+
% sigma. You should complete this function to return the optimal C and
7+
% sigma based on a cross-validation set.
8+
%
9+
10+
C = 1;
11+
sigma = 0.1;
12+
13+
% Code to compute the parameters with minimum cost
14+
% [C, sigma] = computeMinCostParameters()
15+
16+
function [C, sig] = computeMinCostParameters()
17+
results = eye(64,3);
18+
errorRow = 0;
19+
20+
for C_test = [0.01 0.03 0.1 0.3 1, 3, 10 30]
21+
for sigma_test = [0.01 0.03 0.1 0.3 1, 3, 10 30]
22+
errorRow = errorRow + 1;
23+
model = svmTrain(X, y, C_test, @(x1, x2) gaussianKernel(x1, x2, sigma_test));
24+
predictions = svmPredict(model, Xval);
25+
prediction_error = mean(double(predictions ~= yval));
26+
results(errorRow,:) = [C_test, sigma_test, prediction_error];
27+
end
28+
end
29+
30+
sorted_results = sortrows(results, 3); % sort matrix by column #3, the error, ascending
31+
32+
C = sorted_results(1,1);
33+
sig = sorted_results(1,2);
34+
disp('C'); disp(C);
35+
disp('sigma'); disp(sig);
36+
endfunction
37+
end

week7/ex6/emailFeatures.m

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function x = emailFeatures(word_indices)
2+
%EMAILFEATURES takes in a word_indices vector and produces a feature vector
3+
%from the word indices
4+
% x = EMAILFEATURES(word_indices) takes in a word_indices vector and
5+
% produces a feature vector from the word indices.
6+
7+
% Total number of words in the dictionary
8+
n = 1899;
9+
10+
% You need to return the following variables correctly.
11+
x = zeros(n, 1);
12+
13+
for i = 1:length(word_indices)
14+
x(word_indices(i)) = 1;
15+
endfor
16+
end

week7/ex6/emailSample1.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
> Anyone knows how much it costs to host a web portal ?
2+
>
3+
Well, it depends on how many visitors you're expecting.
4+
This can be anywhere from less than 10 bucks a month to a couple of $100.
5+
You should checkout http://www.rackspace.com/ or perhaps Amazon EC2
6+
if youre running something big..
7+
8+
To unsubscribe yourself from this mailing list, send an email to:
9+
10+

week7/ex6/emailSample2.txt

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Folks,
2+
3+
my first time posting - have a bit of Unix experience, but am new to Linux.
4+
5+
6+
Just got a new PC at home - Dell box with Windows XP. Added a second hard disk
7+
for Linux. Partitioned the disk and have installed Suse 7.2 from CD, which went
8+
fine except it didn't pick up my monitor.
9+
10+
I have a Dell branded E151FPp 15" LCD flat panel monitor and a nVidia GeForce4
11+
Ti4200 video card, both of which are probably too new to feature in Suse's default
12+
set. I downloaded a driver from the nVidia website and installed it using RPM.
13+
Then I ran Sax2 (as was recommended in some postings I found on the net), but
14+
it still doesn't feature my video card in the available list. What next?
15+
16+
Another problem. I have a Dell branded keyboard and if I hit Caps-Lock twice,
17+
the whole machine crashes (in Linux, not Windows) - even the on/off switch is
18+
inactive, leaving me to reach for the power cable instead.
19+
20+
If anyone can help me in any way with these probs., I'd be really grateful -
21+
I've searched the 'net but have run out of ideas.
22+
23+
Or should I be going for a different version of Linux such as RedHat? Opinions
24+
welcome.
25+
26+
Thanks a lot,
27+
Peter
28+
29+
--
30+
Irish Linux Users' Group: [email protected]
31+
http://www.linux.ie/mailman/listinfo/ilug for (un)subscription information.
32+
List maintainer: [email protected]
33+
34+

week7/ex6/ex6.m

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
%% Machine Learning Online Class
2+
% Exercise 6 | Support Vector Machines
3+
%
4+
% Instructions
5+
% ------------
6+
%
7+
% This file contains code that helps you get started on the
8+
% exercise. You will need to complete the following functions:
9+
%
10+
% gaussianKernel.m
11+
% dataset3Params.m
12+
% processEmail.m
13+
% emailFeatures.m
14+
%
15+
% For this exercise, you will not need to change any code in this file,
16+
% or any other files other than those mentioned above.
17+
%
18+
19+
%% Initialization
20+
clear ; close all; clc
21+
22+
%% =============== Part 1: Loading and Visualizing Data ================
23+
% We start the exercise by first loading and visualizing the dataset.
24+
% The following code will load the dataset into your environment and plot
25+
% the data.
26+
%
27+
28+
fprintf('Loading and Visualizing Data ...\n')
29+
30+
% Load from ex6data1:
31+
% You will have X, y in your environment
32+
load('ex6data1.mat');
33+
34+
% Plot training data
35+
plotData(X, y);
36+
37+
fprintf('Program paused. Press enter to continue.\n');
38+
pause;
39+
40+
%% ==================== Part 2: Training Linear SVM ====================
41+
% The following code will train a linear SVM on the dataset and plot the
42+
% decision boundary learned.
43+
%
44+
45+
% Load from ex6data1:
46+
% You will have X, y in your environment
47+
load('ex6data1.mat');
48+
49+
fprintf('\nTraining Linear SVM ...\n')
50+
51+
% You should try to change the C value below and see how the decision
52+
% boundary varies (e.g., try C = 1000)
53+
C = 1;
54+
model = svmTrain(X, y, C, @linearKernel, 1e-3, 20);
55+
visualizeBoundaryLinear(X, y, model);
56+
57+
fprintf('Program paused. Press enter to continue.\n');
58+
pause;
59+
60+
%% =============== Part 3: Implementing Gaussian Kernel ===============
61+
% You will now implement the Gaussian kernel to use
62+
% with the SVM. You should complete the code in gaussianKernel.m
63+
%
64+
fprintf('\nEvaluating the Gaussian Kernel ...\n')
65+
66+
x1 = [1 2 1]; x2 = [0 4 -1]; sigma = 2;
67+
sim = gaussianKernel(x1, x2, sigma);
68+
69+
fprintf(['Gaussian Kernel between x1 = [1; 2; 1], x2 = [0; 4; -1], sigma = %f :' ...
70+
'\n\t%f\n(for sigma = 2, this value should be about 0.324652)\n'], sigma, sim);
71+
72+
fprintf('Program paused. Press enter to continue.\n');
73+
pause;
74+
75+
%% =============== Part 4: Visualizing Dataset 2 ================
76+
% The following code will load the next dataset into your environment and
77+
% plot the data.
78+
%
79+
80+
fprintf('Loading and Visualizing Data ...\n')
81+
82+
% Load from ex6data2:
83+
% You will have X, y in your environment
84+
load('ex6data2.mat');
85+
86+
% Plot training data
87+
plotData(X, y);
88+
89+
fprintf('Program paused. Press enter to continue.\n');
90+
pause;
91+
92+
%% ========== Part 5: Training SVM with RBF Kernel (Dataset 2) ==========
93+
% After you have implemented the kernel, we can now use it to train the
94+
% SVM classifier.
95+
%
96+
fprintf('\nTraining SVM with RBF Kernel (this may take 1 to 2 minutes) ...\n');
97+
98+
% Load from ex6data2:
99+
% You will have X, y in your environment
100+
load('ex6data2.mat');
101+
102+
% SVM Parameters
103+
C = 1; sigma = 0.1;
104+
105+
% We set the tolerance and max_passes lower here so that the code will run
106+
% faster. However, in practice, you will want to run the training to
107+
% convergence.
108+
model= svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma));
109+
visualizeBoundary(X, y, model);
110+
111+
fprintf('Program paused. Press enter to continue.\n');
112+
pause;
113+
114+
%% =============== Part 6: Visualizing Dataset 3 ================
115+
% The following code will load the next dataset into your environment and
116+
% plot the data.
117+
%
118+
119+
fprintf('Loading and Visualizing Data ...\n')
120+
121+
% Load from ex6data3:
122+
% You will have X, y in your environment
123+
load('ex6data3.mat');
124+
125+
% Plot training data
126+
plotData(X, y);
127+
128+
fprintf('Program paused. Press enter to continue.\n');
129+
pause;
130+
131+
%% ========== Part 7: Training SVM with RBF Kernel (Dataset 3) ==========
132+
133+
% This is a different dataset that you can use to experiment with. Try
134+
% different values of C and sigma here.
135+
%
136+
137+
% Load from ex6data3:
138+
% You will have X, y in your environment
139+
load('ex6data3.mat');
140+
141+
% Try different SVM Parameters here
142+
[C, sigma] = dataset3Params(X, y, Xval, yval);
143+
144+
% Train the SVM
145+
model= svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma));
146+
visualizeBoundary(X, y, model);
147+
148+
fprintf('Program paused. Press enter to continue.\n');
149+
pause;
150+

0 commit comments

Comments
 (0)