Machine Learning 第六波编程作业——Support Vector Machines

仅列出核心代码:

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;

课程地址:https://www.coursera.org/course/ml

Advertisements
  1. 本来想坚持不看这里的答案的,但是被第二题卡得很烦躁,最后还是忍不住来看了一下……
    原来要用svmTrain这个隐藏函数啊。之前的指导都太亲切了,一切手把手教,这次绕了个小弯,有点意外。
    取error最小的C和sigma那里,我是在循环里用if语句做的,想不到这个也可以转换成矩阵运算。
    我想问一下博主之前有过octave或者matlab的使用经验吗?

    • 有的,我的专业就是信号处理,Matlab是常用工具。

  2. 第3题的另一种语法:
    [index, err] = cellidx( cellstr( vocabList ), str );
    if index ~= 0
    word_indices = [word_indices; index ];
    end

  1. No trackbacks yet.

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s