使用 Python 定位数据置信区间之间的区域
Locating region between a confidence interval in data using Python
我有一些数据如下所示:
生成此图的代码:
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)
绿线表示置信区间。我需要以编程方式定位 y 值与内部置信度交叉的 x 值。但是,复杂的是,由于这是一个置信区间,我需要获得在两个方向上都穿过绿线的值:
即我需要 y 数据穿过红色箭头的 x 值,同时排除那些位于黑色箭头处的值。我尝试了从插值数据中减去置信区间以及取绝对值的多种变体,但我仍然无法分离出置信区间。
基于
I want to x values for which the y values lie within a specific range
看来你只是在找面具。如果你给 numpy 一个关系语句,例如my_array > x
,对于满足此关系的任何索引,它将 return 一个带有 True
的布尔数组。如果您将这样的掩码传递给数组以进行索引,它将 return 此掩码为 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]
我有一些数据如下所示:
生成此图的代码:
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)
绿线表示置信区间。我需要以编程方式定位 y 值与内部置信度交叉的 x 值。但是,复杂的是,由于这是一个置信区间,我需要获得在两个方向上都穿过绿线的值:
即我需要 y 数据穿过红色箭头的 x 值,同时排除那些位于黑色箭头处的值。我尝试了从插值数据中减去置信区间以及取绝对值的多种变体,但我仍然无法分离出置信区间。
基于
I want to x values for which the y values lie within a specific range
看来你只是在找面具。如果你给 numpy 一个关系语句,例如my_array > x
,对于满足此关系的任何索引,它将 return 一个带有 True
的布尔数组。如果您将这样的掩码传递给数组以进行索引,它将 return 此掩码为 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]