仅列出核心代码:
1.gaussianKernel.m
sim = exp(-sum((x1 - x2).^2) /(2*sigma^2));
2.dataset3Params.m
TD = [0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30];
pre_err = zeros(length(TD));
for i = 1:length(TD)
for j = 1:length(TD)
C = TD(i);
sigma = TD(j);
model= svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma));
predictions = svmPredict(model, Xval);
pre_err(i, j) = mean(double(predictions ~= yval));
end
end
mm = min(min(pre_err));
[ind_C, ind_sigma] = find(pre_err == mm);
C = TD(ind_C);
sigma = TD(ind_sigma);
3.processEmail.m
for i = 1:length(vocabList)
v = strcmp(str, vocabList(i));
if v==1
word_indices = [word_indices ; i];
end
end
4.emailFeatures.m
x(word_indices) = 1;