Andrew Ng coursera上的《机器学习》ex7
来源:互联网 发布:北京铁路局网络通信 编辑:程序博客网 时间:2024/05/21 03:20
Andrew Ng coursera上的《机器学习》ex7
按照课程所给的ex7的文档要求,ex7要求完成以下几个计算过程的代码编写:
一、findClosestCentroids.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 KK = 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) tmp=zeros(K,1); for j=1:K tmp(j)=sum((X(i,:)-centroids(j,:)).^2); end [~, idx(i)]=min(tmp,[],1);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 for-loop over the centroids to compute this.%num = zeros(K,1);for k = 1:K for i = 1:m if idx(i) == k centroids(k,:) = centroids(k,:) + X(i,:); num(k) = num(k) + 1; end end centroids(k,:) = centroids(k,:)/num(k);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).%sig=1/m*X'*X;[U, S ,V]=svd(sig);% =========================================================================end
四、projectData.m
要求是根据上一个算法求出的U,计算出相应的压缩之后的数据。
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);%for i=1:size(X,1) for k=1:K x= X(i, :)'; Z(i,k) = x' * U(:, k); endend% =============================================================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.% for i=1:size(Z,1) for j=1:size(U,1) v = Z(i, :)'; X_rec(i,j) = v' * U(j, 1:K)'; endend% =============================================================end
0 0
- Andrew Ng coursera上的《机器学习》ex7
- Coursera上的Andrew Ng《机器学习》学习笔记Week1
- Coursera上的Andrew Ng《机器学习》学习笔记Week2
- Andrew Ng coursera上的《机器学习》ex1
- Andrew Ng coursera上的《机器学习》ex2
- Andrew Ng coursera上的《机器学习》ex3
- Andrew Ng coursera上的《机器学习》ex4
- Andrew Ng coursera上的《机器学习》ex5
- Andrew Ng coursera上的《机器学习》ex6
- Andrew Ng coursera上的《机器学习》ex8
- Coursera上Andrew Ng机器学习课程总结(一)
- Coursera上Andrew Ng机器学习课程总结(二)
- Coursera 的机器学习 (Andrew Ng) 课程 视频百度云
- coursera斯坦福Andrew Ng的机器学习编程作业答案
- Coursera机器学习(Andrew Ng)笔记1
- Coursera机器学习(Andrew Ng)笔记:神经网络
- Coursera机器学习(Andrew Ng)笔记:大规模机器学习
- Andrew Ng机器学习笔记ex7 K-means聚类和PCA
- 100个不能错过的实用JS自定义函数
- [iOS AFNetWrokign 3.0以上] AFNetWrokign3.0.X 版本使用简介
- oracle数据库用户及权限操作介绍
- Unity的【CaronteFX】插件制作掉落碰撞后破碎的物理效果(2)
- [LeetCode OJ]75. Sort Colors
- Andrew Ng coursera上的《机器学习》ex7
- android:UI设计
- Docker内置编排生产准备就绪:Docker 1.12 面向大众推出
- android 国际化后button 上的英文变成了大写
- 欢迎使用CSDN-markdown编辑器
- jenkins配置
- 【C程序设计语言】第三章-控制流 | 练习
- 一个AIDL的简单使用
- 建立ssh信任关系