Skip to content

Commit ffeaa22

Browse files
authored
Add files via upload
1 parent 38a4144 commit ffeaa22

27 files changed

+3701
-0
lines changed

machine-learning-ex2/ex2.pdf

228 KB
Binary file not shown.
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function [J, grad] = costFunction(theta, X, y)
2+
%COSTFUNCTION Compute cost and gradient for logistic regression
3+
% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
4+
% parameter for logistic regression and the gradient of the cost
5+
% w.r.t. to the parameters.
6+
7+
% Initialize some useful values
8+
m = length(y); % number of training examples
9+
10+
% You need to return the following variables correctly
11+
J = 0;
12+
grad = zeros(size(theta));
13+
14+
% ====================== YOUR CODE HERE ======================
15+
% Instructions: Compute the cost of a particular choice of theta.
16+
% You should set J to the cost.
17+
% Compute the partial derivatives and set grad to the partial
18+
% derivatives of the cost w.r.t. each parameter in theta
19+
%
20+
% Note: grad should have the same dimensions as theta
21+
%
22+
23+
J= -1 * sum( y .* log( sigmoid(X*theta) ) + (1 - y ) .* log( (1 - sigmoid(X*theta)) ) ) / m ;
24+
25+
grad = ( X' * (sigmoid(X*theta) - y ) )/ m ;
26+
27+
28+
29+
30+
31+
32+
% =============================================================
33+
34+
end
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function [J, grad] = costFunctionReg(theta, X, y, lambda)
2+
%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization
3+
% J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using
4+
% theta as the parameter for regularized logistic regression and the
5+
% gradient of the cost w.r.t. to the parameters.
6+
7+
% Initialize some useful values
8+
m = length(y); % number of training examples
9+
10+
% You need to return the following variables correctly
11+
J = 0;
12+
grad = zeros(size(theta));
13+
14+
% ====================== YOUR CODE HERE ======================
15+
% Instructions: Compute the cost of a particular choice of theta.
16+
% You should set J to the cost.
17+
% Compute the partial derivatives and set grad to the partial
18+
% derivatives of the cost w.r.t. each parameter in theta
19+
20+
theta_1=[0;theta(2:end)]; % 先把theta(1)拿掉,不参与正则化
21+
J= -1 * sum( y .* log( sigmoid(X*theta) ) + (1 - y ) .* log( (1 - sigmoid(X*theta)) ) ) / m + lambda/(2*m) * theta_1' * theta_1 ;
22+
grad = ( X' * (sigmoid(X*theta) - y ) )/ m + lambda/m * theta_1 ;
23+
24+
25+
26+
27+
28+
% =============================================================
29+
30+
end

machine-learning-ex2/ex2/ex2.m

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
%% Machine Learning Online Class - Exercise 2: Logistic Regression
2+
%
3+
% Instructions
4+
% ------------
5+
%
6+
% This file contains code that helps you get started on the logistic
7+
% regression exercise. You will need to complete the following functions
8+
% in this exericse:
9+
%
10+
% sigmoid.m
11+
% costFunction.m
12+
% predict.m
13+
% costFunctionReg.m
14+
%
15+
% For this exercise, you will not need to change any code in this file,
16+
% or any other files other than those mentioned above.
17+
%
18+
19+
%% Initialization
20+
clear ; close all; clc
21+
22+
%% Load Data
23+
% The first two columns contains the exam scores and the third column
24+
% contains the label.
25+
26+
data = load('ex2data1.txt');
27+
X = data(:, [1, 2]); y = data(:, 3);
28+
29+
%% ==================== Part 1: Plotting ====================
30+
% We start the exercise by first plotting the data to understand the
31+
% the problem we are working with.
32+
33+
fprintf(['Plotting data with + indicating (y = 1) examples and o ' ...
34+
'indicating (y = 0) examples.\n']);
35+
36+
plotData(X, y);
37+
38+
% Put some labels
39+
hold on;
40+
% Labels and Legend
41+
xlabel('Exam 1 score')
42+
ylabel('Exam 2 score')
43+
44+
% Specified in plot order
45+
legend('Admitted', 'Not admitted')
46+
hold off;
47+
48+
fprintf('\nProgram paused. Press enter to continue.\n');
49+
pause;
50+
51+
52+
%% ============ Part 2: Compute Cost and Gradient ============
53+
% In this part of the exercise, you will implement the cost and gradient
54+
% for logistic regression. You neeed to complete the code in
55+
% costFunction.m
56+
57+
% Setup the data matrix appropriately, and add ones for the intercept term
58+
[m, n] = size(X);
59+
60+
% Add intercept term to x and X_test
61+
X = [ones(m, 1) X];
62+
63+
% Initialize fitting parameters
64+
initial_theta = zeros(n + 1, 1);
65+
66+
% Compute and display initial cost and gradient
67+
[cost, grad] = costFunction(initial_theta, X, y);
68+
69+
fprintf('Cost at initial theta (zeros): %f\n', cost);
70+
fprintf('Expected cost (approx): 0.693\n');
71+
fprintf('Gradient at initial theta (zeros): \n');
72+
fprintf(' %f \n', grad);
73+
fprintf('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n');
74+
75+
% Compute and display cost and gradient with non-zero theta
76+
test_theta = [-24; 0.2; 0.2];
77+
[cost, grad] = costFunction(test_theta, X, y);
78+
79+
fprintf('\nCost at test theta: %f\n', cost);
80+
fprintf('Expected cost (approx): 0.218\n');
81+
fprintf('Gradient at test theta: \n');
82+
fprintf(' %f \n', grad);
83+
fprintf('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n');
84+
85+
fprintf('\nProgram paused. Press enter to continue.\n');
86+
pause;
87+
88+
89+
%% ============= Part 3: Optimizing using fminunc =============
90+
% In this exercise, you will use a built-in function (fminunc) to find the
91+
% optimal parameters theta.
92+
93+
% Set options for fminunc
94+
options = optimset('GradObj', 'on', 'MaxIter', 400);
95+
96+
% Run fminunc to obtain the optimal theta
97+
% This function will return theta and the cost
98+
[theta, cost] = ...
99+
fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
100+
101+
% Print theta to screen
102+
fprintf('Cost at theta found by fminunc: %f\n', cost);
103+
fprintf('Expected cost (approx): 0.203\n');
104+
fprintf('theta: \n');
105+
fprintf(' %f \n', theta);
106+
fprintf('Expected theta (approx):\n');
107+
fprintf(' -25.161\n 0.206\n 0.201\n');
108+
109+
% Plot Boundary
110+
plotDecisionBoundary(theta, X, y);
111+
112+
% Put some labels
113+
hold on;
114+
% Labels and Legend
115+
xlabel('Exam 1 score')
116+
ylabel('Exam 2 score')
117+
118+
% Specified in plot order
119+
legend('Admitted', 'Not admitted')
120+
hold off;
121+
122+
fprintf('\nProgram paused. Press enter to continue.\n');
123+
pause;
124+
125+
%% ============== Part 4: Predict and Accuracies ==============
126+
% After learning the parameters, you'll like to use it to predict the outcomes
127+
% on unseen data. In this part, you will use the logistic regression model
128+
% to predict the probability that a student with score 45 on exam 1 and
129+
% score 85 on exam 2 will be admitted.
130+
%
131+
% Furthermore, you will compute the training and test set accuracies of
132+
% our model.
133+
%
134+
% Your task is to complete the code in predict.m
135+
136+
% Predict probability for a student with score 45 on exam 1
137+
% and score 85 on exam 2
138+
139+
prob = sigmoid([1 45 85] * theta);
140+
fprintf(['For a student with scores 45 and 85, we predict an admission ' ...
141+
'probability of %f\n'], prob);
142+
fprintf('Expected value: 0.775 +/- 0.002\n\n');
143+
144+
% Compute accuracy on our training set
145+
p = predict(theta, X);
146+
147+
fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);
148+
fprintf('Expected accuracy (approx): 89.0\n');
149+
fprintf('\n');
150+
151+

machine-learning-ex2/ex2/ex2_reg.m

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
%% Machine Learning Online Class - Exercise 2: Logistic Regression
2+
%
3+
% Instructions
4+
% ------------
5+
%
6+
% This file contains code that helps you get started on the second part
7+
% of the exercise which covers regularization with logistic regression.
8+
%
9+
% You will need to complete the following functions in this exericse:
10+
%
11+
% sigmoid.m
12+
% costFunction.m
13+
% predict.m
14+
% costFunctionReg.m
15+
%
16+
% For this exercise, you will not need to change any code in this file,
17+
% or any other files other than those mentioned above.
18+
%
19+
20+
%% Initialization
21+
clear ; close all; clc
22+
23+
%% Load Data
24+
% The first two columns contains the X values and the third column
25+
% contains the label (y).
26+
27+
data = load('ex2data2.txt');
28+
X = data(:, [1, 2]); y = data(:, 3);
29+
30+
plotData(X, y);
31+
32+
% Put some labels
33+
hold on;
34+
35+
% Labels and Legend
36+
xlabel('Microchip Test 1')
37+
ylabel('Microchip Test 2')
38+
39+
% Specified in plot order
40+
legend('y = 1', 'y = 0')
41+
hold off;
42+
43+
44+
%% =========== Part 1: Regularized Logistic Regression ============
45+
% In this part, you are given a dataset with data points that are not
46+
% linearly separable. However, you would still like to use logistic
47+
% regression to classify the data points.
48+
%
49+
% To do so, you introduce more features to use -- in particular, you add
50+
% polynomial features to our data matrix (similar to polynomial
51+
% regression).
52+
%
53+
54+
% Add Polynomial Features
55+
56+
% Note that mapFeature also adds a column of ones for us, so the intercept
57+
% term is handled
58+
X = mapFeature(X(:,1), X(:,2));
59+
60+
% Initialize fitting parameters
61+
initial_theta = zeros(size(X, 2), 1);
62+
63+
% Set regularization parameter lambda to 1
64+
lambda = 1;
65+
66+
% Compute and display initial cost and gradient for regularized logistic
67+
% regression
68+
[cost, grad] = costFunctionReg(initial_theta, X, y, lambda);
69+
70+
fprintf('Cost at initial theta (zeros): %f\n', cost);
71+
fprintf('Expected cost (approx): 0.693\n');
72+
fprintf('Gradient at initial theta (zeros) - first five values only:\n');
73+
fprintf(' %f \n', grad(1:5));
74+
fprintf('Expected gradients (approx) - first five values only:\n');
75+
fprintf(' 0.0085\n 0.0188\n 0.0001\n 0.0503\n 0.0115\n');
76+
77+
fprintf('\nProgram paused. Press enter to continue.\n');
78+
pause;
79+
80+
% Compute and display cost and gradient
81+
% with all-ones theta and lambda = 10
82+
test_theta = ones(size(X,2),1);
83+
[cost, grad] = costFunctionReg(test_theta, X, y, 10);
84+
85+
fprintf('\nCost at test theta (with lambda = 10): %f\n', cost);
86+
fprintf('Expected cost (approx): 3.16\n');
87+
fprintf('Gradient at test theta - first five values only:\n');
88+
fprintf(' %f \n', grad(1:5));
89+
fprintf('Expected gradients (approx) - first five values only:\n');
90+
fprintf(' 0.3460\n 0.1614\n 0.1948\n 0.2269\n 0.0922\n');
91+
92+
fprintf('\nProgram paused. Press enter to continue.\n');
93+
pause;
94+
95+
%% ============= Part 2: Regularization and Accuracies =============
96+
% Optional Exercise:
97+
% In this part, you will get to try different values of lambda and
98+
% see how regularization affects the decision coundart
99+
%
100+
% Try the following values of lambda (0, 1, 10, 100).
101+
%
102+
% How does the decision boundary change when you vary lambda? How does
103+
% the training set accuracy vary?
104+
%
105+
106+
% Initialize fitting parameters
107+
initial_theta = zeros(size(X, 2), 1);
108+
109+
% Set regularization parameter lambda to 1 (you should vary this)
110+
lambda = 1;
111+
112+
% Set Options
113+
options = optimset('GradObj', 'on', 'MaxIter', 400);
114+
115+
% Optimize
116+
[theta, J, exit_flag] = ...
117+
fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options);
118+
119+
% Plot Boundary
120+
plotDecisionBoundary(theta, X, y);
121+
hold on;
122+
title(sprintf('lambda = %g', lambda))
123+
124+
% Labels and Legend
125+
xlabel('Microchip Test 1')
126+
ylabel('Microchip Test 2')
127+
128+
legend('y = 1', 'y = 0', 'Decision boundary')
129+
hold off;
130+
131+
% Compute accuracy on our training set
132+
p = predict(theta, X);
133+
134+
fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);
135+
fprintf('Expected accuracy (with lambda = 1): 83.1 (approx)\n');
136+

0 commit comments

Comments
 (0)