The spatial package imported from Scipy can measure the Euclidean distance between specified points. Is it possible to return the same measurement by using the Delaunay package? Using the df below, the average distance between all points is measured grouped by Time. However, I'm hoping to use Delaunay triangulation to measure the average distance.
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)
Average distance between all points can be measured using below but I'm hoping to incorporate 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()
)
Intended Output:
Time 0
0 1 1.082842
1 2 1.082842