您的位置:首页 > 其它

机器学习2_逻辑回归和标准化的逻辑回归_AndrewNG

2015-10-29 18:56 357 查看
1.在plotData.m中YOUR CODE后(以后的加入位置均是)加入:

% Find Indices of Positive and Negative Examples

pos = find(y==1); neg = find(y == 0);

% Plot Examples

plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, ...

'MarkerSize', 7);

plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', ...

'MarkerSize', 7);

2.在sigmoid.m加入:

g = 1.0 ./ (1.0 + exp(-z));

3.在命令窗口检验sigmoid函数

sigmoid(0)

结果为0.5

再试一个矩阵就可以submit。

4.在costFunction.m

加入

h = sigmoid(X * theta);

J = (1.0/m) * sum(-y .* log(h) - (1.0 - y) .* log(1.0 - h));

grad = (1.0/m) .* X' * (h - y);

然后运行ex2.m,submit.

5.在predict.m

加入:

p = sigmoid(X* theta);

index_1 = find(p > 0.5);

index_0 = find(p <= 0.5);

p(index_1) = ones(size(index_1));

p(index_0) = zeros(size(index_0));

然后运行ex2.m,submit.

6.在costFunctionReg.m

加入

h = sigmoid(X * theta);

J = (1.0/m) * sum(-y .* log(h) - (1.0 - y) .* log(1.0 - h)) + lambda / (2 * m) * norm(theta(2:end))^2;

reg = (lambda/m) .* theta;

reg(1) = 0;

grad = (1.0/m) .* X' * (h - y) + reg;

其中norm是对向量theta的后面n维求模.

可以改变lambda的值得到不同的结果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: