为了账号安全,请及时绑定邮箱和手机立即绑定

在多维numpy数组中选择随机条目的有效方法

在多维numpy数组中选择随机条目的有效方法

茅侃侃 2022-11-01 16:57:14
我正在尝试找出一种更有效的方法来做到这一点。这是我的问题: a 有一个(m, n, 2)numpy 数组。为了清楚起见,我将维度称为总体、样本,对于每个样本,第 0 列是频率,第 1 列是幅度。对于每个样本,重复一些频率,但幅度不同。我想要的是一种为每个频率选择一个(并且只有一个)随机幅度并将其放入输出数组的有效方法。举例说明问题。假设第 m 个样本是:1, 22, 32, 43, 5并且输出应该是1, 22, 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_freqoutput_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)。有没有更有效的方法?也许在我迭代这些行时构建字典?
查看完整描述

1 回答

?
ibeautiful

TA贡献1993条经验 获得超5个赞

国际大学联盟:


import numpy


m = 3000


freq_compare = np.random.choice(np.arange(0,20), 8, replace=False)

output_random = np.zeros((m, len(freq_compare), 2))

a = np.random.randint(0, 20, (m, 400, 2))


i = 0

for r in a: 


    freqs = np.unique(r[:,0]) 

    d = [[f, np.random.choice(r[r[:,0]==f, 1])] if f in freqs else [f, 0] for f in freq_compare]


    output_random[i] = d

    i+=1

在我的本地机器中使用%timeit会导致:


710 ms ± 97.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)


查看完整回答
反对 回复 2022-11-01
  • 1 回答
  • 0 关注
  • 93 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信