在python中计算PCA的欧氏距离

在python中计算PCA的欧氏距离,python,numpy,pca,euclidean-distance,Python,Numpy,Pca,Euclidean Distance,我有3Dnumpy数组的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行 我必须用pcar中的每一行(点)找到out_列表中每一行的欧几里德距离

我有3D
numpy数组的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行

我必须用pcar中的每一行(点)找到out_列表中每一行的欧几里德距离,并将该pcar点添加到out_列表簇中最近的点。

Edit 好的,我下载、安装并自学了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, :]
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')
将numpy导入为np
pcar=np.rand.rand(10,3)
out_list=pcar[np.random.randint(0,pcar.shape[0],2)]
ol_1=out_列表[0,:]
ol_2=out_列表[1,:]
##获取单个距离
##这里的技巧是将1x3 ol向量与一行
##大小为10x1的阵列,以获得一个10x3阵列,并复制ol,以便
##可以简单地减去
d1=pcar-一个(尺寸(pcar,1))*ol_1
d2=pcar-一个(尺寸(pcar,1))*ol_2
##%使用按元素排列的正方形将其对齐
d1s=np.平方(d1)
d2s=np.平方(d2)
##%跨行求和,而不是向下求列
d1ss=np.和(d1s,轴=1)
d2ss=np.和(d2s,轴=1)
##%使用元素平方根的平方根
e1=np.sqrt(d1ss)
e2=np.sqrt(d2ss)
##%分配给一班或二班
##%首先将一个分配给所有对象,然后选择所有包含ol_2的对象
##%再靠近一点,给他们分配数字2
赋值=个(大小(e1,0));
分配[e2编辑
好的,我下载、安装并自学了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, :]
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')
将numpy导入为np
pcar=np.rand.rand(10,3)
out_list=pcar[np.random.randint(0,pcar.shape[0],2)]
ol_1=out_列表[0,:]
ol_2=out_列表[1,:]
##获取单个距离
##这里的技巧是将1x3 ol向量与一行
##大小为10x1的阵列,以获得一个10x3阵列,并复制ol,以便
##可以简单地减去
d1=pcar-一个(尺寸(pcar,1))*ol_1
d2=pcar-一个(尺寸(pcar,1))*ol_2
##%使用按元素排列的正方形将其对齐
d1s=np.平方(d1)
d2s=np.平方(d2)
##%跨行求和,而不是向下求列
d1ss=np.和(d1s,轴=1)
d2ss=np.和(d2s,轴=1)
##%使用元素平方根的平方根
e1=np.sqrt(d1ss)
e2=np.sqrt(d2ss)
##%分配给一班或二班
##%首先将一个分配给所有对象,然后选择所有包含ol_2的对象
##%再靠近一点,给他们分配数字2
赋值=个(大小(e1,0));

分配[e2在以下方面有一个非常快速的实现:

cdist采用两个向量,如pchar 1,并计算每个点之间的距离。 pdist将只给出该矩阵的上三角形


由于它们是在后台用C或Fortran实现的,因此性能非常好。

在以下方面有一个非常快速的实现:

cdist采用两个向量,如pchar 1,并计算每个点之间的距离。 pdist将只给出该矩阵的上三角形


由于它们是在后台用C或Fortran实现的,因此性能非常好。

如果两个点具有相同的x,y,z,会发生什么?如果两个点具有相同的x,y,z,会发生什么?尽管这很有用,但它并没有像python中要求的那样回答OP的问题,很好的工作nonetheless@EdChum我意识到OP使用的是numpy。如果p问题是这个概念,然后用伪代码回答就可以了。如果伪代码可以了,为什么不使用Matlab呢?Matlab是numpy的第一个表亲?无论如何,我想这不会有什么坏处。@EdChum好的,我提供了一个numpy版本。这是stackoverflow和python的功劳,我可以下载、安装并学习足够的numpy来翻译我的代码在大约30m!@timbo中,它给出了两个包含pcar的数组,其中少了一行,另一个数组包含单行,这是从pcar中删除的数组。@timbo您不应该使用“from numpy import*”,尤其是在您“import numpy as np”之后。除此之外,您可以通过执行“d1s=d12”和“e1=d1ss0.5”来提高可读性,而不是你正在使用的语法。很明显,你只知道numpy 30分钟,但再给它30分钟,你就会爱上它,再过30分钟,你就会意识到matlab是毫无意义的,只是占用了磁盘空间,而且钱可以花在比matlab许可证费更好的其他东西上;-)。欢迎来到numpy,它会毁了你的m虽然这很有用,但它并没有回答OP的问题,因为他们在python中需要它,很好的努力nonetheless@EdChum我意识到OP使用的是numpy。如果问题是概念,那么用伪代码回答就可以了。如果伪代码可以了,为什么不使用numpy的第一个表亲Matlab呢?无论如何,我想它不能赫特。@EdChum好的,我已经提供了一个numpy版本。这是stackoverflow和python的功劳,我可以下载、安装并学习足够的numpy来在大约30米的时间内翻译我的代码!@timbo它提供了两个带有pcar的数组,其中少了一行,另一个带有单行的数组是从pcar中删除的数组。@timbo您不应该使用nu中的'mpy import*,尤其是在“将numpy作为np导入”之后。除此之外,您可以通过执行“d1s=d12”和“e1=d1ss0.5”来提高可读性,而不是使用语法。很明显,您只知道numpy 30分钟,