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); %选择CcurrSigma = 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;endend
endC = minC;
sigma = minSigma;
processEmail.m
%遍历词汇表for i = 1:length(vocabList)if(strcmp(vocabList(i), str)) %如果相等word_indices = [word_indices; i]; %添加索引break;endend
emailFeatures.m
for i = 1:length(word_indices) %遍历单词向量表x(word_indices(i)) = 1 %将对应的值置为1
end