如何构造多个python数组进行排序 [英] How to structure multiple python arrays for sorting

查看:215
本文介绍了如何构造多个python数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行傅立叶分析,输出5个数据字段,我将每个数据字段收集到一维numpy数组中:频率仓编号,幅度,波长,归一化幅度,%功率.

A fourier analysis I'm doing outputs 5 data fields, each of which I've collected into 1-d numpy arrays: freq bin #, amplitude, wavelength, normalized amplitude, %power.

如何最好地构造数据,以便我可以按降幅排序?

当仅使用一个数据字段进行测试时,我可以使用以下命令:

When testing with just one data field, I was able to use a dict as follows:

fourier_tuples = zip(range(len(fourier)), fourier)
fourier_map = dict(fourier_tuples)
import operator
fourier_sorted = sorted(fourier_map.items(), key=operator.itemgetter(1))
fourier_sorted = np.argsort(-fourier)[:3]

我的意图是将其他数组添加到第1行,但这不起作用,因为字典仅接受2个条件. (这就是这篇文章无法解决我的问题的原因.)

My intent was to add the other arrays to line 1, but this doesn't work since dicts only accept 2 terms. (That's why this post doesn't solve my issue.)

退一步,这是一种合理的方法,还是有更好的方法将&排序单独的数组?最终,我想从前3个频率和相关的其他数据中获取数据值,并将它们写入输出数据文件.

Stepping back, is this a reasonable approach, or are there better ways to combine & sort separate arrays? Ultimately, I want to take the data values from the top 3 freqs and associated other data, and write them to an output data file.

这是我的数据的一部分:

Here's a snippet of my data:

fourier = np.array([1.77635684e-14, 4.49872050e+01, 1.05094837e+01, 8.24322470e+00, 2.36715913e+01])
freqs = np.array([0.        ,  0.00246951,  0.00493902,  0.00740854,  0.00987805])
wavelengths = np.array([inf, 404.93827165, 202.46913583, 134.97942388, 101.23456791])
amps = np.array([4.33257766e-16, 1.09724890e+00, 2.56328871e-01, 2.01054261e-01, 5.77355886e-01])
powers% = np.array([4.8508237956526163e-32, 0.31112370227749603, 0.016979224022185751, 0.010445983875848858, 0.086141014686372669])

最后4个数组是与傅立叶"相对应的其他字段. (实际数组长度为42,但为简单起见,将其缩减为5.)

The last 4 arrays are other fields corresponding to 'fourier'. (Actual array lengths are 42, but pared down to 5 for simplicity.)

推荐答案

您似乎正在使用numpy,因此这是执行此操作的numpy方法.您的帖子中具有正确的功能np.argsort,但您似乎并未正确使用它:

You appear to be using numpy, so here is the numpy way of doing this. You have the right function np.argsort in your post, but you don't seem to use it correctly:

order = np.argsort(amplitudes)

这与您的字典技巧类似,只是它计算与过程相比的逆混洗.顺便提一句.为什么要浏览字典而不是简单地查看元组列表?

This is similar to your dictionary trick only it computes the inverse shuffling compared to your procedure. Btw. why go through a dictionary and not simply a list of tuples?

order的内容现在是amplitudes的索引.order的第一个单元格包含amplitudes的最小元素的位置,第二个单元格包含下一个元素的位置,等等.

The contents of order are now indices into amplitudes the first cell of order contains the position of the smallest element of amplitudes, the second cell contains the position of the next etc. Therefore

top5 = order[:-6:-1]

假设您的数据是1 numpy array,则可以使用高级索引

Provided your data are 1d numpy arrays you can use top5 to extract the elements corresponding to the top 5 ampltiudes by using advanced indexing

freq_bin[top5]
amplitudes[top5]
wavelength[top5]

如果需要,可以将它们分组到列中,然后将top5应用于生成的n-by-5数组:

If you want you can group them together in columns and apply top5 to the resulting n-by-5 array:

np.c_[freq_bin, amplitudes, wavelength, ...][top5, :]

这篇关于如何构造多个python数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