python矩阵计算用numpy的原因_用numpy计算矩阵上的距离

您需要在使用hypo的地方定义一个距离函数。通常用K表示

距离=和((点形心)^2)

这里有一些matlab代码可以做到。。。如果你不能的话,我可以把它移走,但是试试看。就像你说的,学习的唯一途径。在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

更新

这应该返回距离,注意上面的matlab代码只返回最近质心的距离(和索引)…您的函数返回所有距离,下面的函数也是这样。在

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值