使用Python在数据的置信区间之间定位区域

时间:2016-04-13 10:32:28

标签: python numpy scipy interpolation confidence-interval

我有一些看起来像这样的数据: enter image description here

生成此图的代码:

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值跨越此置信度内部。但是,复杂的是,由于这是一个置信区间,我需要获得两个方向上穿过绿线的值: enter image description here

即。我需要x值穿过红色箭头的x值,同时排除黑色箭头上的值。我尝试了从插值数据中减去置信区间以及取绝对值的许多变化,但我仍然无法隔离置信区间。有人可以帮帮我吗?提前致谢

1 个答案:

答案 0 :(得分:1)

基于

  

我想要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])

因此,要查找x位于特定范围内的f(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] 那面具。

 public string Comment
  {
   get
   { return _comment; }
   set
   {
    if (value != null)
    {
       // value I am trying to convert is "Testing smileys \\uD83D\\uDE03\\uDE03"
       string convertedCharacter = System.Text.RegularExpressions.Regex.Unescape(value);
       _comment = convertedCharacter;
    }
}
}