無監督學習——K-means演算法
阿新 • • 發佈:2019-01-07
筆記:
核心步驟:
那我們就實現這兩個函式就行啦:
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 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.
%
m = size(X,1);
dis = zeros(m,K); %(m,k)位置表示第m個樣本和第K個聚類中心的距離的平方
for i=1:m
for j=1:K
dis(i,j) = X(i,:)*X(i,:)' + centroids(j,:)*centroids(j,:)' - ...
X(i,:)*centroids(j,:)'*2;
end
end
[~, idx] = min(dis,[],2); %尋找每一行中最小的元素索引
% =============================================================
end
computeCentroids.m(更新聚類中心):
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.
%
for i=1:K
index = find(idx == i);
centroids(i,:) = mean(X(index,:));
end
% =============================================================
end
看看聚類中心是怎麼變化的吧~
剩下的基本不怎麼變啦~
還有一點需要注意:聚類中心的隨機初始化:
Code(kMeansInitCentroids.m):
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
%
% Randomly reorder the indices of examples
randidx = randperm(size(X, 1));
% Take the first K examples as centroids
centroids = X(randidx(1:K), :);
% =============================================================
end
另外最後還給了個例子,是關於影象顏色壓縮的,也是用的K-means演算法,並不是很難,自己看看了解一下就好~