123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- clear ; close all; clc
- fprintf('Loading data ...\n');
- data = load('ex1data2.txt');
- X = data(:, 1:2);
- y = data(:, 3);
- m = length(y);
- fprintf('First 10 examples from the dataset: \n');
- fprintf(' x = [%.0f %.0f], y = %.0f \n', [X(1:10,:) y(1:10,:)]');
- fprintf('Program paused. Press enter to continue.\n');
- pause;
- fprintf('Normalizing Features ...\n');
- [X mu sigma] = featureNormalize(X);
- X = [ones(m, 1) X];
- fprintf('Running gradient descent ...\n');
- alpha = 0.01;
- num_iters = 400;
- theta = zeros(3, 1);
- [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);
- figure;
- plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2);
- xlabel('Number of iterations');
- ylabel('Cost J');
- fprintf('Theta computed from gradient descent: \n');
- fprintf(' %f \n', theta);
- fprintf('\n');
- price = 0;
- fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
- '(using gradient descent):\n $%f\n'], price);
- fprintf('Program paused. Press enter to continue.\n');
- pause;
- fprintf('Solving with normal equations...\n');
- data = csvread('ex1data2.txt');
- X = data(:, 1:2);
- y = data(:, 3);
- m = length(y);
- X = [ones(m, 1) X];
- theta = normalEqn(X, y);
- fprintf('Theta computed from the normal equations: \n');
- fprintf(' %f \n', theta);
- fprintf('\n');
- price = 0;
- fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
- '(using normal equations):\n $%f\n'], price);
|