Python 如何确定两条线之间的距离何时在某个阈值内?

Python 如何确定两条线之间的距离何时在某个阈值内?,python,pandas,numpy,matplotlib,Python,Pandas,Numpy,Matplotlib,我有一个带有主要数据点(蓝线)和最大值(绿色)和最小值(红色)的图表。 请注意,最小值和最大值的x值不相同,也不能保证它们具有相同的值计数 现在我的目标是确定沿y轴的最大值线和最小值线之间的距离(积分?对不起,自从uni中的微积分以来已经有一段时间了)何时低于沿y轴的平均距离的10%(或任何其他任意阈值) 以下是用于生成以下内容的代码: # Finding the min and max c_max_index = argrelextrema(df.flow.values, np.greate

我有一个带有主要数据点(蓝线)和最大值(绿色)和最小值(红色)的图表。

请注意,最小值和最大值的x值不相同,也不能保证它们具有相同的值计数

现在我的目标是确定沿y轴的最大值线和最小值线之间的距离(积分?对不起,自从uni中的微积分以来已经有一段时间了)何时低于沿y轴的平均距离的10%(或任何其他任意阈值)

以下是用于生成以下内容的代码:

# Finding the min and max
c_max_index = argrelextrema(df.flow.values, np.greater, order=3)
c_min_index = argrelextrema(df.flow.values, np.less, order=3)

df['min_extreme'] = df.flow[c_min_index[0]]
df['max_extreme'] = df.flow[c_max_index[0]]

# Plotting the values for the graph above
plt.plot(df.flow.values)
upper_bound = plt.plot(c_max_index[0], df.flow.values[c_max_index[0]], linewidth=0.8, c='g')
lower_bound = plt.plot(c_min_index[0], df.flow.values[c_min_index[0]], linewidth=0.8, c='r')

如果有区别,我使用的是Pandas Dataframe、scipy和matplotlib。

如果我正确理解您的问题,您基本上希望插值由极值定义的线。从这篇文章中窃取答案,你可以这样做

# Finding the min and max
c_max_index = argrelextrema(df.flow.values, np.greater, order=3)
c_min_index = argrelextrema(df.flow.values, np.less, order=3)

df['min_extreme'] = df.flow[c_min_index[0]]
df['max_extreme'] = df.flow[c_max_index[0]]

# Interpolate so you get no 'nan' values
df['min_extreme'] = df['min_extreme'].interpolate()
df['max_extreme'] = df['max_extreme'].interpolate() 
从这里可以很容易地处理两条线之间有距离的所有东西。比如说

# Get the average distance between the upper and lower extrema-lines
df['distance'] = df['max_extreme'] - df['min_extreme']
avg_dist = np.mean(df['distance'])

# Find indexes where distance is within some tolerance
df.index[df['distance']< avg_dist * .95]
#获取上、下肢线之间的平均距离
df['distance']=df['max\u extreme']-df['min\u extreme']
平均距离=np.平均值(df[“距离])
#查找距离在一定公差范围内的索引
df.索引[df['distance']
这绝对不是一个完美的解决方案。它的目的是给你一些想法,如何可以做到这一点,因为没有更多的数据

您试图解决的主要问题是处理两条分段直线。而且碎片没有对齐。一个明显的解决方案是对两者进行插值并获得x的并集。那么距离的计算就更容易了

import numpy as np
import matplotlib.pyplot as plt

# Toy data
x1 = [0, 1, 2, 3, 4, 5, 6]
y1 = [9, 8, 9, 10, 7, 6, 9]
x2 = [0.5, 3, 5, 6, 9]
y2 = [0, 1, 3, 2, 1]

# Interpolation for both lines
points1 = list(zip(x1, y1))
y1_interp = np.interp(x2, x1, y1)
interp_points1 = list(zip(x2, y1_interp))
l1 = list(set(points1 + interp_points1))
all_points1 = sorted(l1, key = lambda x: x[0])

points2 = list(zip(x2, y2))
y2_interp = np.interp(x1, x2, y2)
interp_points2 = list(zip(x1, y2_interp))
l2 = list(set(points2 + interp_points2))
all_points2 = sorted(l2, key = lambda x: x[0])

assert(len(all_points1) == len(all_points2))

# Since I do not have data points on the blue line, 
# I will calculate the average distance based on x's of all interpolated points
sum_d = 0
for i in range(len(all_points1)):
    sum_d += all_points1[i][1] - all_points2[i][1]
avg_d = sum_d / len(all_points1)
threshold = 0.5
d_threshold = avg_d * threshold

for i in range(len(all_points1)):
    d = all_points1[i][1] - all_points2[i][1]
    if d / avg_d < threshold:
        print("Distance below threshold between", all_points1[i], "and", all_points2[i])

您的问题是
min\u extreme
max\u extreme
没有完全对齐/定义。我们可以通过
插值
来解决它:

# this will interpolate values linearly, i.e data on the upper and lower lines
df = df.interpolate()

# vertical distance between upper and lower lines:
df['dist'] = df.max_extreme - df.min_extreme

# thresholding, thresh can be scalar or series
# thresh = 0.5 -- absolute value
# thresh = df.max_extreme / 2 -- relative to the current max_extreme

thresh = df.dist.quantile(0.5) # larger than 50% of the distances

df['too_far'] = df.dist.gt(thresh)

# visualize:
tmp_df = df[df.too_far]

upper_bound = plt.plot(c_max_index[0], df.flow.values[c_max_index[0]], linewidth=0.8, c='g')
lower_bound = plt.plot(c_min_index[0], df.flow.values[c_min_index[0]], linewidth=0.8, c='r')

df.flow.plot()

plt.scatter(tmp_df.index, tmp_df.min_extreme, s=10)
plt.scatter(tmp_df.index, tmp_df.max_extreme, s=10)
plt.show()
输出:


能否添加一些数据?能否定义“沿y轴的平均距离”?min_-extreme(max_-extreme)的最左边(最右边)部分如何,它在max_-extreme(min_-extreme)上没有对手?你只是想忽略吗?@YiBao是的,这些可以忽略,因为数据集实际上要大得多,并且会被均匀地修剪。平均距离可以定义为:(从绿线到红线的距离)蓝线上的每个x值/蓝线上的总点数太棒了!我发现这是最简单、最直接的解决方案。
# this will interpolate values linearly, i.e data on the upper and lower lines
df = df.interpolate()

# vertical distance between upper and lower lines:
df['dist'] = df.max_extreme - df.min_extreme

# thresholding, thresh can be scalar or series
# thresh = 0.5 -- absolute value
# thresh = df.max_extreme / 2 -- relative to the current max_extreme

thresh = df.dist.quantile(0.5) # larger than 50% of the distances

df['too_far'] = df.dist.gt(thresh)

# visualize:
tmp_df = df[df.too_far]

upper_bound = plt.plot(c_max_index[0], df.flow.values[c_max_index[0]], linewidth=0.8, c='g')
lower_bound = plt.plot(c_min_index[0], df.flow.values[c_min_index[0]], linewidth=0.8, c='r')

df.flow.plot()

plt.scatter(tmp_df.index, tmp_df.min_extreme, s=10)
plt.scatter(tmp_df.index, tmp_df.max_extreme, s=10)
plt.show()