2014-04-09 93 views
1

我有PCA与3D numpy array作为计算欧几里得距离PCA在python

pcar =[[xa ya za] 
     [xb yb zb] 
     [xc yc zc] 
     . 
     . 
     [xn yn zn]] 

其中每行是一个点,我已经选择从上方PCA任意二个随机的行作为一个群集作为

out_list=pcar[numpy.random.randint(0,pcar.shape[0],2)] 

它给出了2行的numpy数组。

我必须从每行out_list与pcar中的每行(点)找到欧几里得距离,并将该pcar点添加到out_list集群中的最近点。

+0

如果两点具有相同的x,y,z会发生什么? – Harpal

回答

2

有在Scipy一个非常快速的实施:

from scipy.spatial.distance import cdist, pdist 

cdist需要两个向量,比如你的pchar,然后计算这些点之间的距离。 pdist只会给你那个矩阵的上三角形。

由于它们是在C或Fortran幕后执行的,因此它们非常高效。

+0

另请参阅以下内容:http://stackoverflow.com/questions/17527340/more-efficient-way-to-calculate-distance-in-numpy如果您的阵列变得巨大,那么这些方法并不是最好的选择。 – usethedeathstar

2

编辑 好的,我下载,安装并教导自己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') 
+1

虽然这很有用,但它并不能回答OP的问题,因为他们需要Python,但仍然付出很大努力 – EdChum

+1

@EdChum我意识到OP使用numpy。如果问题是这个概念,那么在伪代码中的答案将会很好。如果伪代码可以很好地运行,为什么不使用Matlab,这是numpy的第一代堂兄弟?无论如何,我认为它不能伤害。 – timbo

+2

@EdChum好吧,我提供了一个numpy版本。这是一个信誉stackoverflow和python,我可以下载,安装和学习足够的numpy来翻译我的代码约30米! – timbo

相关问题