Skip to content

Commit dc31f2f

Browse files
Upload Exercise of Week 9
1 parent 4cba7e6 commit dc31f2f

33 files changed

+5518
-0
lines changed

machine-learning-ex8.zip

822 KB
Binary file not shown.

machine-learning-ex8/ex8.pdf

242 KB
Binary file not shown.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
function checkCostFunction(lambda)
2+
%CHECKCOSTFUNCTION Creates a collaborative filering problem
3+
%to check your cost function and gradients
4+
% CHECKCOSTFUNCTION(lambda) Creates a collaborative filering problem
5+
% to check your cost function and gradients, it will output the
6+
% analytical gradients produced by your code and the numerical gradients
7+
% (computed using computeNumericalGradient). These two gradient
8+
% computations should result in very similar values.
9+
10+
% Set lambda
11+
if ~exist('lambda', 'var') || isempty(lambda)
12+
lambda = 0;
13+
end
14+
15+
%% Create small problem
16+
X_t = rand(4, 3);
17+
Theta_t = rand(5, 3);
18+
19+
% Zap out most entries
20+
Y = X_t * Theta_t';
21+
Y(rand(size(Y)) > 0.5) = 0;
22+
R = zeros(size(Y));
23+
R(Y ~= 0) = 1;
24+
25+
%% Run Gradient Checking
26+
X = randn(size(X_t));
27+
Theta = randn(size(Theta_t));
28+
num_users = size(Y, 2);
29+
num_movies = size(Y, 1);
30+
num_features = size(Theta_t, 2);
31+
32+
numgrad = computeNumericalGradient( ...
33+
@(t) cofiCostFunc(t, Y, R, num_users, num_movies, ...
34+
num_features, lambda), [X(:); Theta(:)]);
35+
36+
[cost, grad] = cofiCostFunc([X(:); Theta(:)], Y, R, num_users, ...
37+
num_movies, num_features, lambda);
38+
39+
disp([numgrad grad]);
40+
fprintf(['The above two columns you get should be very similar.\n' ...
41+
'(Left-Your Numerical Gradient, Right-Analytical Gradient)\n\n']);
42+
43+
diff = norm(numgrad-grad)/norm(numgrad+grad);
44+
fprintf(['If your cost function implementation is correct, then \n' ...
45+
'the relative difference will be small (less than 1e-9). \n' ...
46+
'\nRelative Difference: %g\n'], diff);
47+
48+
end
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function [J, grad] = cofiCostFunc(params, Y, R, num_users, num_movies, ...
2+
num_features, lambda)
3+
%COFICOSTFUNC Collaborative filtering cost function
4+
% [J, grad] = COFICOSTFUNC(params, Y, R, num_users, num_movies, ...
5+
% num_features, lambda) returns the cost and gradient for the
6+
% collaborative filtering problem.
7+
%
8+
9+
% Unfold the U and W matrices from params
10+
X = reshape(params(1:num_movies*num_features), num_movies, num_features);
11+
Theta = reshape(params(num_movies*num_features+1:end), ...
12+
num_users, num_features);
13+
14+
15+
% You need to return the following values correctly
16+
J = 0;
17+
X_grad = zeros(size(X));
18+
Theta_grad = zeros(size(Theta));
19+
20+
% ====================== YOUR CODE HERE ======================
21+
% Instructions: Compute the cost function and gradient for collaborative
22+
% filtering. Concretely, you should first implement the cost
23+
% function (without regularization) and make sure it is
24+
% matches our costs. After that, you should implement the
25+
% gradient and use the checkCostFunction routine to check
26+
% that the gradient is correct. Finally, you should implement
27+
% regularization.
28+
%
29+
% Notes: X - num_movies x num_features matrix of movie features
30+
% Theta - num_users x num_features matrix of user features
31+
% Y - num_movies x num_users matrix of user ratings of movies
32+
% R - num_movies x num_users matrix, where R(i, j) = 1 if the
33+
% i-th movie was rated by the j-th user
34+
%
35+
% You should set the following variables correctly:
36+
%
37+
% X_grad - num_movies x num_features matrix, containing the
38+
% partial derivatives w.r.t. to each element of X
39+
% Theta_grad - num_users x num_features matrix, containing the
40+
% partial derivatives w.r.t. to each element of Theta
41+
%
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
% =============================================================
59+
60+
grad = [X_grad(:); Theta_grad(:)];
61+
62+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function numgrad = computeNumericalGradient(J, theta)
2+
%COMPUTENUMERICALGRADIENT Computes the gradient using "finite differences"
3+
%and gives us a numerical estimate of the gradient.
4+
% numgrad = COMPUTENUMERICALGRADIENT(J, theta) computes the numerical
5+
% gradient of the function J around theta. Calling y = J(theta) should
6+
% return the function value at theta.
7+
8+
% Notes: The following code implements numerical gradient checking, and
9+
% returns the numerical gradient.It sets numgrad(i) to (a numerical
10+
% approximation of) the partial derivative of J with respect to the
11+
% i-th input argument, evaluated at theta. (i.e., numgrad(i) should
12+
% be the (approximately) the partial derivative of J with respect
13+
% to theta(i).)
14+
%
15+
16+
numgrad = zeros(size(theta));
17+
perturb = zeros(size(theta));
18+
e = 1e-4;
19+
for p = 1:numel(theta)
20+
% Set perturbation vector
21+
perturb(p) = e;
22+
loss1 = J(theta - perturb);
23+
loss2 = J(theta + perturb);
24+
% Compute Numerical Gradient
25+
numgrad(p) = (loss2 - loss1) / (2*e);
26+
perturb(p) = 0;
27+
end
28+
29+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function [mu sigma2] = estimateGaussian(X)
2+
%ESTIMATEGAUSSIAN This function estimates the parameters of a
3+
%Gaussian distribution using the data in X
4+
% [mu sigma2] = estimateGaussian(X),
5+
% The input X is the dataset with each n-dimensional data point in one row
6+
% The output is an n-dimensional vector mu, the mean of the data set
7+
% and the variances sigma^2, an n x 1 vector
8+
%
9+
10+
% Useful variables
11+
[m, n] = size(X);
12+
13+
% You should return these values correctly
14+
mu = zeros(n, 1);
15+
sigma2 = zeros(n, 1);
16+
17+
% ====================== YOUR CODE HERE ======================
18+
% Instructions: Compute the mean of the data and the variances
19+
% In particular, mu(i) should contain the mean of
20+
% the data for the i-th feature and sigma2(i)
21+
% should contain variance of the i-th feature.
22+
%
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
% =============================================================
34+
35+
36+
end

machine-learning-ex8/ex8/ex8.m

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
%% Machine Learning Online Class
2+
% Exercise 8 | Anomaly Detection and Collaborative Filtering
3+
%
4+
% Instructions
5+
% ------------
6+
%
7+
% This file contains code that helps you get started on the
8+
% exercise. You will need to complete the following functions:
9+
%
10+
% estimateGaussian.m
11+
% selectThreshold.m
12+
% cofiCostFunc.m
13+
%
14+
% For this exercise, you will not need to change any code in this file,
15+
% or any other files other than those mentioned above.
16+
%
17+
18+
%% Initialization
19+
clear ; close all; clc
20+
21+
%% ================== Part 1: Load Example Dataset ===================
22+
% We start this exercise by using a small dataset that is easy to
23+
% visualize.
24+
%
25+
% Our example case consists of 2 network server statistics across
26+
% several machines: the latency and throughput of each machine.
27+
% This exercise will help us find possibly faulty (or very fast) machines.
28+
%
29+
30+
fprintf('Visualizing example dataset for outlier detection.\n\n');
31+
32+
% The following command loads the dataset. You should now have the
33+
% variables X, Xval, yval in your environment
34+
load('ex8data1.mat');
35+
36+
% Visualize the example dataset
37+
plot(X(:, 1), X(:, 2), 'bx');
38+
axis([0 30 0 30]);
39+
xlabel('Latency (ms)');
40+
ylabel('Throughput (mb/s)');
41+
42+
fprintf('Program paused. Press enter to continue.\n');
43+
pause
44+
45+
46+
%% ================== Part 2: Estimate the dataset statistics ===================
47+
% For this exercise, we assume a Gaussian distribution for the dataset.
48+
%
49+
% We first estimate the parameters of our assumed Gaussian distribution,
50+
% then compute the probabilities for each of the points and then visualize
51+
% both the overall distribution and where each of the points falls in
52+
% terms of that distribution.
53+
%
54+
fprintf('Visualizing Gaussian fit.\n\n');
55+
56+
% Estimate my and sigma2
57+
[mu sigma2] = estimateGaussian(X);
58+
59+
% Returns the density of the multivariate normal at each data point (row)
60+
% of X
61+
p = multivariateGaussian(X, mu, sigma2);
62+
63+
% Visualize the fit
64+
visualizeFit(X, mu, sigma2);
65+
xlabel('Latency (ms)');
66+
ylabel('Throughput (mb/s)');
67+
68+
fprintf('Program paused. Press enter to continue.\n');
69+
pause;
70+
71+
%% ================== Part 3: Find Outliers ===================
72+
% Now you will find a good epsilon threshold using a cross-validation set
73+
% probabilities given the estimated Gaussian distribution
74+
%
75+
76+
pval = multivariateGaussian(Xval, mu, sigma2);
77+
78+
[epsilon F1] = selectThreshold(yval, pval);
79+
fprintf('Best epsilon found using cross-validation: %e\n', epsilon);
80+
fprintf('Best F1 on Cross Validation Set: %f\n', F1);
81+
fprintf(' (you should see a value epsilon of about 8.99e-05)\n');
82+
fprintf(' (you should see a Best F1 value of 0.875000)\n\n');
83+
84+
% Find the outliers in the training set and plot the
85+
outliers = find(p < epsilon);
86+
87+
% Draw a red circle around those outliers
88+
hold on
89+
plot(X(outliers, 1), X(outliers, 2), 'ro', 'LineWidth', 2, 'MarkerSize', 10);
90+
hold off
91+
92+
fprintf('Program paused. Press enter to continue.\n');
93+
pause;
94+
95+
%% ================== Part 4: Multidimensional Outliers ===================
96+
% We will now use the code from the previous part and apply it to a
97+
% harder problem in which more features describe each datapoint and only
98+
% some features indicate whether a point is an outlier.
99+
%
100+
101+
% Loads the second dataset. You should now have the
102+
% variables X, Xval, yval in your environment
103+
load('ex8data2.mat');
104+
105+
% Apply the same steps to the larger dataset
106+
[mu sigma2] = estimateGaussian(X);
107+
108+
% Training set
109+
p = multivariateGaussian(X, mu, sigma2);
110+
111+
% Cross-validation set
112+
pval = multivariateGaussian(Xval, mu, sigma2);
113+
114+
% Find the best threshold
115+
[epsilon F1] = selectThreshold(yval, pval);
116+
117+
fprintf('Best epsilon found using cross-validation: %e\n', epsilon);
118+
fprintf('Best F1 on Cross Validation Set: %f\n', F1);
119+
fprintf(' (you should see a value epsilon of about 1.38e-18)\n');
120+
fprintf(' (you should see a Best F1 value of 0.615385)\n');
121+
fprintf('# Outliers found: %d\n\n', sum(p < epsilon));

0 commit comments

Comments
 (0)