Stanford 机器学习 Week3 作业 Logistic Regression

Visualizing the data

pos = find(y==1); neg = find(y == 0);
plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, ...
     'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', ...
     'MarkerSize', 7);

Warmup exercise: sigmoid function

g = 1 ./ (1 + exp(-z)) 

Cost function and gradient

J = sum( -y .* log(sigmoid(X*theta)) - (1 - y) .* log(1 - sigmoid(X*theta))) / m;

for i = 1:size(theta)
    grad(i) = sum((sigmoid(X*theta) - y) .* X(:,i)) / m;
end;

Predict

p = zeros(m, 1);

for i = 1:m
    tmp = sigmoid(X(i,:)*theta);
    if tmp < 0.5
        p(i) = 0;
    else
        p(i) = 1;
    end;
end;

Regularized logistic regression

m = length(y);
J = 0;
grad = zeros(size(theta));
J = sum( -y .* log(sigmoid(X*theta)) - (1 - y) .* log(1 - sigmoid(X*theta))) / m + theta(2:end)' * theta(2:end) * lambda / m / 2;

for i = 1:size(theta)
    grad(i) = sum((sigmoid(X*theta) - y) .* X(:,i)) / m;
    if i > 1
        grad(i) = grad(i) + lambda / m *theta(i);
    end
end

你可能感兴趣的:(机器学习,octave)