Questions tagged [array-broadcasting]

Broadcasting (or singleton expansion) applies a function element-wise across one or more multidimensional arrays, matching shapes of the arguments by repeating missing or singleton dimensions. Be sure to also tag the programming language; many languages with strong array support have implicit or explicit broadcasting behaviors, sometimes with idiosyncratic rules.

Many languages and frameworks have implementations of broadcasting (also known as singleton expansion), including but not limited to:

Some lower-level languages, like (with getelementptr) and (with synchronizing warps) support broadcasting between scalars and vectors, but without support for higher dimensional arrays.

879 questions
3
votes
2 answers

What is the nicest way to weight planes in a numpy array?

I have the following code in which w is a 1D numpy array of compatible dimension, and M is a 4D array, i = 0 for weight in w: M[:, :, i, :] *= weight i += 1 Is there a nicer way to achieve the same effect?
ati
  • 307
  • 3
  • 10
3
votes
1 answer

numpy broadcasting between matrix and vector?

I am learning numpy linear algerba, and I want to perform a simple calculation: I have: m = np.array([[1,2], [3,4], [5,6]] v = np.array([10,20,30]) what I want to calculate/output: [ [1/10, 2/10], [3/20, 4/20], …
Wei
  • 341
  • 2
  • 14
3
votes
1 answer

np.dot() with Python broadcasting

I have two numpy arrays, one shaped (3000,) and the other is an array of twenty 3000 by 3000 matrices, i.e. shape (20, 3000, 3000) first.shape = (3000,) second.shape = (20, 3000, 3000) I being doing a numpy dot product. import numpy as np dotprod1…
ShanZhengYang
  • 16,511
  • 49
  • 132
  • 234
3
votes
3 answers

Use numpy to get the positions of all objects in 3D space relative to one another

I want get the differences between all permutations of pairs of vectors in a numpy array. In my specific use case these vectors are the 3D position vectors of a list of objects. So, if I have an array r = [r1, r2, r3] where r1, r2 and r3 are…
astro101
  • 165
  • 7
3
votes
1 answer

Broadcasting across an indexed array Numpy

I'm looking for a Numpy (i.e. hopefully faster) way to perform the following: import numpy as np x = np.array([1,2,3,4,5],dtype=np.double) arr =…
johntfoster
  • 361
  • 5
  • 17
2
votes
1 answer

Is there a standard method for broadcasting a 1d numpy array along the first axis of a higher-dimensional array?

My data often takes the form of a single numpy array made from a stack of N n-dimensional arrays of arbitrary shape (e.g. data.shape = (N, a, b, c, ...), where a, b, c, ... are unknown ahead of time), and a corresponding 1d array of coefficients…
2
votes
2 answers

ValueError: operands could not be broadcast together with shapes (842,474) (844,476)

I have this code, but I can't figure out why it gives an error: ValueError: operands could not be broadcast together with shapes (842,474) (844,476) Code: import numpy as np from skimage.io import imread, imshow from scipy.signal import…
2
votes
0 answers

How to find insert location of each row in 2D array into another sorted 2D array

I have a reference 2D numpy array R with 50k rows and 1k columns, which is sorted along axis 0. Given a query 1D array x, I can find the insert location of each value in x into each column of R with this code snippet: R = np.array([[0, 8, 12, 2], …
2
votes
2 answers

How to broadcast the outer sum of elements of two vector of vectors?

I have two Matrix{Vector{}}, say A and B, of the same sizes. I want to get a Matrix{Matrix{}}, M, such that each element is an outer sum of each element of A and B, i.e. M[i, j][k, l] = A[i, j][k] + B[i, j][l]. How could I vectorize the above…
2
votes
1 answer

In PyTorch, how can I avoid an expensive broadcast when adding two tensors then immediately collapsing?

I have two 2-d tensors, which align via broadcasting, so if I add/subtract them, I incur a huge 3-d tensor. I don't really need that though, since I'll be performing a mean on one dimension. In this demo, I unsqueeze the tensors to show how they…
Josh.F
  • 3,666
  • 2
  • 27
  • 37
2
votes
1 answer

Override broadcasting conventions in Numpy?

I have two Numpy arrays, x and y, where x is n-dimensional with n>=1 but unknown at "compile time", whereas y is one-dimensional. The first dimension of x is guaranteed to match the first (and only) dimension of y. I would like get their "sum", call…
Chicken
  • 113
  • 7
2
votes
1 answer

How to set the values of different channels in a numpy array to zero based on index values from another array without using loops?

I have a numpy array "arr" and an array of indices "ind": import numpy as np arr = np.random.randint(255, size=(100,64,64,16)) ind = np.random.randint(16, size=(100,2)) In arr, the last dimension represents channels while the first dimension…
nOOb99
  • 33
  • 3
2
votes
2 answers

Why mini-batches larger than 1 doesn't work, but larger accumulating gradients work?

I am trying to implement a neural network approximating the logical XOR function, however, the network only converges when using a batch size of 1. I don't understand why: when I use gradient accumulation with multiple mini-batches of size 1, the…
Romain
  • 23
  • 4
2
votes
1 answer

Numpy Equivalent of Nesting For loops for approximate pixel color change

The follow code shows how to change a a pixel in a 3D image with numpy using for loops. As you can see, the truth value is a few logical ANDs where an RGB pixel value must be within a certain range. I am wondering how to do this since with numpy…
421
  • 203
  • 1
  • 5
  • 13
2
votes
2 answers

How to use scipy.integrate.fixed_quad for computing many integrals at once?

Given a function func(x,y,z), I want to provide a function def integral_over_z(func,x,y,zmin=0,zmax=1,n=16): lambda_func = z,x,y: ??? return scipy.integrate.fixed_quad(lambda_func,a=zmin,b=zmax,args=(x,y),n=n) that computes its integral…
Walter
  • 44,150
  • 20
  • 113
  • 196