Machine Learning - week 4 - 编程练习

= X' * (h - y)

LrCostFunction 与上一周的有什么不同?

与 week3 的 costFunctionReg 是一样的。Week3 中参考答案没有排除 theta 第一行,但我认为应该排除。而这里排除了。

 如果卡了很久,搜索正确答案然后再对比。

 

oneVsAll

循环 `类别个数` 次

初始化 θ 为,X 的列数 + 1

计算出 θ

添加到 all_theta 第 i 行 

initial_theta = zeros(n+1, 1);

for i = 1:num_labels
  options = optimset('GradObj', 'on', 'MaxIter', 50);
  [theta] = fmincg(@(t)(lrCostFunction(t, X, (y==i), lambda)), ...
                   initial_theta, options);
  all_theta(i, :) = theta;
end

predictOneVsAll

预测 label 是什么。每行对应的是什么类别。

for j = 1:m,
% predict each row
    [_, p(j)] = max(X(j, :) * all_theta');
end;

  这里也是取最大值,而不是固定值 1。

 

参考

https://github.com/emersonmoretto/mlclass-ex3/blob/master/predictOneVsAll.m  

posted @ 2017-09-08 10:51  Jay54520  阅读(579)  评论(0编辑  收藏  举报