动作 为装箱的3D点集中的每个bin选择最靠近质心的位置。
尝试
使用binned_statistic_dd
中的scipy.stats
我试图获取此3D点集的分箱平均值。但是,它一次只能计算其中一个维度的平均值。
import numpy as np
from scipy.stats import binned_statistic_dd
points = np.array([range(2000), range(2000), range(2000)]).T
# points[:,1] is y
y_means, bin_edges, bin_indices = binned_statistic_dd(points, points[:, 1], statistic=np.mean, bins=np.cbrt(1000))
y_means.shape # (10,10,10)
接下来将计算每个均值到所有原始点的距离并采用最接近的点。导致以下完整实施:
def grid_sample(df):
x_mean, edges, binnumber = binned_statistic_dd(df[['x', 'y', 'z']].values, df.x.values, bins=np.cbrt(1000), statistic=np.mean)
y_mean, edges, binnumber = binned_statistic_dd(df[['x', 'y', 'z']].values, df.y.values, bins=np.cbrt(1000), statistic=np.mean)
z_mean, edges, binnumber = binned_statistic_dd(df[['x', 'y', 'z']].values, df.z.values, bins=np.cbrt(1000), statistic=np.mean)
xyz_mean = pd.DataFrame({'x_mean':x_mean.flatten(), 'y_mean':y_mean.flatten(), 'z_mean':z_mean.flatten()}).dropna()
def find_nearest(point, df=df):
idx = (np.sum(np.abs(df[['x', 'y', 'z']].values-point.values)**2, axis=1)**.5).argmin()
return df.iloc[idx]
df = xyz_mean.apply(find_nearest, axis=1)
return df
问题 两个问题:
closest-to-centroid
功能?我在scipy.stats