我试图根据这条线的第一个元素对数据行进行一种绑定。
我的数据是这样的:
[[Temperature, value0, value1, ... value249]
[Temperature, ...
]所以说:每条线的第一个元素是一个温度值,其余的是一个信号的时间轨迹。
我想做一个这样的数组:
[Temperature-bin,[[values]
[values]
... ]]
Next Temp.-bin, [[values]
[values]
... ]]
...
]其中,原始数据数组中的线条应该在相应的温度箱的子数组中排序。
data= np.array([values]) # shape is [temp+250 timesteps,400K]
temp=data[0]
start=23000
end=380000
tempmin=np.min(temp[start:end])
tempmax=np.max(temp[start:end])
binsize=1
bincenters=np.arange(np.round(tempmin),np.round(tempmax)+1,binsize)
binneddata=np.empty([len(bincenters),2])
for i in np.arange(len(temp)):
binneddata[i]=[bincenters[i],np.array([])]我希望像上面描述的那样得到一个结果数组,其中每一行都由bin (bincentersi)的平均温度和一个时间跟踪数组组成。Python给了我一个关于“用序列设置数组元素”的错误。我可以在之前的另一个脚本中创建这种数组,由不同的数据类型组成,但在这里我必须具体地定义它,这在本例中是不可能的,因为我正在处理几行100 K的数据大小的文件。同时,我想使用尽可能多的内置函数和最小的循环,因为我的计算机已经花了一些时间处理这个大小的文件。
谢谢你的意见,
鳞翅目
发布于 2019-10-07 14:53:17
第一:感谢克温克姆暗示使用熊猫的数据。我找到了一个使用这个特性的解决方案。
现在就这样做了:
tempmin=np.min(temp[start:end])
tempmax=np.max(temp[start:end])
binsize=1
bincenters=np.array(np.arange(np.round(tempmin),np.round(tempmax)+1,binsize))
lowerbinedges=np.array(bincenters-binsize/2)
higherbinedges=np.array(bincenters+binsize/2)
allbinedges=np.append(lowerbinedges,higherbinedges[-1])
temp_pd=pd.Series(temp[start:end])
traces=pd.Series(list(data[start:end,0:250]))
tempbins=pd.cut(temp_pd,allbinedges,labels=bincenters)
df=pd.concat([temp_pd,tempbins,traces], keys=['Temp','Bincenter','Traces'], axis=1)通过定义回收箱(在本例中是大小相同)。变量"tempbins“与temp (”原始“温度)具有相同的形状,并将每一行数据分配给特定的bin。
因此,实际的分析非常简短。首先:
rf=pd.DataFrame({'Bincenter': bincenters})结果框架("rf")以bincenter开始(稍后作为绘图中的x轴),并为所需的结果添加列。
使用
df[df.Bincenter==xyz] 我只能从df中选择那些数据行,这是我想要在所选的bin中拥有的。
在我的例子中,我对实际的时间跟踪不感兴趣,而是对和或平均值感兴趣,所以我使用lambda函数,它遍历rf的行并搜索df中的每一行,在那里具有相同的值。
rf['Binsize']=rf.apply(lambda row: len(df.Traces[df.Bincenter==row.Bincenter]), axis=1)
rf['Trace_sum']=rf.apply(lambda row: sum(df.Traces[df.Bincenter==row.Bincenter]), axis=1)对于这些,结果帧rf中将添加另一列,以求跟踪和bin中的行数。
我在rf.Trace_sum中做了一些贴合的痕迹,而我在熊猫中没有这样做。
尽管如此,数据文件在这里还是非常有用的。我用odr来装成这样
for i in binnumber:
fitdata=odr.Data(time[fitstart:],rf.Trace_sum.values[i][fitstart:])
... some more fit stuff here...并把适者生存的结果
lifetimefits=pd.DataFrame({'lifetime': fitresult[:,1], 'sd_lifetime':fitresult[:,4]})并最终将它们添加到结果框架中。
rf=pd.concat([rf,lifetimefits],axis=1)
rf[['Bincenter','Binsize','lifetime','sd_lifetime']].to_csv('results.csv', header=True, index=False)这使得输出类似于
Out[78]:
Bincenter Binsize ... lifetime sd_lifetime
0 139.0 4102 ... 38.492028 2.803211
1 140.0 4252 ... 33.659729 2.534872
2 141.0 3785 ... 31.220312 2.252104
3 142.0 3823 ... 29.391562 1.783890
4 143.0 3808 ... 40.422578 2.849545我希望,这一解释可以帮助其他人不要浪费时间,尝试这一点。再次感谢克温克姆对熊猫DataFrame的使用提出了非常有益的建议。
最好的,丽来克
https://stackoverflow.com/questions/58204651
复制相似问题