11. %% Load Data
data = load('ex1data2.txt');
X = data(:, 1:2);
y = data(:, 3);
1. Xを正規化
2. 最急降下法でコスト関数最小化→パラメータ推定
(J(Θ)の値ををグラフで確認しつつ)
2’. 正規方程式による方法でパラメータ推定
11
Assignment(Multiple Variablesのみ)
データの準備
ex1data2.txt
解く順序
:
家のサイズ(feet^2)
ベットルーム数 家の価格
問題:学習データを使って、家のサイズとベッドルーム数
から、家の価格を予測する
12. 12
function [X_norm, mu, sigma] = featureNormalize(X)
mu = mean(X); % (1 x 2)
sigma = std(X); % (1 x 2)
mu_m = ones(length(X), 1) * mu; % (m x 1)*(1 x 2)=(m x 2)
sig_m = ones(length(X), 1) * sigma; % (m x 1)*(1 x 2)=(m x 2)
X_norm = (X - mu_m) ./ sig_m; % OK (m x 2)
%X_norm = (X - mu_m) / sig_m; % NG (m x m)
end
Feature Normalization
featureNormalize.m
[X mu sigma] = featureNormalize(X);
X = data(:, 1:2); % (m x 2)
y = data(:, 3); % (m x 1)
14. 14
% Choose some alpha value
alpha = 0.01;
num_iters = 400;
% Init Theta and Run Gradient Descent
theta = zeros(3, 1);
[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);
Gradient Descent
% Add intercept term to X
X = [ones(m, 1) X]; % (m x 3)
% Estimate the price of a 1650 sq-ft, 3 br house
t = [1650, 3];
price = [1, (t-mu)./sigma] * theta; 正規化が必要(1以外)
[X mu sigma] = featureNormalize(X); Xを正規化
15. 15
function [theta, J_history] = gradientDescentMulti(X, y, theta,
alpha, num_iters)
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
h = X * theta;
theta = theta - alpha * 1.0/m * X' * (h-y);
% Save the cost J in every iteration
J_history(iter) = computeCostMulti(X, y, theta);
end
end
gradientDescentMulti.m
16. 16
computeCostMulti.m
function J = computeCostMulti(X, y, theta)
% number of training examples
m = length(y);
% non-vectorized form
%h = X*theta;
%J = 1.0/(2.0*m) * sum((h - y).^2);
% vectorized form
J = 1.0/(2.0*m) * (X*theta-y)' * (X*theta-y);
end
19. 19
Normal Equations
function [theta] = normalEqn(X, y)
theta = pinv(X'*X) * X' * y;
end
% Calculate the parameters from the normal equation
theta = normalEqn(X, y);
% Estimate the price of a 1650 sq-ft, 3 br house
price = [1, 1650, 3] * theta;
normalEqn.m