gaussianKernel.m
sim = exp(-sum((x1-x2).^2)/(sigma.^2*2)); % similarity(x,l^(1)) = exp(-(||x-l^(1)||^(2))/2Sigma^2)
dataset3Params.m
steps = [0.01,0.03,0.1,0.3,1,3,10,30];
minError = Inf;
minC = Inf;
minSigma = Inf;
for i = 1:length(steps) %遍历所有的选项
for j = 1:length(steps) %遍历所有的选项
currC = steps(i); %选择C
currSigma = steps(j); %选择Sigma
%训练模型
model = svmTrain(X, y, currC, @(x1, x2) gaussianKernel(x1, x2, currSigma));
predictions = svmPredict(model, Xval); %进行预测
error = mean(double(predictions ~= yval)); %计算误差
%选择最小误差的参数
if(error < minError)
minError = error;
minC = currC;
minSigma = currSigma;
end
end
end
C = minC;
sigma = minSigma;
processEmail.m
%遍历词汇表
for i = 1:length(vocabList)
if(strcmp(vocabList(i), str)) %如果相等
word_indices = [word_indices; i]; %添加索引
break;
end
end
emailFeatures.m
for i = 1:length(word_indices) %遍历单词向量表
x(word_indices(i)) = 1 %将对应的值置为1
end