长(缠绕)版本:
我正在用Python收集研究数据。我最初的分析是难看的(但功能性的)代码,它给了我一些基本信息,并将我的原始数据转换成适合使用SPSS进行繁重统计分析的格式。然而,每次修改实验,我都要深入分析代码。在
对于一个典型的实验,我将有30个文件,每个文件针对一个唯一的用户。每个实验的场计数是固定的(但是可以从一个到另一个10-20个)。文件通常有700-1000个记录,带有一个标题行。记录格式是制表符分隔的(参见示例,它是4个整数、3个字符串和10个浮点数)。在
我需要把我的名单分类。在一个1000行的文件中,我可以有4-256个类别。我不想预先确定每个文件有多少个类别,而是使用下面的代码对它们进行计数。每行开头的整数表示行中浮点值对应的类别。整数组合可以被字符串值修改以产生完全不同的结果,多个组合有时可以集中在一起。在
一旦他们分类,数字运算就开始了。我得到统计信息(每个文件的每个类别的平均值、标准差等)。在
要点:
我需要像下面的示例一样将数据解析为类别。类别是每个记录中非浮点数的组合。我还试图想出一种动态(图形)的方法来将列组合与类别相关联。将对此进行新的发布。
我正在寻找如何做到这两方面的建议。在# data is a list of tab separated records
# fields is a list of my field names
# get a list of fieldtypes via gettype on our first row
# gettype is a function to get type from string without changing data
fieldtype = [gettype(n) for n in data[1].split('\t')]
# get the indexes for fields that aren't floats
mask = [i for i, field in enumerate(fieldtype) if field!="float"]
# for each row of data[skipping first and last empty lists] we split(on tabs)
# and take the ith element of that split where i is taken from the list mask
# which tells us which fields are not floats
records = [[row.split('\t')[i] for i in mask] for row in data[1:-1]]
# we now get a unique set of combos
# since set doesn't happily take a list of lists, we join each row of values
# together in a comma seperated string. So we end up with a list of strings.
uniquerecs = set([",".join(row) for row in records])
print len(uniquerecs)
quit()
def gettype(s):
try:
int(s)
return "int"
except ValueError:
pass
try:
float(s)
return "float"
except ValueError:
return "string"
样本数据:
^{pr2}$