Support Vector Machines

I’ve heard many times about SVM before, but now I have witnessed its power. 😄

1.2.1 Gaussian Kernel

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

1.2.3 Example Dataset 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
l = [0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30];
mix = realmax;

for c = l
for s = l
model= svmTrain(X, y, c, @(x1, x2) gaussianKernel(x1, x2, s));

p = svmPredict(model, Xval);
m = mean(double(p ~= yval));

if m < mix
C = c;sigma = s;mix = m;
end
end
end

I have written the Gaussian kernel function to accept sigma as a vector input and return a simulated vector, so g = gaussianKernel(x1, x2, s) is a vector and it can skip the unnecessary calculation in the for c = l loop for each s = l. Then, if svmTrain can be calculated as a vector, the for c = l loop can also be skipped.

But it cannot. 😢

What does it have to do with functional programming?

2.1.1 Vocabulary List

1
2
3
4
i = find(strcmp(vocabList, str));
if i
word_indices = [word_indices; i];
end

2.2 Extracting Features from Emails

1
2
3
for i = word_indices
x(i) = 1;
end

Translated by gpt-3.5-turbo