Reputation: 2977
I have a data set that has points
in variable number of dimensions with coordinates of both raw and corresponding center:
point | c_1 | c_2 | ... | c_n | center_1 | center_2 | ... | center_n
--------------------------------------------------------------------
p_1 | 0.1 | 0.3 | ... | 0.5 | 1.2 | 1.1 | ... | 0.7
p_2 | 1.0 | 1.5 | ... | 1.7 | 3.1 | 2.0 | ... | 1.3
p_3 | 0.5 | 0.8 | ... | 1.0 | 2.0 | 1.2 | ... | 3.8
... | ... | ... | ... | ... | ... | ... | ... | ...
For now I need to compute the Euclidean
distance of each point to its center.
For instance, a simplified 3-dimensional data set with three points would look like:
point | c_1 | c_2 | c_3 | center_1 | center_2 | center_3 | distance
-------------------------------------------------------------------
p_1 | 0.0 | 0.0 | 0.0 | 1.0 | 1.0 | 1.0 | 1.732
p_2 | 1.0 | 1.0 | 1.0 | 3.0 | 3.0 | 3.0 | 3.464
p_3 | 0.5 | 0.5 | 0.5 | 2.0 | 2.0 | 2.0 | 2.598
I can do the following on 1-dimension:
import pandas as pd
import numpy as np
points = pd.DataFrame({
"point": ("p_1", "p_2", "p_3"),
"c_1": (0.0, 1.0, 0.5),
"c_2": (0.0, 1.0, 0.5),
"c_3": (0.0, 1.0, 0.5),
"center_1": (1.0, 3.0, 2.0),
"center_2": (1.0, 3.0, 2.0),
"center_3": (1.0, 3.0, 2.0)
})
points['distance'] = points.apply(lambda row:
np.linalg.norm(row['c_1']-row['center_1']), axis=1)
but how to better do this on variable number of dimensions giving a range, say 10?
Upvotes: 0
Views: 49
Reputation: 323276
IIUC
from scipy.spatial import distance
a=distance.cdist(df[['c_1','c_2','c_2']].values, df[['center_1','center_2','center_3']].values)
a[np.arange(len(a)),np.arange(len(a))]
Out[249]: array([1.73205081, 3.46410162, 2.59807621])
Upvotes: 1