我有一些看起来像这样的数据:
生成此图的代码:
CI=4.5 data=pandas.DataFrame([3,5,1,2,3,4,5,6]) plt.figure() plt.plot(data) plt.plot([CI]*len(data),'--') plt.ylabel('y data',fontsize=15) plt.xlabel('x data',fontsize=15) plt.title('example data',fontsize=15)我也可以使用scipy插入数据:
from scipy.interpolate import interp1d f=interp1d(x_data,y_data,kind='linear') x_interp=numpy.linspace(min(x_data),max(x_data), num=100*len(x_data), endpoint=True) y_interp=f(x_interp) plt.figure() plt.plot(x_interp,y_interp)绿线表示置信区间。 我需要以编程方式定位x值,其中y值跨越此置信度内部。 但是,复杂的是因为这是一个置信区间,我需要获得两个方向上穿过绿线的值:
即我需要x值穿过红色箭头的x值,同时排除黑色箭头的值。 我尝试了从插值数据中减去置信区间以及取绝对值的许多变化,但我仍然无法隔离置信区间。 有人可以帮帮我吗? 提前致谢
I have some data that looks like this:
The code to generate this plot:
CI=4.5 data=pandas.DataFrame([3,5,1,2,3,4,5,6]) plt.figure() plt.plot(data) plt.plot([CI]*len(data),'--') plt.ylabel('y data',fontsize=15) plt.xlabel('x data',fontsize=15) plt.title('example data',fontsize=15)I can also interpolate the data using scipy:
from scipy.interpolate import interp1d f=interp1d(x_data,y_data,kind='linear') x_interp=numpy.linspace(min(x_data),max(x_data), num=100*len(x_data), endpoint=True) y_interp=f(x_interp) plt.figure() plt.plot(x_interp,y_interp)The green line represents a confidence interval. I need to programmatically locate the x values where the y values cross this confidence internal. But, the complication is that since this is a confidence interval I need to get the values that cross the green line in both directions:
i.e. I need the x values where the y data crosses the red arrows whilst excluding those at the black arrow. I've tried many variations of subtracting the confidence interval from the interpolated data as well as taking the absolute values but I still cannot isolate the confidence interval. Can anybody help me out? Thanks in advance
最满意答案
基于
我想要y值在特定范围内的x值
看来你只是在寻找一个面具。 如果给numpy一个关系语句,例如my_array > x ,它将为满足此关系的任何索引返回一个带有True的布尔数组。 如果将这样的掩码传递给数组进行索引,它将返回此掩码为True的值。 例如,
In [2]: a = np.array([1, 3, 2, 5, 2, 9, 4]) In [3]: a > 2 Out[3]: array([False, True, False, True, False, True, True], dtype=bool) In [4]: a[a > 2] Out[4]: array([3, 5, 9, 4])因此,要找到f(x)位于特定范围内的x值,请找到f(x)在所需范围内的索引,并根据该掩码过滤x 。
# multiplication between 1s and 0s acts like logical AND mask = (y_interp >= lower_bound) * (y_interp <= upper_bound) accepted_parameters = x_interp[mask]Based on
I want to x values for which the y values lie within a specific range
it seems you're simply looking for a mask. If you give numpy a relational statement, e.g. my_array > x, it will return a boolean array with True for any indices where this relation is satisfied. And if you pass such a mask to an array for indexing, it'll return the values where this mask is True. For example,
In [2]: a = np.array([1, 3, 2, 5, 2, 9, 4]) In [3]: a > 2 Out[3]: array([False, True, False, True, False, True, True], dtype=bool) In [4]: a[a > 2] Out[4]: array([3, 5, 9, 4])So, to find the x values where f(x) lies within a specific range, find the indices where f(x) is in the desired range and filter your x based on that mask.
# multiplication between 1s and 0s acts like logical AND mask = (y_interp >= lower_bound) * (y_interp <= upper_bound) accepted_parameters = x_interp[mask]更多推荐
发布评论