获得 seaborn.catplot 中计算的平均值的标准误差
Obtain standard error of mean as computed in seaborn.catplot
我正在使用 seaborn.catplot
和 kind='point'
来绘制我的数据。我想使用 与 seaborn 相同的方法计算每个色调变量和每个类别的均值标准误差 (SEM),以确保我的计算值与绘制的值完全匹配错误栏。计算 SEM 和 95% 置信区间 (CI) 的默认解决方案包含自举算法,其中均值自举 1000 次以计算 SEM/CIs。在 earlier post 中,我看到了一个可能为此提供函数的方法(使用像 seaborn.utils.ci()
和 seaborn.algorithms.bootstrap()
这样的 seaborn 源代码函数),但我不确定如何实现它。由于自举使用随机抽样,因此还需要确保为绘图和获取 SEM 生成相同的 1000 个均值数组。
这是一个代码示例:
import numpy as np
import pandas as pd
import seaborn as sns
# simulate data
rng = np.random.RandomState(42)
measure_names = np.tile(np.repeat(['Train BAC','Test BAC'],10),2)
model_numbers = np.repeat([0,1],20)
measure_values = np.concatenate((rng.uniform(low=0.6,high=1,size=20),
rng.uniform(low=0.5,high=0.8,size=20)
))
folds=np.tile([1,2,3,4,5,6,7,8,9,10],4)
plot_df = pd.DataFrame({'model_number':model_numbers,
'measure_name':measure_names,
'measure_value':measure_values,
'outer_fold':folds})
# plot data as pointplot
g = sns.catplot(x='model_number',
y='measure_value',
hue='measure_name',
kind='point',
seed=rng,
data=plot_df)
产生:
我想获得两种模型的所有训练和测试分数的 SEM。即:
# obtain SEM for each score in each model using the same method as in sns.catplot
model_0_train_bac = plot_df.loc[((plot_df['model_number'] == 0) & (plot_df['measure_name'] == 'Train BAC')),'measure_value']
model_0_test_bac = plot_df.loc[((plot_df['model_number'] == 0) & (plot_df['measure_name'] == 'Test BAC')),'measure_value']
model_1_train_bac = plot_df.loc[((plot_df['model_number'] == 1) & (plot_df['measure_name'] == 'Train BAC')),'measure_value']
model_1_test_bac = plot_df.loc[((plot_df['model_number'] == 1) & (plot_df['measure_name'] == 'Test BAC')),'measure_value']
我不确定我是否满足了您采集的样本完全相同的要求。根据定义,bootstrapping 是通过随机抽样来工作的,因此一个 运行 与下一个 运行 会有一些差异(除非我弄错了)。
您可以像 seaborn 那样计算 CI:
# simulate data
rng = np.random.RandomState(42)
measure_names = np.tile(np.repeat(['Train BAC','Test BAC'],10),2)
model_numbers = np.repeat([0,1],20)
measure_values = np.concatenate((rng.uniform(low=0.6,high=1,size=20),
rng.uniform(low=0.5,high=0.8,size=20)
))
folds=np.tile([1,2,3,4,5,6,7,8,9,10],4)
plot_df = pd.DataFrame({'model_number':model_numbers,
'measure_name':measure_names,
'measure_value':measure_values,
'outer_fold':folds})
x_col = 'model_number'
y_col = 'measure_value'
hue_col = 'measure_name'
ci = 95
est = np.mean
n_boot = 1000
for gr,temp_df in plot_df.groupby([hue_col,x_col]):
print(gr,est(temp_df[y_col]), sns.utils.ci(sns.algorithms.bootstrap(temp_df[y_col], func=est,
n_boot=n_boot,
units=None,
seed=rng)))
输出:
('Test BAC', 0) 0.7581071363371585 [0.69217109 0.8316217 ]
('Test BAC', 1) 0.6527812067134964 [0.59523784 0.71539669]
('Train BAC', 0) 0.8080546943810699 [0.73214414 0.88102816]
('Train BAC', 1) 0.6201161718490218 [0.57978654 0.66241543]
请注意,如果您 运行 第二次循环,您会得到 CI 相似但不完全相同的结果。
如果你真的想获得 seaborn 在绘图中使用的确切值(再次注意,如果你第二次绘制相同的数据,这些值会略有不同),那么你可以提取值直接来自用于绘制误差线的 Line2D 艺术家:
g = sns.catplot(x=x_col,
y=y_col,
hue=hue_col,
kind='point',
ci=ci,
estimator=est,
n_boot=n_boot,
seed=rng,
data=plot_df)
for l in g.ax.lines:
print(l.get_data())
输出:
(array([0., 1.]), array([0.80805469, 0.62011617]))
(array([0., 0.]), array([0.73203808, 0.88129836])) # <<<<
(array([1., 1.]), array([0.57828366, 0.66300033])) # <<<<
(array([0., 1.]), array([0.75810714, 0.65278121]))
(array([0., 0.]), array([0.69124145, 0.83297914])) # <<<<
(array([1., 1.]), array([0.59113739, 0.71572469])) # <<<<
我正在使用 seaborn.catplot
和 kind='point'
来绘制我的数据。我想使用 与 seaborn 相同的方法计算每个色调变量和每个类别的均值标准误差 (SEM),以确保我的计算值与绘制的值完全匹配错误栏。计算 SEM 和 95% 置信区间 (CI) 的默认解决方案包含自举算法,其中均值自举 1000 次以计算 SEM/CIs。在 earlier post 中,我看到了一个可能为此提供函数的方法(使用像 seaborn.utils.ci()
和 seaborn.algorithms.bootstrap()
这样的 seaborn 源代码函数),但我不确定如何实现它。由于自举使用随机抽样,因此还需要确保为绘图和获取 SEM 生成相同的 1000 个均值数组。
这是一个代码示例:
import numpy as np
import pandas as pd
import seaborn as sns
# simulate data
rng = np.random.RandomState(42)
measure_names = np.tile(np.repeat(['Train BAC','Test BAC'],10),2)
model_numbers = np.repeat([0,1],20)
measure_values = np.concatenate((rng.uniform(low=0.6,high=1,size=20),
rng.uniform(low=0.5,high=0.8,size=20)
))
folds=np.tile([1,2,3,4,5,6,7,8,9,10],4)
plot_df = pd.DataFrame({'model_number':model_numbers,
'measure_name':measure_names,
'measure_value':measure_values,
'outer_fold':folds})
# plot data as pointplot
g = sns.catplot(x='model_number',
y='measure_value',
hue='measure_name',
kind='point',
seed=rng,
data=plot_df)
产生:
我想获得两种模型的所有训练和测试分数的 SEM。即:
# obtain SEM for each score in each model using the same method as in sns.catplot
model_0_train_bac = plot_df.loc[((plot_df['model_number'] == 0) & (plot_df['measure_name'] == 'Train BAC')),'measure_value']
model_0_test_bac = plot_df.loc[((plot_df['model_number'] == 0) & (plot_df['measure_name'] == 'Test BAC')),'measure_value']
model_1_train_bac = plot_df.loc[((plot_df['model_number'] == 1) & (plot_df['measure_name'] == 'Train BAC')),'measure_value']
model_1_test_bac = plot_df.loc[((plot_df['model_number'] == 1) & (plot_df['measure_name'] == 'Test BAC')),'measure_value']
我不确定我是否满足了您采集的样本完全相同的要求。根据定义,bootstrapping 是通过随机抽样来工作的,因此一个 运行 与下一个 运行 会有一些差异(除非我弄错了)。
您可以像 seaborn 那样计算 CI:
# simulate data
rng = np.random.RandomState(42)
measure_names = np.tile(np.repeat(['Train BAC','Test BAC'],10),2)
model_numbers = np.repeat([0,1],20)
measure_values = np.concatenate((rng.uniform(low=0.6,high=1,size=20),
rng.uniform(low=0.5,high=0.8,size=20)
))
folds=np.tile([1,2,3,4,5,6,7,8,9,10],4)
plot_df = pd.DataFrame({'model_number':model_numbers,
'measure_name':measure_names,
'measure_value':measure_values,
'outer_fold':folds})
x_col = 'model_number'
y_col = 'measure_value'
hue_col = 'measure_name'
ci = 95
est = np.mean
n_boot = 1000
for gr,temp_df in plot_df.groupby([hue_col,x_col]):
print(gr,est(temp_df[y_col]), sns.utils.ci(sns.algorithms.bootstrap(temp_df[y_col], func=est,
n_boot=n_boot,
units=None,
seed=rng)))
输出:
('Test BAC', 0) 0.7581071363371585 [0.69217109 0.8316217 ]
('Test BAC', 1) 0.6527812067134964 [0.59523784 0.71539669]
('Train BAC', 0) 0.8080546943810699 [0.73214414 0.88102816]
('Train BAC', 1) 0.6201161718490218 [0.57978654 0.66241543]
请注意,如果您 运行 第二次循环,您会得到 CI 相似但不完全相同的结果。
如果你真的想获得 seaborn 在绘图中使用的确切值(再次注意,如果你第二次绘制相同的数据,这些值会略有不同),那么你可以提取值直接来自用于绘制误差线的 Line2D 艺术家:
g = sns.catplot(x=x_col,
y=y_col,
hue=hue_col,
kind='point',
ci=ci,
estimator=est,
n_boot=n_boot,
seed=rng,
data=plot_df)
for l in g.ax.lines:
print(l.get_data())
输出:
(array([0., 1.]), array([0.80805469, 0.62011617]))
(array([0., 0.]), array([0.73203808, 0.88129836])) # <<<<
(array([1., 1.]), array([0.57828366, 0.66300033])) # <<<<
(array([0., 1.]), array([0.75810714, 0.65278121]))
(array([0., 0.]), array([0.69124145, 0.83297914])) # <<<<
(array([1., 1.]), array([0.59113739, 0.71572469])) # <<<<