我定义了以下函数,它适用于二维数组。 angle
函数用于计算向量之间的夹角。
在调用以下函数时,它将以 "directions" 作为参数,该参数是一个2d数组(其中有2列,一列带有x值,另一列带有y值)。
现在,directions
是通过应用 np.diff()
函数2d数组获得的。
import matplotlib.pyplot as plt
import numpy as np
import os
import rdp
def angle(dir):
"""
Returns the angles between vectors.
Parameters:
dir is a 2D-array of shape (N,M) representing N vectors in M-dimensional space.
The return value is a 1D-array of values of shape (N-1,), with each value between 0 and pi.
0 implies the vectors point in the same direction
pi/2 implies the vectors are orthogonal
pi implies the vectors point in opposite directions
"""
dir2 = dir[1:]
dir1 = dir[:-1]
return np.arccos((dir1*dir2).sum(axis=1)/(np.sqrt((dir1**2).sum(axis=1)*(dir2**2).sum(axis=1))))
tolerance = 70
min_angle = np.pi*0.22
filename = os.path.expanduser('~/tmp/bla.data')
points = np.genfromtxt(filename).T
print(len(points))
x, y = points.T
# Use the Ramer-Douglas-Peucker algorithm to simplify the path
# http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
# Python implementation: https://github.com/sebleier/RDP/
simplified = np.array(rdp.rdp(points.tolist(), tolerance))
print(len(simplified))
sx, sy = simplified.T
# compute the direction vectors on the simplified curve
directions = np.diff(simplified, axis=0)
theta = angle(directions)
# Select the index of the points with the greatest theta
# Large theta is associated with greatest change in direction.
idx = np.where(theta>min_angle)[0]+1
我希望将上述代码应用于轨迹数据的 pandas.DataFrame
上。
下面是示例 df
。具有相同 subid
的 sx
、sy
值被视为一个轨迹,例如行(0-3)与 2 具有相同的 subid
,而 id
为 11 的点被视为一条轨迹上的点。行(4-6)也是一条轨迹。因此,每当 subid
或 id
发生更改时,就会发现单独的轨迹数据。
id subid simplified_points sx sy
0 11 2 (3,4) 3 4
1 11 2 (5,6) 5 6
2 11 2 (7,8) 7 8
3 11 2 (9,9) 9 9
4 11 3 (10,12) 10 12
5 11 3 (12,14) 12 14
6 11 3 (13,15) 13 15
7 12 9 (18,20) 18 20
8 12 9 (22,24) 22 24
9 12 9 (25,27) 25 27
上述数据框已经应用了rdp算法,simplified_points
进一步解压成两列sx
和sy
是rdp算法的结果。
问题在于如何获取每条轨迹的directions
,然后随后获取theta
和idx
。由于上面的代码仅为一个轨迹实现,而且还是在2d数组上实现的,所以我无法将其实现到以上pandas数据框中。
请建议我一种方法来为df中的每个轨迹数据实现上述代码。