gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:33:29 25 4
gpt4 key购买 nike

我有一个包含主要数据点(蓝线)以及最大值(绿色)和最小值(红色)的图表。 enter image description here

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

现在我的目标是确定最大值和最小值线之间沿 y 轴的距离(积分?抱歉,单向微积分已经有一段时间了)低于 10%(或任何其他任意阈值)沿 y 轴的平均距离。

下面是用于生成的代码:

# 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。

最佳答案

如果我理解你的问题是正确的,你基本上想要插入由极值定义的线。从这篇文章中窃取答案Interpolate NaN values in a numpy array , 你可以这样做

# 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]

关于python - 如何确定两条线之间的距离何时在某个阈值内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56380368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com