【发布时间】:2021-02-08 07:23:39
【问题描述】:
从Scipy导入的spatial包可以测量指定点之间的欧几里得距离。是否可以使用Delaunay 包返回相同的测量值?使用下面的df,测量所有点之间的平均距离,并按Time 分组。但是,我希望使用 Delaunay 三角测量来测量平均距离。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay
df = pd.DataFrame({
'Time' : [1,1,1,1,2,2,2,2],
'A_X' : [5, 5, 6, 6, 4, 3, 3, 4],
'A_Y' : [5, 6, 6, 5, 5, 6, 5, 6],
})
def make_points(x):
return np.array(list(zip(x['A_X'], x['A_Y'])))
points = df.groupby("Time").apply(make_points)
for p in points:
tri = Delaunay(p)
ax.triplot(*p.T, tri.simplices)
所有点之间的平均距离可以使用以下方法测量,但我希望将 Delaunay 纳入其中。
avg_dist = (df.groupby(['Time'])
.apply(lambda x: spatial.distance.pdist
(np.array(list(zip(x['A_X'], x['A_Y']))))
.mean() if len(x) > 1 else 0)
.reset_index()
)
预期输出:
Time 0
0 1 1.082842
1 2 1.082842
【问题讨论】:
标签: python pandas scipy delaunay