我有一个包含主要数据点(蓝线)和最大值(绿色)和最小值(红色)的图形。
请注意,最小值和最大值的x值不相同,也不保证它们具有相同的值计数。
现在,我的目标是确定最大值和最小值线之间的y轴距离(积分吗?对不起,因为uni中的微积分已经有一段时间了)何时从该值开始小于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。
答案 0 :(得分:1)
如果我对您的问题理解正确,那么您基本上想对由极值定义的线进行插值。窃取此帖子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]
答案 1 :(得分:1)
这绝不是一个完美的解决方案。它旨在为您提供一些有关如何完成此操作的想法,因为没有更多数据。
您要解决的主要问题是处理两条分段直线。和碎片不对齐。一个明显的解决方案是对两者进行插值并获得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])
请注意,np.interp
也可以推断值,但它们不参与计算。
现在还有一个问题:如果您实际上需要知道何时,距离仅低于插值点就低于阈值,则需要分析地搜索每块中的第一个和最后一个点行。这是一个示例:
for i in range(len(all_points1) - 1):
(pre_x1, pre_y1) = all_points1[i]
(post_x1, post_y1) = all_points1[i + 1]
(pre_x2, pre_y2) = all_points2[i]
(post_x2, post_y2) = all_points2[i + 1]
# Skip the pieces that will never have qualified points
if (pre_y1 - pre_y2) / avg_d >= threshold and (post_y1 - post_y2) / avg_d >= threshold:
continue
k1 = (post_y1 - pre_y1) / (post_x1 - pre_x1)
b1 = (post_x1 * pre_y1 - pre_x1 * post_y1) / (post_x1 - pre_x1)
k2 = (post_y2 - pre_y2) / (post_x2 - pre_x2)
b2 = (post_x2 * pre_y2 - pre_x2 * post_y2) / (post_x2 - pre_x2)
x_start = (d_threshold - b1 + b2) / (k1 - k2)
print("The first point where the distance falls below threshold is at x=", x_start)
break
答案 2 :(得分:1)
您的问题是min_extreme
和max_extreme
并没有完全对齐/定义。我们可以通过interpolate
解决它:
# 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()
输出: