简体   繁体   English

使用numpy.tile重复2D数组

[英]Using numpy.tile to repeat 2D arrays

I have 3 arrays: 我有3个数组:

e = np.array(range(3,100))
dRdE = np.load('arr_25.npy')

The npy file contains an array with random values but is of the same length as e. npy文件包含具有随机值的数组,但长度与e相同。 I then take the outer product of dRdE with another array. 然后,我将dRdE的外部乘积与另一个数组一起使用。

s = np.array(range(1,100))
dRdE = np.outer(s, dRdE)

And so dRdE is now 2D. 因此, dRdE现在是2D。

I want e to repeat the number of times of each element in dRdE . 我要e重复dRdE中每个元素的dRdE I could use dRdE before when it was 1D and of the same length as e using numpy repeat. 我可以在dRdE为1D之前使用它,并且与numpy repeat的长度相同。 The code I had before was: 我之前的代码是:

earray = np.repeat(e, dRdE)

But since dRdE is no longer 1D I thought I might be able to use np.tile but I'm not sure exactly how to. 但是由于dRdE不再是一维的,所以我认为我可以使用np.tile但是我不确定如何使用。

The context might help, so I have an energy varying between 3keV to 100keV and I have an array containing the number of events ( dRdE ) for each energy between that range. 上下文可能会有所帮助,因此我的能量在3keV100keV之间3keV ,并且我有一个数组,其中包含该范围内每种能量的事件数( dRdE )。 But now I want to introduce a third variable that is just some factor of dRdE between 1 to 100. So for example if: 但是,现在我要介绍第三个变量,它只是dRdE的一部分,介于1到100之间。因此,例如:

dRdE= [1,2,1,2..],[2,4,2,4..],[3,6,3,6]

I want: 我想要:

earray=([3,4,4,5,6,6,...],[3,3,4,4,4,4,5,5,6,6,6,6,...],[3,3,3,4...])

Any help would be appreciated! 任何帮助,将不胜感激!

I asked about variable length sublists because that is a clear indicator that some sort of iteration is required. 我询问了可变长度子列表,因为这清楚地表明需要某种迭代。 There aren't many numpy operations that produce a list or tuple of differing length arrays. 没有多少numpy操作会产生不同长度数组的列表或元组。

Looking into tile a bit more (it's not something I use everyday), I don't think it helps at all. 进一步研究tile (这不是我每天使用的东西),我认为这完全没有帮助。 Its reps are one value per dimension , not per element . 它的reps是每个维度一个值,而不是每个元素一个值。

But lets explore repeat . 但是让探索repeat

In [73]: vals=[[1,2,3,4],[11,12,13,14],[21,22,23,24]]
In [74]: reps=[[1,2,1,2],[2,3,2,4],[3,6,3,6]]
In [75]: valA=np.array(vals)
In [76]: repA=np.array(reps)

An iterative solution: 迭代解决方案:

In [77]: [np.repeat(v,r) for v,r in zip(valA,repA)]
Out[77]: 
[array([1, 2, 2, 3, 4, 4]),
 array([11, 11, 12, 12, 12, 13, 13, 14, 14, 14, 14]),
 array([21, 21, 21, 22, 22, 22, 22, 22, 22, 23, 23, 23, 24, 24, 24, 24, 24,
       24])]

We could also apply repeat once 我们也可以申请repeat一次

In [78]: np.repeat(valA.flat,repA.flat)
Out[78]: 
array([ 1,  2,  2,  3,  4,  4, 11, 11, 12, 12, 12, 13, 13, 14, 14, 14, 14,
       21, 21, 21, 22, 22, 22, 22, 22, 22, 23, 23, 23, 24, 24, 24, 24, 24,
       24])

Now if only we could split that array into 3 arrays, each 6,11,18 elements long. 现在,只要将数组拆分为3个数组即可,每个数组长6,11,18个元素。

In [79]: [len(np.repeat(v,r)) for v,r in zip(valA,repA)]
Out[79]: [6, 11, 18]

In [80]: np.split(np.repeat(valA.flat,repA.flat),[6,17])
Out[80]: 
[array([1, 2, 2, 3, 4, 4]),
 array([11, 11, 12, 12, 12, 13, 13, 14, 14, 14, 14]),
 array([21, 21, 21, 22, 22, 22, 22, 22, 22, 23, 23, 23, 24, 24, 24, 24, 24,
       24])]

I'm sure there's a faster way way of calculating [6,17] list. 我确定有一种更快的方法来计算[6,17]列表。

But when I do a timeit I find that the list comprehension is faster (10x) than the split. 但是当我做一个timeit我发现列表理解比拆分更快(10倍)。 It's even faster that the single repeat. 单次重复甚至更快。 Timings will vary with larger arrays, but getting around the list iteration does not look promising. 时间会随更大的数组而变化,但是绕过列表迭代看起来没有希望。

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

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