1.K-means方法是什么?
1.首先K-meas方法是一种无监督的聚类问题。
2.方法内容:首先需要确定聚类中心u和聚类数量K:我们可以随机选择中心u;当K<m(m是样本数),我们可以随机选择K个样本作为聚类中心,这个方法一般效果会好一些。聚类数量K我们可以通过最小化K-means的优化目标函数J来实现,得到“肘部规则”的拐点,即可找到合适的聚类数量。但是往往很多时候,通过这种方法并不能得到具有拐点的变化曲线,因此我们也可以尝试借助后续目标来确定聚类数量。比如卖衣服:有S、M、L三个号码,那我们可以将聚类数量K=3,但是号码也是是xs、s、M、L、xL,我们也可以将聚类数目定为5,如何确定3还是5呢,我们可以根据两种分类方法后续的销量和利润进行对比,选择最佳聚类数目。
3.方法步骤:
step1:首先我们随机确定K个聚类中心,这里的表示的是坐标。
step2:分类:在这里我们根据每个样本点到K个聚类中心的欧式距离来测算相似度。我们将每个样本点划分到与它距离最短的聚类中。用来记录每个样本点所属的聚类。比如:都距离聚类2最近,那么
step3:计算各个聚类中心的新位置:使用每个聚类中样本坐标的平均值。比如:聚类2中有三个样本,那么为聚类2的新坐标。
step4:不断重复2,3直到聚类不再变化。
notes:假如没有点聚类到某一个聚类中,那么直接将这个聚类删除。
2.使用优化目标函数的K-means
step1:首先进行不加入优化目标函数的k-means第2,3步,计算出,其中已经进行过聚类的所属聚类的中心坐标。
step2:固定不变,最小化J,也就是对所用点重新进行聚类操作。计算.
step3:最小化J,计算。(???不太清楚 利用2更新的聚类划分)
note:wrt:with respect to 着眼于
3.编程作业
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);
nums=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),
temp=1000000;
tempidx=0;
for j=1:size(centroids,1),
nums(j)=sum((X(i,:)-centroids(j,:)).^2);
if nums(j)<=temp,
temp=nums(j);
tempidx=j;
end
idx(i)=tempidx;
end
% =============================================================
end
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);
centroids_temp = 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:size(centroids,1),
temp=0;
for j=1:size(idx,1),
if idx(j)==i,
centroids_temp(i,:)=centroids_temp(i,:)+X(j,:);
temp=temp+1;
end
centroids(i,:)=centroids_temp(i,:)/temp;
end
% =============================================================
end