Skip to content

Commit 38a4144

Browse files
authored
Add files via upload
1 parent 5e86c78 commit 38a4144

28 files changed

+3657
-0
lines changed

machine-learning-ex1/ex1.pdf

478 KB
Binary file not shown.
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function J = computeCost(X, y, theta)
2+
%COMPUTECOST Compute cost for linear regression
3+
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
4+
% parameter for linear regression to fit the data points in X and y
5+
6+
% Initialize some useful values
7+
m = length(y); % number of training examples
8+
9+
% You need to return the following variables correctly
10+
J = 0;
11+
12+
% ====================== YOUR CODE HERE ======================
13+
% Instructions: Compute the cost of a particular choice of theta
14+
% You should set J to the cost.
15+
J = sum((X * theta - y).^2) / (2*m); % X(79,2) theta(2,1)
16+
17+
18+
19+
20+
% =========================================================================
21+
22+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function J = computeCostMulti(X, y, theta)
2+
%COMPUTECOSTMULTI Compute cost for linear regression with multiple variables
3+
% J = COMPUTECOSTMULTI(X, y, theta) computes the cost of using theta as the
4+
% parameter for linear regression to fit the data points in X and y
5+
6+
% Initialize some useful values
7+
m = length(y); % number of training examples
8+
9+
% You need to return the following variables correctly
10+
J = 0;
11+
12+
% ====================== YOUR CODE HERE ======================
13+
% Instructions: Compute the cost of a particular choice of theta
14+
% You should set J to the cost.
15+
J = sum((X * theta - y).^2) / (2*m);
16+
17+
18+
19+
20+
% =========================================================================
21+
22+
end

