Skip to content

Commit 642800c

Browse files
author
Daniel Petry
committed
initial commit - warm up exercise works
1 parent eaa6665 commit 642800c

13 files changed

+689
-0
lines changed
Binary file not shown.

pandas/ex1/computeCost.py

+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+
16+
J = (((X*theta) - y)' * ((X*theta) - y))/(2 * m);
17+
18+
19+
20+
% =========================================================================
21+
22+
end

pandas/ex1/computeCostMulti.py

+22
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+
16+
J = (((X*theta) - y)' * ((X*theta) - y))/(2 * m);
17+
18+
19+
20+
% =========================================================================
21+
22+
end

pandas/ex1/ex1.py

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

pandas/ex1/ex1_multi.py

+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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+
num_iters = 200;
86+
87+
% Init Theta and Run Gradient Descent
88+
theta = zeros(3, 1);
89+
alpha = 0.1;
90+
[theta, J_history_1] = gradientDescentMulti(X, y, theta, alpha, num_iters);
91+
alpha = 0.3;
92+
theta = zeros(3, 1);
93+
[theta, J_history_2] = gradientDescentMulti(X, y, theta, alpha, num_iters);
94+
alpha = 1;
95+
theta = zeros(3, 1);
96+
[theta, J_history_3] = gradientDescentMulti(X, y, theta, alpha, num_iters);
97+
98+
% Plot the convergence graph
99+
figure;
100+
plot(1:numel(J_history_1), J_history_1, '-b', 'LineWidth', 2);
101+
hold on;
102+
plot(1:numel(J_history_2), J_history_2, '-r', 'LineWidth', 2);
103+
plot(1:numel(J_history_3), J_history_3, '-k', 'LineWidth', 2);
104+
xlabel('Number of iterations');
105+
ylabel('Cost J');
106+
107+
% Display gradient descent's result
108+
fprintf('Theta computed from gradient descent: \n');
109+
fprintf(' %f \n', theta);
110+
fprintf('\n');
111+
112+
% Estimate the price of a 1650 sq-ft, 3 br house
113+
% ====================== YOUR CODE HERE ======================
114+
% Recall that the first column of X is all-ones. Thus, it does
115+
% not need to be normalized.
116+
predictedHouseFeatures = [1650, 3];
117+
%normalise
118+
predictedHouseFeatures = (predictedHouseFeatures - mu) ./ sigma;
119+
%add ones
120+
predictedHouseFeatures = [1,predictedHouseFeatures];
121+
%predict
122+
price = predictedHouseFeatures*theta; % You should change this
123+
124+
125+
% ============================================================
126+
127+
fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
128+
'(using gradient descent):\n $%f\n'], price);
129+
130+
fprintf('Program paused. Press enter to continue.\n');
131+
pause;
132+
133+
%% ================ Part 3: Normal Equations ================
134+
135+
fprintf('Solving with normal equations...\n');
136+
137+
% ====================== YOUR CODE HERE ======================
138+
% Instructions: The following code computes the closed form
139+
% solution for linear regression using the normal
140+
% equations. You should complete the code in
141+
% normalEqn.m
142+
%
143+
% After doing so, you should complete this code
144+
% to predict the price of a 1650 sq-ft, 3 br house.
145+
%
146+
147+
%% Load Data
148+
data = csvread('ex1data2.txt');
149+
X = data(:, 1:2);
150+
y = data(:, 3);
151+
m = length(y);
152+
153+
% Add intercept term to X
154+
X = [ones(m, 1) X];
155+
156+
% Calculate the parameters from the normal equation
157+
theta = normalEqn(X, y);
158+
159+
% Display normal equation's result
160+
fprintf('Theta computed from the normal equations: \n');
161+
fprintf(' %f \n', theta);
162+
fprintf('\n');
163+
164+
165+
% Estimate the price of a 1650 sq-ft, 3 br house
166+
% ====================== YOUR CODE HERE ======================
167+
price = 0; % You should change this
168+
169+
170+
% ============================================================
171+
predictedHouseFeatures = [1650, 3];
172+
predictedHouseFeatures = [1,predictedHouseFeatures];
173+
%predict
174+
price = predictedHouseFeatures*theta; % You should change this
175+
fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
176+
'(using normal equations):\n $%f\n'], price);
177+

0 commit comments

Comments
 (0)