简体   繁体   English

numpy,h5py:如何从使用h5py保存的较大列中按其列之一排序的数组?

[英]numpy, h5py: How do I make an array sorted by one of its columns from a bigger one saved with h5py?

I'd like to give you some background info so you understand my problem better. 我想给您一些背景信息,以便您更好地理解我的问题。

From the results of an experiment I fill a big hdf5 table with lots of columns using h5py. 从实验结果中,我使用h5py填充了一个巨大的hdf5表,其中包含许多列。 Once all my measurements are done, I need to plot and fit some results. 完成所有测量后,我需要绘制并拟合一些结果。 This is already working but when I get to the point when I want to plot the fitting function, as my data is not sorted by the column with the 'x' axis data, instead of a single line I get an ugly back-and-forth line (I'd show it to you but I don't have enough reputation yet). 这已经可以工作了,但是当我想绘制拟合函数时,由于我的数据不是按带有“ x”轴数据的列排序的,而不是单行,所以我得到了一个丑陋的来回说明。第四行(我会告诉您,但我的声誉还不够高)。

So my first thought was to sort the arrays before plotting and fitting. 所以我首先想到的是在绘制和拟合之前对数组进行排序。 I tried following several guides I found here but my joined array had the wrong shape and that was the time I though there might be a better way of doing it. 我尝试遵循在这里找到的几条指南,但是我加入的数组的形状不正确,那时候我可以做一个更好的方法了。

So my question is, What's the best way of getting an array sorted by one of its columns from a bigger array saved in an hdf5 file using h5py? 所以我的问题是, 使用h5py从保存在hdf5文件中的更大数组中按其列之一对数组进行排序的最佳方法什么?

This is how I'm currently doing it: 这是我目前正在做的事情:

Let's say I already extracted the columns from the hdf5 file (even though maybe this could be improved!), now I'm making them up. 假设我已经从hdf5文件中提取了列(即使可能会进行改进!),现在我正在对它们进行组合。

x_d = array([5, 2, 10, 4])
y_d = array([0.2, 1.0, 4.1, 0.1])

wtype = np.dtype([('x', x_d.dtype), ('y', y_d.dtype)])
w = np.empty(len(x_d), dtype=wtype)
w['x'] = x_d
w['y'] = y_d

w.sort(order='x')

Something along these lines should work: 遵循以下原则应该可以:

f = h5py.File('myfile.hdf5','r')
x_d = f['x_axis'][:]
y_d = f['values'][:]
sorted_y = y_d[numpy.argsort(x_d)]

or if you want to have the reverse order: 或者,如果您想要相反的顺序:

sorted_y = y_d[numpy.argsort(x_d)[::-1]]

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

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