我正在尝试找出一种更有效的方法来做到这一点。这是我的问题: a 有一个(m, n, 2)numpy 数组。为了清楚起见,我将维度称为总体、样本,对于每个样本,第 0 列是频率,第 1 列是幅度。对于每个样本,重复一些频率,但幅度不同。我想要的是一种为每个频率选择一个(并且只有一个)随机幅度并将其放入输出数组的有效方法。举例说明问题。假设第 m 个样本是:
1, 2
2, 3
2, 4
3, 5
并且输出应该是
1, 2
2, 4 (random choice between 3 and 4)
3, 5
此外,输出数组中的频率必须是另一个名为 的列表中的频率freq_compare。我有一个工作代码,但需要一段时间。如果这有帮助,则会对频率进行排序,但我事先不知道会有多少重复项(如果有的话),也不知道哪些频率会被重复。
这是我到目前为止所拥有的:
def make_dict(sample):
"""Produce a dictionary with the frequencies as keys and amplitudes as values."""
per_freq = dict()
freqs = list(set(sample[:,0]))# get list of all frequencies
for f in freqs:
per_freq[f] = [line[1] for line in sample if line[0] == f]
return per_freq
output_random = np.zeros((m, len(freq_compare), 2))
for i in range(m):
d = make_dict(all_data[i]) #original array
keys = list(d.keys())
for j in range(len(freq_compare)):
if freq_compare[j] in keys:
amp = np.random.choice(d[freq_compare[j]])
output_random[i,j,:] = (freq_compare[j], amp)
else:
output_random[i,j,:] = (freq_compare[j], 0.0)
这样做 10 次大约需要 15 分钟,以获得一系列形状(3000, 400, 2)。有没有更有效的方法?也许在我迭代这些行时构建字典?
ibeautiful
相关分类