numpy - 从 txt 文件计算平均值、标准差的有效方法

标签 numpy python-2.7 python pandas

这是许多 txt 文件之一的副本。

Class 1:
Subject A:
posX posY posZ  x(%)  y(%)
  0   2    0    81    72
  0   2   180   63    38
 -1  -2    0    79    84
 -1  -2   180   85    95
  .   .    .    .     .
Subject B:
posX posY posZ  x(%)   y(%)
  0   2     0    71     73
 -1  -2     0    69     88   
  .   .     .    .      .
Subject C:
posX  posY posZ x(%)   y(%)
  0    2    0    86     71
 -1   -2    0    81     55
  .    .    .     .     .
Class 2:
Subject A:
posX posY posZ  x(%)  y(%)
  0   2    0    81    72
 -1  -2    0    79    84
  .   .    .    .     .
  • 类(class)、科目、行条目的数量都各不相同。
  • Class1-Subject A 始终具有 0 与 180 交替的 posZ 条目
  • 按类(class)和科目计算 x(%)、y(%) 的平均值
  • 按类(class)和科目计算 x(%)、y(%) 的标准差
  • 计算平均值和 std_deviations 时也忽略 180 行的 posZ

我在 excel 中开发了一个笨拙的解决方案(使用宏和 VBA),但我宁愿在 python 中寻求更优化的解决方案。

numpy 非常有用,但 .mean()、.std() 函数仅适用于数组 - 我仍在对其以及 panda 的 groupby 函数进行更多研究。

我希望最终输出如下所示(1. 按类别,2. 按主题)

 1. By Class                 
             X     Y                      
 Average                        
 std_dev     

 2. By Subject  
             X     Y
 Average
 std_dev                   

最佳答案

我认为使用字典(和字典列表)是熟悉在 python 中使用数据的好方法。要像这样格式化数据,您需要读入文本文件并逐行定义变量。

开始:

for line in infile:
    if line.startswith("Class"):
        temp,class_var = line.split(' ')
        class_var = class_var.replace(':','')   
    elif line.startswith("Subject"):
        temp,subject = line.split(' ')
        subject = subject.replace(':','')

这将创建与当前类(class)和当前主题相对应的变量。然后,您想要读入数字变量。读取这些值的一个好方法是通过 try 语句,该语句将尝试将它们转换为整数。

    else:
        line = line.split(" ")
        try:
            keys = ['posX','posY','posZ','x_perc','y_perc']
            values = [int(item) for item in line]
            entry = dict(zip(keys,values))
            entry['class'] = class_var
            entry['subject'] = subject
            outputList.append(entry)
        except ValueError:
            pass

这会将它们放入字典形式,包括之前定义的类和主题变量,并将它们附加到输出列表中。你最终会得到这样的结果:

[{'posX': 0, 'x_perc': 81, 'posZ': 0, 'y_perc': 72, 'posY': 2, 'class': '1', 'subject': 'A'},
{'posX': 0, 'x_perc': 63, 'posZ': 180, 'y_perc': 38, 'posY': 2, 'class': '1', 'subject': 'A'}, ...]

等等

然后,您可以通过对字典列表进行子集化(应用排除 posZ=180 等规则)来平均/取 SD。以下是按类别进行平均:

classes = ['1','2']
print "By Class:"
print "Class","Avg X","Avg Y","X SD","Y SD"
for class_var in classes:   

    x_m = np.mean([item['x_perc'] for item in output if item['class'] == class_var and item['posZ'] != 180])
    y_m = np.mean([item['y_perc'] for item in output if item['class'] == class_var and item['posZ'] != 180])
    x_sd = np.std([item['x_perc'] for item in output if item['class'] == class_var and item['posZ'] != 180])
    y_sd = np.std([item['y_perc'] for item in output if item['class'] == class_var and item['posZ'] != 180])

    print class_var,x_m,y_m,x_sd,y_sd

您必须尝试打印输出才能得到您想要的结果,但这应该可以帮助您入门。

关于numpy - 从 txt 文件计算平均值、标准差的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11350488/

相关文章:

python - 如何使用 Python 下载 pdf 文件?

python - 将字符串列表从数据文件转换为 float

python - 如何找出哪些字符被定义为给定语言环境的字母数字

python - 用零替换足够小的值的快速方法

python - 使用python接口(interface)快速处理opencv图像像素

python - 每次运行在变量之间交替

python - 如果 post_save handler 出现异常,是否可以提交实例?

python - 有没有更快的 bin 文件到 numpy 数组的方法?

python - 将自定义类转换为标准 Python 类型

python - 如何限制正则表达式的 findall() 方法