注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

gmd20的个人空间

// 编程和生活

 
 
 

日志

 
 

机器学习导论 第七周作业 K-means Clustering算法  

2011-12-06 00:36:16|  分类: 程序设计 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

举了一个图像压缩方面的例子,把24位颜色的图像,要压缩成16色的图像,先用k-mean算法计算,用k=16进行,把图像划分了16类,然后在把属于该类的像素点颜色改为中心点的颜色就可以得到最后的16色图像了。这里例子都是很有实际意义的啊!

另外一个例子说的人脸识别的多维数据怎么消减维数。
机器学习导论 第七周作业 K-means Clustering算法 - widebright - widebright的个人空间
 


 

 

-------------------------------------------------------------------------

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.

%

 

 

closest_centroid =  zeros(K,1);

for  i=1:size(X,1)

   for j=1:K

      temp = (X(i,:) - centroids (j,:)) .^2;

 closest_centroid(j)  = sum(temp);    

   end

   [val,index] = min(closest_centroid);

   idx(i) = index;

end

 

 

 

 

 

% =============================================================

 

end

------------------------------------------------------------------------------------

 

 

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 j=1:m   

        k =idx(j);

centroids(k,:) = centroids(k,:) + X(j,:);

num(idx(j))++;

   end 

   

   for i=1:K

        centroids(i,:)  =  centroids(i,:) /num(i);   

   end 

 

 

 

 

 

 

% =============================================================

 

 

end

 

 

--------------------------------------------------------------------------

function centroids = kMeansInitCentroids(X, K)

%KMEANSINITCENTROIDS This function initializes K centroids that are to be 

%used in K-Means on the dataset X

%   centroids = KMEANSINITCENTROIDS(X, K) returns K initial centroids to be

%   used with the K-Means on the dataset X

%

 

% You should return this values correctly

centroids = zeros(K, size(X, 2));

 

% ====================== YOUR CODE HERE ======================

% Instructions: You should set centroids to randomly chosen examples from

%               the dataset X

%

 

% Initialize the centroids to be random examples

% Randomly reorder the indices of examples

randidx = randperm(size(X, 1));

% Take the first K examples as centroids

centroids = X(randidx(1:K), :);

 

 

 

% =============================================================

 

end

 

----------------------------------------------------------

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);

 

 

 

% =========================================================================

 

end


-------------------------------------------------------

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

---------------------------------------------------------------------

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


  评论这张
 
阅读(868)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017