Home > Mobile >  Can numpy create n arrays by summing n elements in 1d array to each element of another 1d array?
Can numpy create n arrays by summing n elements in 1d array to each element of another 1d array?

Time:11-08

Can't find a question/ answer that fits this exact criteria but if this is a duplicate question then I will delete it. Is there a numpy equivalent to the following code or is it better to just keep my code as is/ use xrange?

x = [i for i in range (50)]
y = [i for i in range (120)]

for i in x:
   foo = [i z for z in y]
   print(foo)

This is a toy example but the the data set I am working with can range from something like this to 1000x the size in the example; I have tried np.idter but don't see much of a performance increase and as I gathered from bmu's answer here using range to iterate over a numpy array is the worst. But I cannot see how ufunc and indexing can reproduce the same results as above which is my desired result.

CodePudding user response:

This is a classic application of broadcasting:

import numpy as np
x = np.arange(0,5).reshape(5,1)
y = np.arange(0,12).reshape(1,12)

foos = x   y
print(foos)

[[ 0  1  2  3  4  5  6  7  8  9 10 11]
 [ 1  2  3  4  5  6  7  8  9 10 11 12]
 [ 2  3  4  5  6  7  8  9 10 11 12 13]
 [ 3  4  5  6  7  8  9 10 11 12 13 14]
 [ 4  5  6  7  8  9 10 11 12 13 14 15]]

Obviously a binary operation like addition can't emit multiple arrays, but it can emit a higher dimensional array containing all the output arrays as rows or columns of that higher dimensional array.

As pointed out in comments, note there is also a generalization of the outer product which is functionally identical to the broadcasting approach I have shown.

  • Related