Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to define a distance function where you are using hypot. Usually in K-means it is Distance=sum((point-centroid)^2) Here is some matlab code that does it ... I can port it if you can't, but give it a go. Like you said, only way to learn.</p> <pre><code>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); [numberOfExamples numberOfDimensions] = size(X); % You need to return the following variables correctly. idx = zeros(size(X,1), 1); % 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 % for loop=1:numberOfExamples Distance = sum(bsxfun(@minus,X(loop,:),centroids).^2,2); [value index] = min(Distance); idx(loop) = index; end; end </code></pre> <p>UPDATE</p> <p>This should return the distance, notice that the above matlab code just returns the distance(and index) of the closest centroid...your function returns all distances, as does the one below.</p> <pre><code>def FindDistance(X,centroids): K=shape(centroids)[0] examples, dimensions = shape(X) distance = zeros((examples,K)) for ex in xrange(examples): distance[ex,:] = np.sum((X[ex,:]-centroids)**2,1) return distance </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload