当我们有几个组时,基于控件的样本归一化

阿达兰1

假设我们有以下 DataFrame:

data = {'Compounds': ['Drug_A', 'Drug_A', 'Drug_A', 'Drug_A', 'Drug_A', 'Drug_A', 'Drug_B', 'Drug_B',
                   'Drug_B','Drug_B','Drug_B','Drug_B','Drug_B','Drug_B','Drug_B','Drug_B','Drug_B','Drug_B',
                   'Drug_C', 'Drug_C','Drug_C','Drug_C','Drug_C','Drug_C','Drug_C','Drug_C','Drug_C','Drug_C', np.nan, 
                   np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan,
                   np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], 
        'values': [24, 20, 48, 17, 20, 8, 22, 16, 46, 44, 12, 38, 26, 16, 19, 23, 9, 39, 19, 24, 43, 6, 24, 46, 26, 15, 8, 
                  22, 22, 32, 23, 41, 8, 46, 29, 34, 34, 39, 32, 22, 28, 34, 29, 19, 44, 22, 17, 41, 19, 39, 27, 46, 37, 26],
      'identifier': ['Sample', 'Sample','Sample','Sample','Sample','Sample','Sample','Sample','Sample','Sample',
                    'Sample','Sample','Sample','Sample','Sample','Sample','Sample','Sample','Sample','Sample',
                    'Sample','Sample','Sample','Sample','Sample','Sample','Sample','Sample', 'Control', 'Control',
                    'Control','Control','Control','Control','Control','Control','Control','Control','Control',
                    'Control','Control','Control','Control','Control','Control','Control','Control','Control',
                    'Control','Control','Control','Control','Control','Control',], 
'Experiment': ['P1', 'P1', 'P2', 
                     'P2', 'P3', 'P3', 'P1', 'P1', 'P1', 'P2', 'P2', 'P2', 'P3', 'P3', 'P1', 'P1', 'P1', 'P2', 'P2', 
                    'P2', 'P2', 'P2', 'P3', 'P3', 'P1','P1', 'P1', 'P1', 'P1', 'P1', 'P1', 'P1', 
                    'P2', 'P2','P2','P2','P2','P2','P2','P2','P2','P2','P2','P2','P3','P3','P3','P3','P3','P3', 'P1', 'P2',
                                                                                           'P3','P1' ]}
df = pd.DataFrame(data)

在标识符列中,我们有 Sample 和 Control 值。我们首先要:计算来自不同实验(即 P1、P2、P3)的所有对照的“值”列的平均值:

df_control = df.loc[df['identifier'] == 'Control']
z = df_control['values'].mean()

如果我想在一行中编写它,上面脚本的紧凑形式是什么?我可以使用列表全面吗?

接下来,出于标准化的目的,我们希望分别将 z 除以每个实验 P1、P2、P3 中对照的平均“值”,以获得每个这些实验的 normalization_factor。

最后,将每个特定实验的归一化因子乘以属于该实验的样本值。

最简单、最直接的方法是什么?感谢您的帮助!

存入

这是你要找的吗?

df.groupby(by=['identifier']).mean()
Out: 
               values
identifier           
Control     30.384615
Sample      24.285714

进而:

df.groupby(by=['identifier', 'Experiment']).mean()
Out: 
                          values
identifier Experiment           
Control    P1          28.500000
           P2          30.769231
           P3          31.285714
Sample     P1          20.833333
           P2          29.000000
           P3          23.333333

第二个具有以下MultiIndex可用于访问数据的内容:

MultiIndex([('Control', 'P1'),
            ('Control', 'P2'),
            ('Control', 'P3'),
            ( 'Sample', 'P1'),
            ( 'Sample', 'P2'),
            ( 'Sample', 'P3')],
           names=['identifier', 'Experiment'])

你现在可以以此为基础:

all_mean = df.groupby(by=['identifier']).mean()
spec_mean = df.groupby(by=['identifier', 'Experiment']).mean()
result = all_mean/spec_mean

Out
                         values
identifier Experiment          
Control    P1          1.066127
           P2          0.987500
           P3          0.971198
Sample     P1          1.165714
           P2          0.837438
           P3          1.040816

现在将数据放入某种平面结构中(?OP对此没有明确说明):

normalization_factors = {idx[1]: result.loc[idx].values[0] for idx in result.index if idx[0] == 'Control'}
# {'P1': 1.0661268556005397, 'P2': 0.9874999999999999, 'P3': 0.9711977520196698}
sample_values = {idx[1]: result.loc[idx].values[0] * normalization_factors[idx[1]] for idx in result.index if idx[0] == 'Sample'}
# {'P1': 1.2427993059572005, 'P2': 0.8269704433497537, 'P3': 1.0108384765919014}

映射sample_datadf作为:

df["calculated_col_with_the_name_you_prefer"] = df["Experiment"].map(sample_values)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当我们有新样本时如何更新 hmmlearn 学习对象?

当我们有一个红色区域时,为什么我们需要堆栈分配?

我们应该在有角度的应用程序中对状态进行归一化吗?

当我们只有一个片段时处理 `BackPressed`

当我们有一个列表时,SQL Server 中的 Json 值

当我们在结构内部有一个指针时,container_of宏

当我们导入一个函数时,是否有可能避免多个 ../ ?

当我们有多个查询时的 Kafka Connect

Python:当我们不需要重复的随机样本时如何使用随机样本

当我们有ViewModels时,我们还需要onSaveInstanceState()吗?

归一化单位,为什么我们在数值积分中使用它们?

我们应该对整个数据集应用归一化还是只对 X

当我们覆盖一个方法时,我们应该使用方法签名中的所有参数吗?

SQL server,当我们在sql server中使用cast和float时,我们有一列数据decimal(38,35)被截断

当我们有一个模板类时,如何使用继承有模板指针

为什么我们将归一化的分数乘以0.5以获得IEEE 754表示的有效位数?

当我们使用 group by 时是否可以访问组中的行

当我们具有纯虚函数租用时,如何初始化一个对象?

当我们有一个单独的View控制器时,ItemController不起作用

当我们声明一个类而不扩展任何类时,它有父类吗?

当我们只有一个通用特征时,Rust 处理常量数学的方法是什么

当我们有上下文值时, <ratio> 有什么用?

当我们真正拥有move构造函数时,是否具有(N)RVO?

当我们释放一个按钮时,如何触发一个事件?

当我有一个私有id字段时,为什么Hibernate要求我们实现equals / hashcode方法?

当我们为一个类的对象分配一个整数值时,为什么要调用参数化构造函数?

当我们有 sqrt 时如何避免数学域错误

当我们有Class类型的对象时,如何在Java中创建实例

当我们点击 iPhoneX 中的文本字段时,键盘没有打开