From 3ced8e324b5e8287597cb7ae6565ba4ee89e8fe5 Mon Sep 17 00:00:00 2001 From: ANUJ GUPTA <48452341+rookie-anoozzz@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:28:26 +0530 Subject: [PATCH 1/3] Create week_4 --- Programming Assignment/week_4 | 1 + 1 file changed, 1 insertion(+) create mode 100644 Programming Assignment/week_4 diff --git a/Programming Assignment/week_4 b/Programming Assignment/week_4 new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Programming Assignment/week_4 @@ -0,0 +1 @@ + From 5d29dfd3ee9cb1fdb150bb358319b4ca66c7ffd4 Mon Sep 17 00:00:00 2001 From: ANUJ GUPTA <48452341+rookie-anoozzz@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:28:43 +0530 Subject: [PATCH 2/3] Delete week_4 --- Programming Assignment/week_4 | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Programming Assignment/week_4 diff --git a/Programming Assignment/week_4 b/Programming Assignment/week_4 deleted file mode 100644 index 8b13789..0000000 --- a/Programming Assignment/week_4 +++ /dev/null @@ -1 +0,0 @@ - From bd03728a1154818fe7dd3bce2ad1e8bf92dbfd5f Mon Sep 17 00:00:00 2001 From: ANUJ GUPTA <48452341+rookie-anoozzz@users.noreply.github.com> Date: Mon, 1 Jul 2019 00:29:23 +0530 Subject: [PATCH 3/3] Add files via upload --- .../week_4/anuj_week4/computeCentroids.m | 46 +++++++++++++++++++ .../week_4/anuj_week4/findClosestCentroids.m | 39 ++++++++++++++++ .../week_4/anuj_week4/pca.m | 32 +++++++++++++ .../week_4/anuj_week4/projectData.m | 26 +++++++++++ .../week_4/anuj_week4/recoverData.m | 28 +++++++++++ 5 files changed, 171 insertions(+) create mode 100644 Programming Assignment/week_4/anuj_week4/computeCentroids.m create mode 100644 Programming Assignment/week_4/anuj_week4/findClosestCentroids.m create mode 100644 Programming Assignment/week_4/anuj_week4/pca.m create mode 100644 Programming Assignment/week_4/anuj_week4/projectData.m create mode 100644 Programming Assignment/week_4/anuj_week4/recoverData.m diff --git a/Programming Assignment/week_4/anuj_week4/computeCentroids.m b/Programming Assignment/week_4/anuj_week4/computeCentroids.m new file mode 100644 index 0000000..2a4ad5d --- /dev/null +++ b/Programming Assignment/week_4/anuj_week4/computeCentroids.m @@ -0,0 +1,46 @@ +function centroids = computeCentroids(X, idx, K) +%COMPUTECENTROIDS returns the new centroids by computing the means of the +%data points assigned to each centroid. +% centroids = COMPUTECENTROIDS(X, idx, K) returns the new centroids by +% computing the means of the data points assigned to each centroid. It is +% given a dataset X where each row is a single data point, a vector +% idx of centroid assignments (i.e. each entry in range [1..K]) for each +% example, and K, the number of centroids. You should return a matrix +% centroids, where each row of centroids is the mean of the data points +% assigned to it. +% + +% Useful variables +[m n] = size(X); + +% You need to return the following variables correctly. +centroids = zeros(K, n); + + +% ====================== YOUR CODE HERE ====================== +% Instructions: Go over every centroid and compute mean of all points that +% belong to it. Concretely, the row vector centroids(i, :) +% should contain the mean of the data points assigned to +% centroid i. +% +% Note: You can use a for-loop over the centroids to compute this. +% +ind = zeros(K, 1); +for i = 1:m + centroids(idx(i), :) = centroids(idx(i), :) + X(i, :); + ind(idx(i)) = ind(idx(i)) + 1; +endfor + +centroids = centroids ./ ind; + + + + + + + +% ============================================================= + + +end + diff --git a/Programming Assignment/week_4/anuj_week4/findClosestCentroids.m b/Programming Assignment/week_4/anuj_week4/findClosestCentroids.m new file mode 100644 index 0000000..0412269 --- /dev/null +++ b/Programming Assignment/week_4/anuj_week4/findClosestCentroids.m @@ -0,0 +1,39 @@ +function idx = findClosestCentroids(X, centroids) +%FINDCLOSESTCENTROIDS computes the centroid memberships for every example +% idx = FINDCLOSESTCENTROIDS (X, centroids) returns the closest centroids +% in idx for a dataset X where each row is a single example. idx = m x 1 +% vector of centroid assignments (i.e. each entry in range [1..K]) +% + +% Set K +K = size(centroids, 1); + +% You need to return the following variables correctly. +idx = zeros(size(X,1), 1); + +% ====================== YOUR CODE HERE ====================== +% Instructions: Go over every example, find its closest centroid, and store +% the index inside idx at the appropriate location. +% Concretely, idx(i) should contain the index of the centroid +% closest to example i. Hence, it should be a value in the +% range 1..K +% +% Note: You can use a for-loop over the examples to compute this. +% +temp = zeros(length(idx), K); +for i = 1:length(idx) + for j = 1:K + temp(i,j) = (X(i, :) - centroids(j, :))*(X(i, :) - centroids(j, :))'; + endfor + [x, idx(i)] = min(temp(i, :)); + +endfor + + + + + +% ============================================================= + +end + diff --git a/Programming Assignment/week_4/anuj_week4/pca.m b/Programming Assignment/week_4/anuj_week4/pca.m new file mode 100644 index 0000000..4abaa19 --- /dev/null +++ b/Programming Assignment/week_4/anuj_week4/pca.m @@ -0,0 +1,32 @@ +function [U, S] = pca(X) +%PCA Run principal component analysis on the dataset X +% [U, S, X] = pca(X) computes eigenvectors of the covariance matrix of X +% Returns the eigenvectors U, the eigenvalues (on diagonal) in S +% + +% Useful values +[m, n] = size(X); + +% You need to return the following variables correctly. +U = zeros(n); +S = zeros(n); + +% ====================== YOUR CODE HERE ====================== +% Instructions: You should first compute the covariance matrix. Then, you +% should use the "svd" function to compute the eigenvectors +% and eigenvalues of the covariance matrix. +% +% Note: When computing the covariance matrix, remember to divide by m (the +% number of examples). +% +covariance = (1/m)*(X'*X); +[U, S, V] = svd(covariance); + + + + + + +% ========================================================================= + +end diff --git a/Programming Assignment/week_4/anuj_week4/projectData.m b/Programming Assignment/week_4/anuj_week4/projectData.m new file mode 100644 index 0000000..3a1153d --- /dev/null +++ b/Programming Assignment/week_4/anuj_week4/projectData.m @@ -0,0 +1,26 @@ +function Z = projectData(X, U, K) +%PROJECTDATA Computes the reduced data representation when projecting only +%on to the top k eigenvectors +% Z = projectData(X, U, K) computes the projection of +% the normalized inputs X into the reduced dimensional space spanned by +% the first K columns of U. It returns the projected examples in Z. +% + +% You need to return the following variables correctly. +Z = zeros(size(X, 1), K); + +% ====================== YOUR CODE HERE ====================== +% Instructions: Compute the projection of the data using only the top K +% eigenvectors in U (first K columns). +% For the i-th example X(i,:), the projection on to the k-th +% eigenvector is given as follows: +% x = X(i, :)'; +% projection_k = x' * U(:, k); +% +Z = X*U(:,1:K); + + + +% ============================================================= + +end diff --git a/Programming Assignment/week_4/anuj_week4/recoverData.m b/Programming Assignment/week_4/anuj_week4/recoverData.m new file mode 100644 index 0000000..6de6c7d --- /dev/null +++ b/Programming Assignment/week_4/anuj_week4/recoverData.m @@ -0,0 +1,28 @@ +function X_rec = recoverData(Z, U, K) +%RECOVERDATA Recovers an approximation of the original data when using the +%projected data +% X_rec = RECOVERDATA(Z, U, K) recovers an approximation the +% original data that has been reduced to K dimensions. It returns the +% approximate reconstruction in X_rec. +% + +% You need to return the following variables correctly. +X_rec = zeros(size(Z, 1), size(U, 1)); + +% ====================== YOUR CODE HERE ====================== +% Instructions: Compute the approximation of the data by projecting back +% onto the original space using the top K eigenvectors in U. +% +% For the i-th example Z(i,:), the (approximate) +% recovered data for dimension j is given as follows: +% v = Z(i, :)'; +% recovered_j = v' * U(j, 1:K)'; +% +% Notice that U(j, 1:K) is a row vector. +% +X_rec = Z*U(:,1:K)'; + + +% ============================================================= + +end