我正在编写一个 Python 脚本,该脚本应该以交互方式允许用户从进一步的计算中选择和删除数据,但是,因为我的 y 轴数据在 e-9 到 e-14 的范围内(单个分析包含超过一个数量级,但该数量级可能在 e-9 和 e-14 之间),并且我的 x 轴数据在 10-100 范围内,数量级的差异导致 y 轴主导最近距离计算。结果,如果我有两个由
*
表示的数据,以及一个由 x
表示的点击点:
|
| *
|
|
|
|
| x *
|__________________
单击点显然更接近较低的基准,但是,由于 y 轴主导最近点计算,因此选择了较高的基准。
我尝试将时间数据(x 轴)缩小到原始数据(y 轴)的数量级,反之亦然,如下所示:
# this finds the closest datum to the click point
def find_closest_index(analysis, click_coords, mass):
y_magnitude = np.floor(np.log10(np.max(analysis.raw_data[mass])))
scale_factor = 10**abs(y_magnitude - 1)
scaled_data = analysis.raw_data[mass] * scale_factor
distances = np.sqrt(
(analysis.time_sec - click_coords[0])**2 +
(scaled_data - click_coords[1])**2
)
# find the closest index
closest_index = distances.argmin()
return closest_index
将scale_factor设置为
y_magnitude - 1
会生成大约10-100的缩放y轴数据,这应该是正确的,但是,上述行为仍然存在。将 -1
更改为 -3
或 +3
最终会错误地选择最大或最小数据,而且我还没有找到可以改变上述基本行为的中间值。
我该如何解决此问题并确保正确选择较低的基准?
使用 scikit-learn 作品中的 MinMaxScaler():
# this finds the closest datum to the click point
def find_closest_index(analysis, click_coords, mass):
x_data = analysis.time_sec.to_numpy().reshape(-1,1)
y_data = analysis.raw_data[mass].to_numpy().reshape(-1,1)
# define the scaler and scale the x and y data
scaler = MinMaxScaler()
scaled_data = scaler.fit_transform(np.concatenate((x_data, y_data), axis=1))
scaled_x = scaled_data[:, 0]
scaled_y = scaled_data[:, 1]
# scale click coordinates
scaled_click_coords = scaler.transform([click_coords])
scaled_click_x = scaled_click_coords[0, 0]
scaled_click_y = scaled_click_coords[0, 1]
# calculate distances from click point to data
distances = np.sqrt(
(scaled_x - scaled_click_x)**2 +
(scaled_y - scaled_click_y)**2
)
# find the closest index
closest_index = distances.argmin()
return closest_index