machine-learning-ex1/ex1/ex1.m

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
%% Machine Learning Online Class - Exercise 1: Linear Regression
2+
3+
% Instructions
4+
% ------------
5+
%
6+
% This file contains code that helps you get started on the
7+
% linear exercise. You will need to complete the following functions
8+
% in this exericse:
9+
%
10+
11+
% warmUpExercise.m
12+
% plotData.m
13+
% gradientDescent.m
14+
% computeCost.m
15+
% gradientDescentMulti.m
16+
% computeCostMulti.m
17+
% featureNormalize.m
18+
% normalEqn.m
19+
%
20+
% For this exercise, you will not need to change any code in this file,
21+
% or any other files other than those mentioned above.
22+
%
23+
% x refers to the population size in 10,000s
24+
% y refers to the profit in $10,000s
25+
%
26+
27+
%% Initialization
28+
clear ; close all; clc
29+
30+
%% ==================== Part 1: Basic Function ====================
31+
% Complete warmUpExercise.m
32+
fprintf('Running warmUpExercise ... \n');
33+
fprintf('5x5 Identity Matrix: \n');
34+
warmUpExercise()
35+
36+
fprintf('Program paused. Press enter to continue.\n');
37+
pause;
38+
39+
40+
%% ======================= Part 2: Plotting =======================
41+
fprintf('Plotting Data ...\n')
42+
data = load('ex1data1.txt');
43+
X = data(:, 1); y = data(:, 2);
44+
m = length(y); % number of training examples
45+
46+
% Plot Data
47+
% Note: You have to complete the code in plotData.m
48+
plotData(X, y);
49+
50+
fprintf('Program paused. Press enter to continue.\n');
51+
pause;
52+
53+
%% =================== Part 3: Cost and Gradient descent ===================
54+
55+
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
56+
theta = zeros(2, 1); % initialize fitting parameters
57+
58+
% Some gradient descent settings
59+
iterations = 1500;
60+
alpha = 0.01;
61+
62+
fprintf('\nTesting the cost function ...\n')
63+
% compute and display initial cost
64+
J = computeCost(X, y, theta);
65+
fprintf('With theta = [0 ; 0]\nCost computed = %f\n', J);
66+
fprintf('Expected cost value (approx) 32.07\n');
67+
68+
% further testing of the cost function
69+
J = computeCost(X, y, [-1 ; 2]);
70+
fprintf('\nWith theta = [-1 ; 2]\nCost computed = %f\n', J);
71+
fprintf('Expected cost value (approx) 54.24\n');
72+
73+
fprintf('Program paused. Press enter to continue.\n');
74+
pause;
75+
76+
fprintf('\nRunning Gradient Descent ...\n')
77+
% run gradient descent
78+
theta = gradientDescent(X, y, theta, alpha, iterations);
79+
80+
% print theta to screen
81+
fprintf('Theta found by gradient descent:\n');
82+
fprintf('%f\n', theta);
83+
fprintf('Expected theta values (approx)\n');
84+
fprintf(' -3.6303\n 1.1664\n\n');
85+
86+
% Plot the linear fit
87+
hold on; % keep previous plot visible
88+
plot(X(:,2), X*theta, '-')
89+
legend('Training data', 'Linear regression')
90+
hold off % don't overlay any more plots on this figure
91+
92+
% Predict values for population sizes of 35,000 and 70,000
93+
predict1 = [1, 3.5] *theta;
94+
fprintf('For population = 35,000, we predict a profit of %f\n',...
95+
predict1*10000);
96+
predict2 = [1, 7] * theta;
97+
fprintf('For population = 70,000, we predict a profit of %f\n',...
98+
predict2*10000);
99+
100+
fprintf('Program paused. Press enter to continue.\n');
101+
pause;
102+
103+
%% ============= Part 4: Visualizing J(theta_0, theta_1) =============
104+
fprintf('Visualizing J(theta_0, theta_1) ...\n')
105+
106+
% Grid over which we will calculate J
107+
theta0_vals = linspace(-10, 10, 100);
108+
theta1_vals = linspace(-1, 4, 100);
109+
110+
% initialize J_vals to a matrix of 0's
111+
J_vals = zeros(length(theta0_vals), length(theta1_vals));
112+
113+
% Fill out J_vals
114+
for i = 1:length(theta0_vals)
115+
for j = 1:length(theta1_vals)
116+
t = [theta0_vals(i); theta1_vals(j)];
117+
J_vals(i,j) = computeCost(X, y, t);
118+
end
119+
end
120+
121+
122+
% Because of the way meshgrids work in the surf command, we need to
123+
% transpose J_vals before calling surf, or else the axes will be flipped
124+
J_vals = J_vals';
125+
% Surface plot
126+
figure;
127+
surf(theta0_vals, theta1_vals, J_vals)
128+
xlabel('\theta_0'); ylabel('\theta_1');
129+
130+
% Contour plot
131+
figure;
132+
% Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100
133+
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20))
134+
xlabel('\theta_0'); ylabel('\theta_1');
135+
hold on;
136+
plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);

