Coursera機器學習-吳恩達
Octave 代碼
背景:使用邏輯迴歸預測學生是否會被大學錄取。
% 1.讀取訓練集,並打印正負樣本:
data = load('ex2data1.txt');
X = data(:, [1, 2]);
y = data(:, 3);
plotData(X, y);
% 函數plotData:
function plotData(X, y)
figure; hold on;
% 打印正樣本和負樣本
positive = find(y == 1);
negative = find(y == 0);
plot(X(positive, 1), X(positive, 2), 'k+', 'LineWidth', 2, 'MarkerSize', 7);
plot(X(negative, 1), X(negative, 2), 'ko', 'MarkerFaceColor', 'y', 'MarkerSize', 7);
hold off;
end
hold on;
% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')
legend('Admitted', 'Not admitted')
hold off;
% 2.計算代價函數J 和下降梯度
[m, n] = size(X);
X = [ones(m, 1) X];
initial_theta = zeros(n + 1, 1);
% 計算代價函數,使用初始化全爲0的計算代價
[cost, grad] = costFunction(initial_theta, X, y);
% 函數costFunction:
function [J, grad] = costFunction(theta, X, y)
m = length(y);
J = 0;
grad = zeros(size(theta));
% 計算代價和梯度grad,公式如下圖:
J = -1 / m *(sum(y .* log(sigmoid(X * theta)) + (1 - y) .* log(1 - sigmoid(X * theta))));
grad = 1 / m * X' * (sigmoid(X * theta) - y);
end
fprintf('\nCost at test theta: %f\n', cost);
fprintf(' %f \n', grad);
% 再次計算代價函數,使用測試的theta:
test_theta = [-24; 0.2; 0.2];
[cost, grad] = costFunction(test_theta, X, y);
fprintf('\nCost at test theta: %f\n', cost);
fprintf(' %f \n', grad);
2.1 假設函數由來:
邏輯迴歸是分類問題,若使用線性迴歸的假設函數,h(x)會不是確定的範圍的值。
如下圖,h(x)>0.5 是 yes, h(x)<0.5 是 no,這裏分界是0.5,但到其他問題分界線就會變
2.1.1 因此我們需要將結果縮到 [0,1] 中,這樣方便判斷,
分界線永遠爲0.5,函數值大於0.5就爲yes,小於0.5爲no
因此使用 sigmoid函數,並把之前線性迴歸的假設函數theta * x
帶入:
這樣 theta * x > 0,則 g(z) > 0.5,則 判斷爲1
theta * x < 0,則 g(z) < 0.5,則 判斷爲0
因此 g(z) 即爲邏輯迴歸的假設函數
2.2 代價函數J 由來
若將上述假設函數 g(z) 帶入線性迴歸的代價函數,得到的不是凸函數,
即有很多局部最小值,不利於梯度下降:
2.2.1 因此不能用線性迴歸的代價函數了,用個新的:
這樣是凸函數,然後合併一下:
2.2.3 上述就是邏輯迴歸的代價函數J,還可以優化一下,將各項的和用矩陣的方式
實現:
2.3 梯度下降的由來:
首先由梯度下降的一般公式,然後將代價函數帶入,並對 theta 求偏導數,
再乘步長 a :
% 3. 使用高級優化(Advanced Optimization)來更新 theta
% 'GradObj', -'on':設置梯度目標參數爲on打開狀態
% 'MaxIter', -100:設置最大迭代次數400
options = optimset('GradObj', 'on', 'MaxIter', 400);
% 運行 fminunc 就可以得到最佳 theta,並打印
% @是函數指針
[theta, cost] = fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
fprintf('Cost at theta found by fminunc: %f\n', cost);
fprintf(' %f \n', theta);
plotDecisionBoundary(theta, X, y);
hold on;
xlabel('Exam 1 score')
ylabel('Exam 2 score')
legend('Admitted', 'Not admitted')
hold off;
% 函數plotDecisionBoundary:
function plotDecisionBoundary(theta, X, y)
% 先打印訓練集數據
plotData(X(:,2:3), y);
hold on
%這裏不太懂怎麼打印
% else後是打印便捷,先隨機生成 -1到1.5 50個數字,
% 然後用mapFeatures生成 X1, X2, X1.^2, X2.^2, X1*X2, X1*X2.^2, etc..
% 足夠特徵後打印
if size(X, 2) <= 3
plot_x = [min(X(:,2))-2, max(X(:,2))+2];
plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));
plot(plot_x, plot_y)
legend('Admitted', 'Not admitted', 'Decision Boundary')
axis([30, 100, 30, 100])
else
u = linspace(-1, 1.5, 50);
v = linspace(-1, 1.5, 50);
z = zeros(length(u), length(v));
for i = 1:length(u)
for j = 1:length(v)
z(i,j) = mapFeature(u(i), v(j))*theta;
end
end
z = z';
contour(u, v, z, [0, 0], 'LineWidth', 2)
end
hold off
end
% 4.預測一波,並判斷準確性
% 假設函數:hθ(x) = g(θ'x) = sgmoid(x * theta)
prob = sigmoid([1 45 85] * theta);
fprintf(['For a student with scores 45 and 85, we predict an admission ' ...
'probability of %f\n'], prob);
fprintf('Expected value: 0.775 +/- 0.002\n\n');
% 基於訓練集計算準確性
p = predict(theta, X);
fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);
% 函數:predict:
function p = predict(theta, X)
m = size(X, 1);
p = zeros(m, 1);
temp = sigmoid(X * theta);
p = temp > 0.5;
end