StephenFengz的博客

一个小程序猿的学习历程

Coursera机器学习 week7 assignment

仅供思路参考,代码最好还是自己独立完成。


gaussianKernel.m:

sim = exp(- sum((x1-x2).^2) / (2*(sigma^2)) );


dataset3Params.m:

lambda_all = [0.01 0.03 0.1 0.3 1 3 10 30]';
len_all = size(lambda_all, 1);
error_matrix = zeros(len_all, len_all);

for i = 1:len_all
  now_C = lambda_all(i);
  for j = 1:len_all
    now_sigma = lambda_all(j);
    model = svmTrain(X, y, now_C, @(x1, x2) gaussianKernel(x1, x2, now_sigma)); 
    predictions = svmPredict(model, Xval);
    error = mean(double(predictions ~= yval));
    
    error_matrix(i, j) = error;
  end
end

ans_min = min(error_matrix(:));  %找出(8*8)中的最小值
[index_C, index_sigma] = find(error_matrix==ans_min);  %找出该最小值在(8*8)中的行、列索引

C = lambda_all(index_C);
sigma = lambda_all(index_sigma);


processEmail.m:

for i = 1:length(vocabList)
      if(strcmp(str, vocabList{i}) == 1)
        word_indices = [word_indices ; i];
        break;
      else
        continue;
      end
    end


emailFeatures.m:

for i = 1:length(word_indices)
  x(word_indices(i)) = 1;
end


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/StephenFengz/article/details/52382384
文章标签: 机器学习 Andrew Ng
个人分类: 机器学习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Coursera机器学习 week7 assignment

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