machine-learning-ex1/ex1/ex1_multi.m

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
%% Machine Learning Online Class
2+
% Exercise 1: Linear regression with multiple variables
3+
%
4+
% Instructions
5+
% ------------
6+
%
7+
% This file contains code that helps you get started on the
8+
% linear regression exercise.
9+
%
10+
% You will need to complete the following functions in this
11+
% exericse:
12+
%
13+
% warmUpExercise.m
14+
% plotData.m
15+
% gradientDescent.m
16+
% computeCost.m
17+
% gradientDescentMulti.m
18+
% computeCostMulti.m
19+
% featureNormalize.m
20+
% normalEqn.m
21+
%
22+
% For this part of the exercise, you will need to change some
23+
% parts of the code below for various experiments (e.g., changing
24+
% learning rates).
25+
%
26+
27+
%% Initialization
28+
29+
%% ================ Part 1: Feature Normalization ================
30+
31+
%% Clear and Close Figures
32+
clear ; close all; clc
33+
34+
fprintf('Loading data ...\n');
35+
36+
%% Load Data
37+
data = load('ex1data2.txt');
38+
X = data(:, 1:2);
39+
y = data(:, 3);
40+
m = length(y);
41+
42+
% Print out some data points
43+
fprintf('First 10 examples from the dataset: \n');
44+
fprintf(' x = [%.0f %.0f], y = %.0f \n', [X(1:10,:) y(1:10,:)]');
45+
46+
fprintf('Program paused. Press enter to continue.\n');
47+
pause;
48+
49+
% Scale features and set them to zero mean
50+
fprintf('Normalizing Features ...\n');
51+
52+
[X mu sigma] = featureNormalize(X);
53+
54+
% Add intercept term to X
55+
X = [ones(m, 1) X];
56+
57+
58+
%% ================ Part 2: Gradient Descent ================
59+
60+
% ====================== YOUR CODE HERE ======================
61+
% Instructions: We have provided you with the following starter
62+
% code that runs gradient descent with a particular
63+
% learning rate (alpha).
64+
%
65+
% Your task is to first make sure that your functions -
66+
% computeCost and gradientDescent already work with
67+
% this starter code and support multiple variables.
68+
%
69+
% After that, try running gradient descent with
70+
% different values of alpha and see which one gives
71+
% you the best result.
72+
%
73+
% Finally, you should complete the code at the end
74+
% to predict the price of a 1650 sq-ft, 3 br house.
75+
%
76+
% Hint: By using the 'hold on' command, you can plot multiple
77+
% graphs on the same figure.
78+
%
79+
% Hint: At prediction, make sure you do the same feature normalization.
80+
%
81+
82+
fprintf('Running gradient descent ...\n');
83+
84+
% Choose some alpha value
85+
alpha = 0.01;
86+
num_iters = 8500;
87+
88+
% Init Theta and Run Gradient Descent
89+
theta = zeros(3, 1);
90+
[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);
91+
92+
% Plot the convergence graph
93+
figure;
94+
plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2);
95+
xlabel('Number of iterations');
96+
ylabel('Cost J');
97+
98+
% Display gradient descent's result
99+
fprintf('Theta computed from gradient descent: \n');
100+
fprintf(' %f \n', theta);
101+
fprintf('\n');
102+
103+
% Estimate the price of a 1650 sq-ft, 3 br house
104+
% ====================== YOUR CODE HERE ======================
105+
% Recall that the first column of X is all-ones. Thus, it does
106+
% not need to be normalized.
107+
%price = 0; % You should change this
108+
price = [1 (([1650 3]-mu) ./ sigma)] * theta ;
109+
110+
111+
% ============================================================
112+
113+
fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
114+
'(using gradient descent):\n $%f\n'], price);
115+
116+
fprintf('Program paused. Press enter to continue.\n');
117+
pause;
118+
119+
%% ================ Part 3: Normal Equations ================
120+
121+
fprintf('Solving with normal equations...\n');
122+
123+
% ====================== YOUR CODE HERE ======================
124+
% Instructions: The following code computes the closed form
125+
% solution for linear regression using the normal
126+
% equations. You should complete the code in
127+
% normalEqn.m
128+
%
129+
% After doing so, you should complete this code
130+
% to predict the price of a 1650 sq-ft, 3 br house.
131+
%
132+
133+
%% Load Data
134+
data = csvread('ex1data2.txt');
135+
X = data(:, 1:2);
136+
y = data(:, 3);
137+
m = length(y);
138+
139+
% Add intercept term to X
140+
X = [ones(m, 1) X];
141+
142+
% Calculate the parameters from the normal equation
143+
theta = normalEqn(X, y);
144+
145+
% Display normal equation's result
146+
fprintf('Theta computed from the normal equations: \n');
147+
fprintf(' %f \n', theta);
148+
fprintf('\n');
149+
150+
151+
% Estimate the price of a 1650 sq-ft, 3 br house
152+
% ====================== YOUR CODE HERE ======================
153+
%price = 0; % You should change this
154+
price = [1 1650 3] * theta ;
155+
156+
% ============================================================
157+
158+
fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
159+
'(using normal equations):\n $%f\n'], price);
160+

0 commit comments

Comments
 (0)