Skip to content

Commit 8a83420

Browse files
Exercise6 - Week7 Finished
1 parent 6e770b8 commit 8a83420

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

machine-learning-ex6/ex6/dataset3Params.m

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
C = 1;
1212
sigma = 0.3;
1313

14+
% Predefine Enum
15+
C_vec = [0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30];
16+
sigma_vec = [0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30];
17+
18+
% Prepare variables
19+
n = length(C_vec);
20+
error_matrix = zeros(n, n);
21+
1422
% ====================== YOUR CODE HERE ======================
1523
% Instructions: Fill in this function to return the optimal C and sigma
1624
% learning parameters found using the cross validation set.
@@ -23,11 +31,21 @@
2331
% mean(double(predictions ~= yval))
2432
%
2533

34+
for i = 1:n
35+
for j = 1:n
36+
C_value = C_vec(i);
37+
sigma_value = sigma_vec(j);
38+
model = svmTrain(X, y, C_value, @(x1, x2) gaussianKernel(x1, x2, sigma_value));
39+
pred = svmPredict(model, Xval);
40+
error_matrix(i, j) = mean(double(pred ~= yval));
41+
end
42+
end
2643

44+
[v, I] = min(error_matrix(:));
45+
[I1,I2] = ind2sub(size(error_matrix),I);
2746

28-
29-
30-
47+
C = C_vec(I1);
48+
sigma = sigma_vec(I2);
3149

3250
% =========================================================================
3351

machine-learning-ex6/ex6/emailFeatures.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
% Total number of words in the dictionary
88
n = 1899;
9-
9+
% Total number of word_indices
10+
m = length(word_indices);
1011
% You need to return the following variables correctly.
1112
x = zeros(n, 1);
1213

@@ -48,7 +49,9 @@
4849
%
4950
%
5051

51-
52+
for i = 1:m
53+
x(word_indices(i)) = 1;
54+
end
5255

5356

5457

machine-learning-ex6/ex6/gaussianKernel.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
%
1717
%
1818

19-
19+
sim = exp(-sum((x1 - x2).^2) / (2 * (sigma.^2)));
2020

2121

2222

machine-learning-ex6/ex6/processEmail.m

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function word_indices = processEmail(email_contents)
1+
function word_indices = processEmail(email_contents)
22
%PROCESSEMAIL preprocesses a the body of an email and
33
%returns a list of word_indices
44
% word_indices = PROCESSEMAIL(email_contents) preprocesses
@@ -97,12 +97,13 @@
9797
% str2). It will return 1 only if the two strings are equivalent.
9898
%
9999

100-
101-
102-
103-
104-
105-
100+
n = length(vocabList);
101+
for i = 1:n
102+
if strcmp(str, vocabList{i}) == 1
103+
word_indices = [word_indices; i];
104+
break;
105+
end
106+
end
106107

107108

108109

0 commit comments

Comments
 (0)