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

Broadcasting outside main loop speeds up vectorized numpy ops?

I'm doing some vectorized algebra using numpy and the wall-clock performance of my algorithm seems weird. The program does roughly as follows: Create three matrices: Y (KxD), X (NxD), T (KxN) For each row of Y: subtract Y[i] from each row of X (by…
Przemek D
  • 654
  • 6
  • 26
4
votes
2 answers

Kronecker product of matrix array

I have two matrix arrays A and B such with identical shape: A.shape = B.shape = (M,N,P) I would like to compute the Kronecker product along the axis 0, so that: KP[ii,:,:] = A[ii,:,:]⊗B[ii,:,:] Is there a way of doing this in numpy without using…
4
votes
2 answers

Subtract a column vector from matrix at specified vector of columns using only broadcast

I want to subtract a column vector from a numpy matrix using another vector which is index of columns where the first column vector needs to be subtracted from the main matrix. For eg. M = array([[ 1, 2, 1, 1], [ 2, 1, 1, …
Varun Kuntal
  • 97
  • 1
  • 5
4
votes
2 answers

numpy - vectorize functions: apply_over_axes / apply_along_axis

I want to calculate the determinant of mm subarrays of a mm*n dimensional arrays, and would like to do this in a fast/more elegant way. The brute-force approach works: import numpy as n array=n.array([[[0.,1.,2.,3.],[2,1,1,0]],[[0.5,…
mzzx
  • 1,964
  • 4
  • 16
  • 26
4
votes
2 answers

Python: merging channels in opencv and manually

def frame_processing(frame): out_frame = np.zeros((frame.shape[0],frame.shape[1],4),dtype = np.uint8) b,g,r = cv2.split(frame) alpha = np.zeros_like(b ,…
4
votes
2 answers

TensorFlow broadcasting

Broadcasting is the process of making arrays with different shapes have compatible shapes for arithmetic operations. In numpy, we can broadcast arrays. Does TensorFlow graph support broadcasting similar to the numpy one?
nairouz mrabah
  • 1,177
  • 2
  • 13
  • 26
4
votes
2 answers

How to slice numpy rows using start and end index

index = np.array([[1,2],[2,4],[1,5],[5,6]]) z = np.zeros(shape = [4,10], dtype = np.float32) What is the efficient way to set z[np.arange(4),index[:,0]], z[np.arange(4), index[:,1]] and everything between them as 1? expected output: array([[0, 1,…
figs_and_nuts
  • 4,870
  • 2
  • 31
  • 56
4
votes
1 answer

numpy linear algebra on diagonal arrays without explicit duplication

I have an array w (shape (3000, 100, 100)) which I want to multiply with another array e (shape (5, 3000)) such that the result k has shape (5, 5, 100, 100) and k[:, :, i, j] = e @ np.diag(w[:, i, j]) @ e.T Since w is so large, it's not practical…
DathosPachy
  • 742
  • 1
  • 6
  • 17
4
votes
1 answer

pandas dataframes multiplication with or without broadcasting

I have 2 dataframes: >>> type(c) Out[118]: pandas.core.frame.DataFrame >>> type(N) Out[119]: pandas.core.frame.DataFrame >>> c Out[114]: t 2017-06-01 01:06:00 1.00 2017-06-01 01:13:00 1.00 2017-06-01 02:09:00…
dayum
  • 1,073
  • 15
  • 31
4
votes
1 answer

How to multiply a vector by an array/matrix element-wise in numpy?

I have a multidimensional array a whose shape is (32,3,5,5) and an array v with a shape of (32,). How could I multiply (i,3,5,5) with (i,) for each i using numpy other than a for-loop?
4
votes
1 answer

How to slice a numpy.ndarray made up of numpy.void numbers?

So here's the deal: I have variable x which is a numpy.ndarray. The size of this structure is 1000. If I do x[0], then I get a numpy.void, of 4 numbers. If I do x[1], then I get another numpy.void, also of 4 numbers, etc. What I simply want to do:…
Spacey
  • 2,941
  • 10
  • 47
  • 63
4
votes
3 answers

Numpy 3d array indexing

I have a 3d numpy array (n_samples x num_components x 2) in the example below n_samples = 5 and num_components = 7. I have another array (indices) which is the selected component for each sample which is of shape (n_samples,). I want to select from…
Ash
  • 3,428
  • 1
  • 34
  • 44
4
votes
1 answer

How to implement maxpool: taking a maximum on sliding window on image or tensor

In short: I am looking for a simple numpy (maybe oneliner) implementation of Maxpool - maximum on a window on numpy.narray for all location of the window across dimensions. In more details: I am implementing a convolutional neural network ("CNN"),…
4
votes
2 answers

Un-broadcasting Numpy arrays

In a large code base, I am using np.broadcast_to to broadcast arrays (just using simple examples here): In [1]: x = np.array([1,2,3]) In [2]: y = np.broadcast_to(x, (2,1,3)) In [3]: y.shape Out[3]: (2, 1, 3) Elsewhere in the code, I use…
astrofrog
  • 32,883
  • 32
  • 90
  • 131
4
votes
1 answer

Numpy: How to vectorize parameters of a functional form of a function applied to a data set

Ultimately, I want to remove all explicit loops in the code below to take advantage of numpy vectorization and function calls in C instead of python. Below is simplified for uses of numpy in python. I have the following quadratic function: def…