Skip to content

Commit ac701d5

Browse files
committed
add matlab version
1 parent 1defb2b commit ac701d5

File tree

125 files changed

+9989
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+9989
-4
lines changed

README.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# 机器学习对应Jupyter Notebook
1+
# 机器学习Jupyter Notebook
22

33
吴恩达的Machine Learning课程是非常适合机器学习的初学者。
44

55
但其assignment使用了Matlab/Octave语言,这显然已经不适合当前机器学习/深度学习技术的发展。
66

77
此Repository将其所有课程assignment内容更新为Python版本,并采用Jupyter Notebook的形式,方便阅读和实验。
88

9-
以下为各个assignment的内容:
9+
## Jupyter Notebook (Python)
1010

1111
* [1.linear_regression](1.linear_regression)
1212
* [2.logistic_regression](2.logistic_regression)
@@ -16,6 +16,13 @@
1616
* [6.svm](6.svm)
1717
* [7.kmeans_and_PCA](7.kmeans_and_PCA)
1818
* [8.anomaly_detection_and_recommendation](8.anomaly_detection_and_recommendation)
19-
* 此外,原始Assignment的文字描述整合在[一个PDF](Machine_Learning_Assignment(ex1-ex8).pdf)
2019

21-
如有疑问,请Submit issue或Pull Request,谢谢
20+
## 原始MATLAB版本
21+
* [原始Assignment的MATLAB版本](original-machine-learning-MATLAB)
22+
23+
## 练习的描述PDF
24+
* [原始Assignment的文字描述](Machine_Learning_Assignment(ex1-ex8).pdf)(整合为一个PDF)
25+
26+
27+
如有疑问,请Submit issue或Pull Request,谢谢
28+
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+
predictions = X * theta;
17+
J = (1 / (2 * m)) * sum((predictions - y).^2);
18+
19+
20+
% =========================================================================
21+
22+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
17+
predictions = X * theta;
18+
J = (1 / (2 * m)) * sum((predictions - y).^2);
19+
20+
21+
% =========================================================================
22+
23+
end
+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
%% Initialization
27+
clear ; close all; clc
28+
29+
%% ==================== Part 1: Basic Function ====================
30+
% Complete warmUpExercise.m
31+
fprintf('Running warmUpExercise ... \n');
32+
fprintf('5x5 Identity Matrix: \n');
33+
warmUpExercise()
34+
35+
fprintf('Program paused. Press enter to continue.\n');
36+
pause;
37+
38+
39+
%% ======================= Part 2: Plotting =======================
40+
fprintf('Plotting Data ...\n')
41+
data = load('ex1data1.txt');
42+
X = data(:, 1); y = data(:, 2);
43+
m = length(y); % number of training examples
44+
45+
% Plot Data
46+
% Note: You have to complete the code in plotData.m
47+
plotData(X, y);
48+
49+
fprintf('Program paused. Press enter to continue.\n');
50+
pause;
51+
52+
%% =================== Part 3: Cost and Gradient descent ===================
53+
54+
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
55+
theta = zeros(2, 1); % initialize fitting parameters
56+
57+
% Some gradient descent settings
58+
iterations = 1500;
59+
alpha = 0.01;
60+
61+
fprintf('\nTesting the cost function ...\n')
62+
% compute and display initial cost
63+
J = computeCost(X, y, theta);
64+
fprintf('With theta = [0 ; 0]\nCost computed = %f\n', J);
65+
fprintf('Expected cost value (approx) 32.07\n');
66+
67+
% further testing of the cost function
68+
J = computeCost(X, y, [-1 ; 2]);
69+
fprintf('\nWith theta = [-1 ; 2]\nCost computed = %f\n', J);
70+
fprintf('Expected cost value (approx) 54.24\n');
71+
72+
fprintf('Program paused. Press enter to continue.\n');
73+
pause;
74+
75+
fprintf('\nRunning Gradient Descent ...\n')
76+
% run gradient descent
77+
theta = gradientDescent(X, y, theta, alpha, iterations);
78+
79+
% print theta to screen
80+
fprintf('Theta found by gradient descent:\n');
81+
fprintf('%f\n', theta);
82+
fprintf('Expected theta values (approx)\n');
83+
fprintf(' -3.6303\n 1.1664\n\n');
84+
85+
% Plot the linear fit
86+
hold on; % keep previous plot visible
87+
plot(X(:,2), X*theta, '-')
88+
legend('Training data', 'Linear regression')
89+
hold off % don't overlay any more plots on this figure
90+
91+
% Predict values for population sizes of 35,000 and 70,000
92+
predict1 = [1, 3.5] *theta;
93+
fprintf('For population = 35,000, we predict a profit of %f\n',...
94+
predict1*10000);
95+
predict2 = [1, 7] * theta;
96+
fprintf('For population = 70,000, we predict a profit of %f\n',...
97+
predict2*10000);
98+
99+
fprintf('Program paused. Press enter to continue.\n');
100+
pause;
101+
102+
%% ============= Part 4: Visualizing J(theta_0, theta_1) =============
103+
fprintf('Visualizing J(theta_0, theta_1) ...\n')
104+
105+
% Grid over which we will calculate J
106+
theta0_vals = linspace(-10, 10, 100);
107+
theta1_vals = linspace(-1, 4, 100);
108+
109+
% initialize J_vals to a matrix of 0's
110+
J_vals = zeros(length(theta0_vals), length(theta1_vals));
111+
112+
% Fill out J_vals
113+
for i = 1:length(theta0_vals)
114+
for j = 1:length(theta1_vals)
115+
t = [theta0_vals(i); theta1_vals(j)];
116+
J_vals(i,j) = computeCost(X, y, t);
117+
end
118+
end
119+
120+
121+
% Because of the way meshgrids work in the surf command, we need to
122+
% transpose J_vals before calling surf, or else the axes will be flipped
123+
J_vals = J_vals';
124+
% Surface plot
125+
figure;
126+
surf(theta0_vals, theta1_vals, J_vals)
127+
xlabel('\theta_0'); ylabel('\theta_1');
128+
129+
% Contour plot
130+
figure;
131+
% Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100
132+
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20))
133+
xlabel('\theta_0'); ylabel('\theta_1');
134+
hold on;
135+
plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);
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+
% UfCo2GyyeCNRlEnZ
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+
%% ================ Part 2: Gradient Descent ================
58+
59+
% ====================== YOUR CODE HERE ======================
60+
% Instructions: We have provided you with the following starter
61+
% code that runs gradient descent with a particular
62+
% learning rate (alpha).
63+
%
64+
% Your task is to first make sure that your functions -
65+
% computeCost and gradientDescent already work with
66+
% this starter code and support multiple variables.
67+
%
68+
% After that, try running gradient descent with
69+
% different values of alpha and see which one gives
70+
% you the best result.
71+
%
72+
% Finally, you should complete the code at the end
73+
% to predict the price of a 1650 sq-ft, 3 br house.
74+
%
75+
% Hint: By using the 'hold on' command, you can plot multiple
76+
% graphs on the same figure.
77+
%
78+
% Hint: At prediction, make sure you do the same feature normalization.
79+
%
80+
81+
fprintf('Running gradient descent ...\n');
82+
83+
% Choose some alpha value
84+
alpha = 0.1;
85+
num_iters = 100;
86+
87+
% Init Theta and Run Gradient Descent
88+
theta = zeros(3, 1);
89+
[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);
90+
91+
% Plot the convergence graph
92+
figure;
93+
plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2);
94+
xlabel('Number of iterations');
95+
ylabel('Cost J');
96+
97+
% Display gradient descent's result
98+
fprintf('Theta computed from gradient descent: \n');
99+
fprintf(' %f \n', theta);
100+
fprintf('\n');
101+
102+
% Estimate the price of a 1650 sq-ft, 3 br house
103+
% ====================== YOUR CODE HERE ======================
104+
% Recall that the first column of X is all-ones. Thus, it does
105+
% not need to be normalized.
106+
X = [1650 3];
107+
X = (X - mu) ./ sigma;
108+
price = [1 X] * theta; % You should change this
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 = [1 1650 3] * theta;; % You should change this
154+
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)