Random initialization
This commit is contained in:
parent
6c51a29ca2
commit
5f3f65c69c
2 changed files with 6 additions and 7 deletions
|
@ -86,6 +86,7 @@ max_iters = 10;
|
||||||
% settings them to be random examples (as can be seen in
|
% settings them to be random examples (as can be seen in
|
||||||
% kMeansInitCentroids).
|
% kMeansInitCentroids).
|
||||||
initial_centroids = [3 3; 6 2; 8 5];
|
initial_centroids = [3 3; 6 2; 8 5];
|
||||||
|
%initial_centroids = kMeansInitCentroids(X, K);
|
||||||
|
|
||||||
% Run K-Means algorithm. The 'true' at the end tells our function to plot
|
% Run K-Means algorithm. The 'true' at the end tells our function to plot
|
||||||
% the progress of K-Means
|
% the progress of K-Means
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
function centroids = kMeansInitCentroids(X, K)
|
function centroids = kMeansInitCentroids(X, K)
|
||||||
%KMEANSINITCENTROIDS This function initializes K centroids that are to be
|
%KMEANSINITCENTROIDS This function initializes K centroids that are to be
|
||||||
%used in K-Means on the dataset X
|
%used in K-Means on the dataset X
|
||||||
% centroids = KMEANSINITCENTROIDS(X, K) returns K initial centroids to be
|
% centroids = KMEANSINITCENTROIDS(X, K) returns K initial centroids to be
|
||||||
% used with the K-Means on the dataset X
|
% used with the K-Means on the dataset X
|
||||||
|
@ -13,12 +13,10 @@ centroids = zeros(K, size(X, 2));
|
||||||
% the dataset X
|
% 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), :);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
% =============================================================
|
% =============================================================
|
||||||
|
|
||||||
|
|
Reference in a new issue