计算python中PCA的欧氏距离

时间:2014-04-09 14:32:57

标签: python numpy pca euclidean-distance

我有3D numpy array的PCA

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)]

给numpy数组2行。

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

2 个答案:

答案 0 :(得分:2)

修改 好的,我下载,安装和自学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 :(得分:2)

Scipy中有一个非常快速的实现:

 from scipy.spatial.distance import cdist, pdist

cdist接受两个向量,例如你的pchar,并计算每个点之间的距离。 pdist只会给你那个矩阵的上三角。

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