编辑 好的,我下载,安装并教导自己numpy。这里是一个numpy的版本
老答案
我知道你想有一个numpy的答案。我的numpy是生锈的,但由于没有其他答案,我以为我会在Matlab中给你一个。它应该直接转换。我假设问题是概念,而不是代码。
请注意,有很多方法来剥皮这只猫,我只是给一个。
工作numpy的版本
import numpy as np
pcar = np.random.rand(10,3)
out_list=pcar[np.random.randint(0,pcar.shape[0],2)]
ol_1 = out_list[0,:]
ol_2 = out_list[1,:]
## Get the individual distances
## The trick here is to pre-multiply the 1x3 ol vector with a row of
## ones of size 10x1 to get a 10x3 array with ol replicated, so that it
## can simply be subtracted
d1 = pcar - ones(size(pcar,1))*ol_1
d2 = pcar - ones(size(pcar,1))*ol_2
##% Square them using an element-wise square
d1s = np.square(d1)
d2s = np.square(d2)
##% Sum across the rows, not down columns
d1ss = np.sum(d1s, axis=1)
d2ss = np.sum(d2s, axis=1)
##% Square root using an element-wise square-root
e1 = np.sqrt(d1ss)
e2 = np.sqrt(d2ss)
##% Assign to class one or class two
##% Start by assigning one to everything, then select all those where ol_2
##% is closer and assign them the number 2
assign = ones(size(e1,0));
assign[e2<e1] = 2
##% Separate
pcar1 = pcar[ assign==1, :]
pcar2 = pcar[ assign==2, :]
工作matlab版
close all
clear all
% Create 10 records each with 3 attributes
pcar = rand(10, 3)
% Pick two (normally at random of course)
out_list = pcar(1:2, :)
% Hard-coding this separately, though this can be done iteratively
ol_1 = out_list(1,:)
ol_2 = out_list(2,:)
% Get the individual distances
% The trick here is to pre-multiply the 1x3 ol vector with a row of
% ones of size 10x1 to get a 10x3 array with ol replicated, so that it
% can simply be subtracted
d1 = pcar - ones(size(pcar,1), 1)*ol_1
d2 = pcar - ones(size(pcar,1), 1)*ol_2
% Square them using an element-wise square
d1s = d1.^2
d2s = d2.^2
% Sum across the rows, not down columns
d1ss = sum(d1s, 2)
d2ss = sum(d2s, 2)
% Square root using an element-wise square-root
e1 = sqrt(d1ss)
e2 = sqrt(d2ss)
% Assign to class one or class two
% Start by assigning one to everything, then select all those where ol_2
% is closer and assign them the number 2
assign = ones(length(e1),1);
assign(e2<e1)=2
% Separate
pcar1 = pcar(assign==1, :)
pcar2 = pcar(assign==2, :)
% Plot
plot3(pcar1(:,1), pcar1(:,2), pcar1(:,3), 'g+')
hold on
plot3(pcar2(:,1), pcar2(:,2), pcar2(:,3), 'r+')
plot3(ol_1(1), ol_1(2), ol_1(3), 'go')
plot3(ol_2(1), ol_2(2), ol_2(3), 'ro')
如果两点具有相同的x,y,z会发生什么? – Harpal