Skip to content

Commit d6d3d37

Browse files
author
mlinderman
committed
more updates
1 parent 2ebd73b commit d6d3d37

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

.DS_Store

0 Bytes
Binary file not shown.

machine-learning-ex1/ex1/ex1_multi.m

+30-20
Original file line numberDiff line numberDiff line change
@@ -82,49 +82,58 @@
8282
fprintf('Running gradient descent ...\n');
8383

8484
% Choose some alpha value
85-
alpha_histories = zeros(10,1)
85+
alpha_histories = []
8686
alpha = .01;
87-
num_iters = 150;
88-
theta = zeros(3, 1)
87+
num_iters = 200;
8988

9089
% 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;
9699
end
97100

98101
% Plot the convergence graph
99102
figure;
100103
xlabel('Number of iterations');
101104
ylabel('Cost J');
102105

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');
107115
hold on;
108116
end
109117

110118
% 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
114123

115124
% Estimate the price of a 1650 sq-ft, 3 br house
116125
% ====================== YOUR CODE HERE ======================
117126
% Recall that the first column of X is all-ones. Thus, it does
118127
% not need to be normalized.
119128

120-
testX = [1650, 3]
129+
testX = [1650, 3];
121130

122131
% to normalize a single row of features, you want to use the values you found
123132
% earlier for all samples for mean and standard deviation
124133
testX = (testX - mu) ./ sigma
125134

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
128137
% two equally sized vectors, theta and testX (one column vector, one row vector)
129138
% remember that theta1*x1 + theta2*x2 + theta3*x3... is your hypothesis
130139
% and now you have thetas calculated using gradient descent
@@ -165,7 +174,7 @@
165174
X = [ones(m, 1) X];
166175

167176
% Calculate the parameters from the normal equation
168-
theta = normalEqn(X, y);
177+
theta = normalEqn(X, y)
169178

170179
% Display normal equation's result
171180
fprintf('Theta computed from the normal equations: \n');
@@ -175,7 +184,8 @@
175184

176185
% Estimate the price of a 1650 sq-ft, 3 br house
177186
% ====================== YOUR CODE HERE ======================
178-
price = 0; % You should change this
187+
188+
price = sum(theta' .* [1, 1650, 3]);
179189

180190

181191
% ============================================================

machine-learning-ex1/ex1/gradientDescentMulti.m

+4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919

2020
newThetas = zeros([size(theta), 1]);
2121
for idx = 1:size(theta) % size of the largest dimension, the row count, in this instance
22+
% using X*theta, not theta'X because X samples are rows, not columns and to do this vector/matrix
23+
% multiplication, we want the features for each row of X to be multiplied to the corresponding thetas.'
2224
newThetas(idx, 1) = theta(idx) - alpha * 1/m * sum((X * theta - y) .* X(:, idx));
2325
end
26+
2427
theta = [newThetas];
2528

2629
% ============================================================
@@ -31,3 +34,4 @@
3134
end
3235

3336
end
37+

machine-learning-ex1/ex1/normalEqn.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
% ---------------------- Sample Solution ----------------------
1414

15-
15+
theta = pinv(X'*X)*(X'*y);
1616

1717

1818
% -------------------------------------------------------------

machine-learning-ex1/ex1/token.mat

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Created by Octave 4.4.1, Sun Sep 09 15:55:20 2018 EDT <mlinderm@Mark-Lindermans-MacBook-Pro-2.local>
1+
# Created by Octave 4.4.0, Sat Sep 15 15:09:19 2018 EDT <marklinderman@Mark Linderman's MacBook Pro>
22
# name: email
33
# type: sq_string
44
# elements: 1

0 commit comments

Comments
 (0)