|
82 | 82 | fprintf('Running gradient descent ...\n');
|
83 | 83 |
|
84 | 84 | % Choose some alpha value
|
85 |
| -alpha_histories = zeros(10,1) |
| 85 | +alpha_histories = [] |
86 | 86 | alpha = .01;
|
87 |
| -num_iters = 150; |
88 |
| -theta = zeros(3, 1) |
| 87 | +num_iters = 200; |
89 | 88 |
|
90 | 89 | % Init Theta and Run Gradient Descent 10 times with varying alphas, saving results
|
91 |
| -J_histories = [10, 2] |
92 |
| -for i = length(J_histories) |
93 |
| - J_histories(i, 1:2) = gradientDescentMulti(X, y, theta, alpha, num_iters); % returns [theta, J_history] |
94 |
| - alpha_histories(i) = alpha |
95 |
| - alpha = alpha * 3 |
| 90 | +J_histories = {}; |
| 91 | +ans_thetas = {}; |
| 92 | +for i = 1:8 |
| 93 | + theta = zeros(3, 1); |
| 94 | + [t, h] = gradientDescentMultiVectorized(X, y, theta, alpha, num_iters); % returns [theta, J_history] |
| 95 | + ans_thetas{i} = t; |
| 96 | + J_histories{i} = h; |
| 97 | + alpha_histories = [alpha_histories; alpha]; |
| 98 | + alpha = alpha * 2; |
96 | 99 | end
|
97 | 100 |
|
98 | 101 | % Plot the convergence graph
|
99 | 102 | figure;
|
100 | 103 | xlabel('Number of iterations');
|
101 | 104 | ylabel('Cost J');
|
102 | 105 |
|
103 |
| -for hist = 1:length(J_histories) |
104 |
| - x = [1:length(J_histories(hist, 2))] |
105 |
| - y = J_histories(hist, 2) |
106 |
| - plot(1:numel(J_histories(hist)()), J_history(hist), '-b', 'LineWidth', 2); |
| 106 | +for i = 1:length(J_histories) |
| 107 | + if sum(isinf(J_histories{i})) > 0 |
| 108 | + break |
| 109 | + endif |
| 110 | + x = [1:length(J_histories{i})]; |
| 111 | + y = J_histories{i}; |
| 112 | + plot(x, y, '-'+i, 'LineWidth', 2); |
| 113 | + xlabel('Number of iterations'); |
| 114 | + ylabel('Cost J'); |
107 | 115 | hold on;
|
108 | 116 | end
|
109 | 117 |
|
110 | 118 | % Display gradient descent's result
|
111 |
| -fprintf('Theta computed from gradient descent: \n'); |
112 |
| -fprintf(' %f \n', theta); |
113 |
| -fprintf('\n'); |
| 119 | +fprintf('Thetas computed from gradient descent: \n'); |
| 120 | +for i = 1:length(ans_thetas) |
| 121 | + fprintf('run #%i: %.0f, %.0f, %.0f \n', i, ans_thetas{i}); |
| 122 | +endfor |
114 | 123 |
|
115 | 124 | % Estimate the price of a 1650 sq-ft, 3 br house
|
116 | 125 | % ====================== YOUR CODE HERE ======================
|
117 | 126 | % Recall that the first column of X is all-ones. Thus, it does
|
118 | 127 | % not need to be normalized.
|
119 | 128 |
|
120 |
| -testX = [1650, 3] |
| 129 | +testX = [1650, 3]; |
121 | 130 |
|
122 | 131 | % to normalize a single row of features, you want to use the values you found
|
123 | 132 | % earlier for all samples for mean and standard deviation
|
124 | 133 | testX = (testX - mu) ./ sigma
|
125 | 134 |
|
126 |
| -testX = [1, testX] |
127 |
| -theta |
| 135 | +testX = [1, testX]; |
| 136 | +theta = ans_thetas{length(ans_thetas)}; % the last thetas calculated in the iteration above with different alphas |
128 | 137 | % two equally sized vectors, theta and testX (one column vector, one row vector)
|
129 | 138 | % remember that theta1*x1 + theta2*x2 + theta3*x3... is your hypothesis
|
130 | 139 | % and now you have thetas calculated using gradient descent
|
|
165 | 174 | X = [ones(m, 1) X];
|
166 | 175 |
|
167 | 176 | % Calculate the parameters from the normal equation
|
168 |
| -theta = normalEqn(X, y); |
| 177 | +theta = normalEqn(X, y) |
169 | 178 |
|
170 | 179 | % Display normal equation's result
|
171 | 180 | fprintf('Theta computed from the normal equations: \n');
|
|
175 | 184 |
|
176 | 185 | % Estimate the price of a 1650 sq-ft, 3 br house
|
177 | 186 | % ====================== YOUR CODE HERE ======================
|
178 |
| -price = 0; % You should change this |
| 187 | + |
| 188 | +price = sum(theta' .* [1, 1650, 3]); |
179 | 189 |
|
180 | 190 |
|
181 | 191 | % ============================================================
|
|
0 commit comments