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
1 answer

Array-Broadcasting in Cython Memoryview

I created a typed memoryview in cython and would like to multiply it by a scalar: import numpy as np import math cimport numpy as np def foo(): N = 10 cdef np.double_t [:, :] A = np.ones(shape=(N,N),dtype=np.double_) cdef int i,j …
David
  • 33
  • 2
3
votes
3 answers

How can I repeat an array m times

I have an array, e.g. arr = [1, 2, 3, 4], and m = 3. I want to make a matrix with m rows, repeating that array. The output of the example would be [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]] How can I do this? I tried by np.vstack((arr, arr,…
Katenn
  • 41
  • 4
3
votes
1 answer

PyTorch: How to multiply via broadcasting of two tensors with different shapes

I have the following two PyTorch tensors A and B. A = torch.tensor(np.array([40, 42, 38]), dtype = torch.float64) tensor([40., 42., 38.], dtype=torch.float64) B =…
Leockl
  • 1,906
  • 5
  • 18
  • 51
3
votes
2 answers

Add multiple np.newaxis as needed?

I would like to pairwise compare (with <=) all elements of two NumPy ndarrays A and B, where both arrays can have arbitrary dimensions m and n, such that the result is an array of dimension m + n. I know how to do it for given dimension of…
A. Donda
  • 8,381
  • 2
  • 20
  • 49
3
votes
1 answer

Numpy better ways of iterating over colour channels using binary mask

Is there a better way to apply a binary mask to colour channels in numpy? I end up having to do this all the time and it feels like there should be. for c in range(3): a_image[mask, c] = b_image[mask, c] shapes are (x, y, c) for a_image and…
Zac Todd
  • 113
  • 1
  • 11
3
votes
2 answers

Broadcasting ranges

I have an array of arrays in my dataframe - split.(df2.name): > 392-element Array{Array{SubString{String},1},1}: ["chevrolet", "chevelle", "malibu"] ["buick", "skylark", "320"] ["plymouth", "satellite"] ["amc", "rebel", "sst"] …
chefhose
  • 2,399
  • 1
  • 21
  • 32
3
votes
2 answers

Numpy where functionality for Julia code?

I read the answer to "What is Julia equivalent of numpy's where function?", but do not yet see how the answer (ifelse) gives the user all the functionality of numpy.where. I have posted example code below: A = [0.0 0.9 0.0 0.99 0.0] a =…
elscan
  • 113
  • 1
  • 9
3
votes
1 answer

Access elements of a Tensor

I have the following TensorFlow tensors. tensor1 = tf.constant(np.random.randint(0,255, (2,512,512,1)), dtype='int32') #All elements in range [0,255] tensor2 = tf.constant(np.random.randint(0,255, (2,512,512,1)), dtype='int32') #All elements in…
Silver moon
  • 229
  • 3
  • 15
3
votes
2 answers

How can I use broadcasting with NumPy to speed up this correlation calculation?

I'm trying to take advantage of NumPy broadcasting and backend array computations to significantly speed up this function. Unfortunately, it doesn't scale so well so I'm hoping to greatly improve the performance of this. Right now the code isn't…
O.rka
  • 29,847
  • 68
  • 194
  • 309
3
votes
2 answers

How to append to a ndarray

I'm new to Numpy library from Python and I'm not sure what I'm doing wrong here, could you help me please with this? So, I initialize my ndarray like this. A = np.array([]) And then I'm training to append into this array A a new array X which has a…
3
votes
0 answers

Simplify this triple loop with broadcasting

I have a function written in pure numpy, where i compute some statistic a huge number of time, and it's too long. This function contains a triple loop, but i cannot find how to translate it to broadcasting. As my actual data is hard to understand…
lrnv
  • 1,038
  • 8
  • 19
3
votes
1 answer

How to get numpy to broadcast an operation after a reduction operation

I am trying to normalize some data for the last dimensions. #sample data x = numpy.random.random((3, 1, 4, 16, 16)) x[1] = x[1]*2 x[2] = x[2]*4 I can get the mean, m = x.mean((-3, -2, -1)) Now, x.shape is (3, 1, 4, 16, 16) and m.shape is (3, 1), I…
matt
  • 10,892
  • 3
  • 22
  • 34
3
votes
2 answers

How to force a function to broadcast without invoking `np.vectorize`

I want to look for a way to force a function to broadcast. There are scenarios in which the function/method may be overwritten in a later instance, to constant function. In such case if arr = np.arange(0, 1, 0.0001) f = lambda x: 5 f(arr) # this…
Wunderbar
  • 547
  • 5
  • 11
3
votes
1 answer

Numpy Broadcast_to((50000,), (50000,32,32,3)) fails. Why?

I'm trying to broadcast a 1D numpy array to a 4D numpy array but I get an error: operands could not be broadcast together with remapped shapes [original->remapped]: (50000,) and requested shape (50000,32,32,3) This is my code: from…
Jupiter
  • 1,421
  • 2
  • 12
  • 31
3
votes
1 answer

Multiply each row by different rotation matrix

This function multiplies each of the n rows of pose by a different rotation matrix. Is it possible to avoid the loop by maybe using a 3d tensor of rotation matrices? def transform(ref, pose): n, d = pose.shape p = ref[:, :d].copy() c =…
Manuel Schmidt
  • 2,429
  • 1
  • 19
  • 32