Coursera Machine Learning 第八周 quiz Programming Exercise 7 K-means Clustering and Principal Component
2016-11-13 00:13
567 查看
findClosestCentroids.m
computeCentroids.m
projectData.m
recoverData.m
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. % for i=1:size(X,1) adj=sqrt((X(i,:)-centroids(1,:))*(X(i,:)-centroids(1,:))'); idx(i)=1; for j=2:K temp=sqrt((X(i,:)-centroids(j,:))*(X(i,:)-centroids(j,:))'); if(temp<adj) idx(i)=j; adj=temp; end end end % ============================================================= end
computeCentroids.m
function centroids = computeCentroids(X, idx, K) %COMPUTECENTROIDS returs 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 fo 4000 r-loop over the centroids to compute this. % for i=1:K if(size(find(idx==i),2)~=0) centroids(i,:)=mean(X(find(idx==i),:)); else centroids(i,:)=zeros(1,n); end end % ============================================================= end pca.m
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). % sigma = X' * X / m; %计算协方差矩阵 [U,S,V] = svd(sigma); %利用SVD函数计算降维后的特征向量集U和对角矩阵S % ========================================================================= end
projectData.m
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);%计算X在新维度下的表示Z % ============================================================= end
recoverData.m
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)'; %重建X,把X从K维度重建为N维度 % ============================================================= end
相关文章推荐
- Machine Learning week 8 programming exercise K-means Clustering and Principal Component Analysis
- coursera Machine Learning 第八周 测验quiz2答案解析 Principal Component Analysis
- Machine Learning week 8 quiz: programming assignment-K-Means Clustering and PCA
- Programming Exercise 7:K-means Clustering and Principal Component Analysis 第一部分
- Programming Exercise 7:K-means Clustering and Principal Component Analysis (第二部分PCA)
- Stanford ex7: K-means Clustering and Principal Component Analysis
- Programming Exercise 7: K-means Clustering and Principal Component Analysis Machine Learning
- (六)K-means Clustering and Principal Component Analysis[K-means聚类&主元分析]
- Coursera Machine Learning 第八周week8ex7 K-Means Clustering and PCA编程全套满分题目+注释
- Andrew NG 机器学习 练习7-K-means Clustering and Principal Component Analysis
- Coursera Machine Learning 第八周 quiz Principal Component Analysis
- Coursera Machine Learning 第四周 quiz Programming Exercise 3 Multi-class Classification and Neural
- MachineLearning Exercise 7 : K-means Clustering and Principle Component Analysis
- Coursera Machine Learning 第九周 quizProgramming Exercise 8: Anomaly Detection and Recommender Systems
- Machine Learning 第七波编程作业——K-means Clustering and Principal Component Analysis
- Stanford 机器学习 Week8 作业:K-means Clustering and Principal Component Analysis
- Coursera Machine Learning 第三周 quiz Programming Exercise 2: Logistic Regression
- Coursera Machine Learning 第二周 quiz Programming Exercise 1: Linear Regression
- Coursera Machine Learning 第五周 quiz Programming Exercise 4: Neural Networks Learning
- Coursera Machine Learning 第七周 quiz Programming Exercise 6: Support Vector Machines