支持向量机

以前听过很多次 SVM,现在见识到了它的厉害。😄

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

我把高斯核函数写成可以把 sigma 作为一个向量传递,得到一个模拟向量,因此g = gaussianKernel(x1, x2, s)是一个向量,可以跳过for c = l循环中多余的计算for s = l。那么如果svmTrain可以作为一个向量计算,for c = l也可以跳过。

但它不能。😢

它与函数式编程有什么联系吗?

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