繁体   English   中英

为不同的样本定义一个function 统计在python

[英]Defining a function for different samples with statistics in python

我对 Python 比较陌生,正在寻找一些见解。

我正在尝试为给定数量的样本计算一些统计数据(均值、方差和标准误差)。 在列表samples ,我有 6 个实验,有 N 个样本。 我正在使用numpy库进行统计,所以我知道np.mean() np.std() np.var()

我可以编写类似的代码

print("For n =", n_rep, np.mean(res), np.std(res),np.var(res))

其中 n_rep 是 N 个样本的数量,res = 代码前面定义的一些 function

但是为每个样本编写代码很乏味,而且我不知道如何为每个实验保存,所以我总是只得到一组实验统计数据

我想编写这样的代码:

samples = [5, 20, 50, 100, 200, 1000] for i in samples:

output 应该是这样的print("For n =", n_rep, np.mean(res), np.std(res),np.var(res))但是我得到了 6 个实验的列表

提前致谢!

您可以创建一个空列表,然后 append 结果给它,而不是打印它。

import numpy as np 

samples = [5, 20, 50, 100, 200, 1000]
empty_list = []

for i in samples:
    sample_list = [i, np.mean(i), np.std(i),np.var(i)]
    empty_list.append(sample_list)
    print("For n =",sample_list)

所以我说的function就是下一个。 这个想法是模拟一个充满不同大小样本的专辑,即 n_rep。

def repetitions(n_rep, stickers_total, stickers_pack):
    stickers_album = []
    while len(stickers_album) != n_rep:
        n_packs = how_many_packs(stickers_total, stickers_pack)
        stickers_album.append(n_packs)
    return stickers_album

n_rep 是样本数,所以对于第一个实验,我有 5 个测量值,第二个实验有 10 个,依此类推

当我运行你的代码时,我的终端看起来像这样:

[[5, 866.4, 80.99283918964687, 36.22109882375187], 
[20, 866.4, 80.99283918964687, 36.22109882375187], 
[50, 866.4, 80.99283918964687, 36.22109882375187], 
[100, 866.4, 80.99283918964687, 36.22109882375187], 
[200, 866.4, 80.99283918964687, 36.22109882375187], 
[1000, 866.4, 80.99283918964687, 36.22109882375187]]

我想因为我已经定义了 n_rep,所以我得到了相同的统计数据。 鉴于上述 function,知道如何绕过它吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM