2017-02-24 98 views
0

我有一个足球运动员的位置数据。现在我必须计算数据集中玩家A到其他玩家B,C,D的距离,并根据它们的接近程度排列该列。数据看起来像这样计算距离每其他点的位置数据

ts1 = np.random.rand(10) 
ts2 = np.random.rand(10) 
ts3 = np.random.rand(10) 
ts4 = np.random.rand(10) 
ts5 = np.random.rand(10) 
ts6 = np.random.rand(10) 
ts7 = np.random.rand(10) 
ts8 = np.random.rand(10) 
d = {'A_x': ts1, 'A_y': ts2,'B_x': ts3, 'B_y':ts4, 'C_x':ts5, 'C_y':ts6, 
         'D_x':ts7, 'D_y':ts8} 
df = pd.DataFrame(data=d) 

这样的数据看起来像这样 DF

A_x  A_y  B_x  B_y  C_x  C_y  D_x \ 

0 0.423073 0.980156 0.513101 0.842604 0.196775 0.482592 0.419169
1 0.363428 0.520050 0.799685 0.184905 0.919634 0.483978 0.313876
2 0.029975 0.010020 0.739184 0.443437 0.884022 0.328365 0.448889
3 0.674032 0.399175 0.512700 0.697177 0.414588 0.915752 0.095993
4 0.962292 0.939266 0.466896 0.406284 0.922713 0.405991 0.798676
5 0.929178 0.936950 0.886348 0.639929 0.518401 0.848468 0.985375
6 0.290297 0.398941 0.896976 0.775312 0.976815 0.546444 0.937562
7 0.332582 0.191384 0.075902 0.645477 0.433419 0.917658 0.850850
8 0.817298 0.612915 0.903426 0.662707 0.825628 0.648411 0.556697
9 0.383350 0.715573 0.994297 0.491445 0.785036 0.110449 0.072658

D_y 

0 0.909314
1 0.235216
2 0.284318
3 0.022496
4 0.756945
5 0.772382
6 0.850470
7 0.586915
8 0.799697
9 0.391556

所以第一问题是如何计算的距离为每对列?所以输出应该有3列,如: AB AC AD 而第二个问题是,我怎么能生成一个新的位置表的每一行,列将由他们是A.如何接近,因此新表下令将有像 A_X A_y nearest1st_X列nearest1st_y nearest2nd_x nearest2nd_y nearest3d_x nearest3d_y 感谢

回答

0

您可以使用numpy.linalg.norm:

dist = numpy.linalg.norm(a-b) 

或SciPy的有一个函数,该函数,这就是所谓的欧几里德

from scipy.spatial import distance 
a = (1,2,3) 
b = (4,5,6) 
dst = distance.euclidean(a,b) 

,包括这一个循环读取每一列

编辑:

这也许不是最好的方式,但一个例子:我知道如何计算距离

import numpy as np 

num_P=10 
ts1 = np.random.rand(num_P) 
ts2 = np.random.rand(num_P) 
ts3 = np.random.rand(num_P) 
ts4 = np.random.rand(num_P) 
ts5 = np.random.rand(num_P) 
ts6 = np.random.rand(num_P) 
ts7 = np.random.rand(num_P) 
ts8 = np.random.rand(num_P) 
d = {'A_x': ts1, 'A_y': ts2,'B_x': ts3, 'B_y':ts4, 'C_x':ts5, 'C_y':ts6, 
         'D_x':ts7, 'D_y':ts8} 
f={} 
for i in range(num_P): 
    print i 
    nkey="postion "+ str(i) 
    f[nkey]=[] 
    position=[item[i] for item in d.values()] 
     for n,k in enumerate(position[:-1]): 
     a=position[n] 
     b=position[n+1]   
     dist = np.linalg.norm(a-b) 
     f[nkey].append(dist) 
+0

但你知道我怎样才能同时计算它们吗? – qnguyen

+0

您可以通过'df''for'循环读数计算每个距离。 – Dadep

+0

请参阅文章中的编辑 – Dadep