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

broadcast each row of A along B, avoiding `repeat`

I have 2 2D arrays, with 1 axis of the same dimension: a = np.array(np.arange(6).reshape((2,3))) b = np.array(np.arange(12).reshape((3,4))) I want to multiply and broadcast each row of a with b, that is b_r = np.repeat(b[:,:,None], 2, axis=2) ab =…
Itamar Katz
  • 9,544
  • 5
  • 42
  • 74
3
votes
2 answers

Element wise dot product of matrices and vectors

There are really similar questions here, here, here, but I don't really understand how to apply them to my case precisely. I have an array of matrices and an array of vectors and I need element-wise dot product. Illustration: In [1]: matrix1 =…
nicoco
  • 1,421
  • 9
  • 30
3
votes
2 answers

Np array dot product of vector and array

I have a problem in understanding the working behind the numpy dot function and broadcasting.Below is the snippet I am trying to understand a=np.array([[1,2],[3,5]]) if we check the shape of a a.shape it will be (2,2) b=np.array([3,6]) and b.shape…
ankyAS
  • 301
  • 2
  • 11
3
votes
4 answers

How to find the pairwise differences between rows of two very large matrices using numpy?

Given two matrices, I want to compute the pairwise differences between all rows. Each matrix has 1000 rows and 100 columns so they are fairly large. I tried using a for loop and pure broadcasting but the for loop seem to be working faster. Am I…
Bluegreen17
  • 983
  • 1
  • 11
  • 25
3
votes
1 answer

Numpy normalize multi dim (>=3) array

I have a 5 dim array (comes from binning operations) and would like to have it normed (sum == 1 for the last dimension). I thought I found the answer here but it says: ValueError: Found array with dim 5. the normalize function expected <= 2. I…
bio
  • 501
  • 1
  • 5
  • 16
3
votes
1 answer

How to vectorize a dot product of one matrix for every row of another?

If I have a matrix A and I want to get the dot product of A with every row of B. import numpy as np a = np.array([[1.0, 2.0], [3.0, 4.0]]) b = np.array([[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]) If the goal…
3
votes
1 answer

Efficient array creation in numpy

I am trying to create a three dimensional array, Tusing numpy defined as follows: T_{i, j, k} = \delta_{i, k} - \delta{j, k} where \delta_{i, j} is the Kronecker delta function (1 when i=j and 0 otherwise). I am wondering what the most efficient way…
3
votes
0 answers

Index ranges/slices given by two numpy index arrays

I have a data vector x, and two index vectors left and right. The values in left and right correspond the the lower and upper values of the slice I want to define. I could write something like: for l, r in zip(left, right): z = x[l:r] #…
Paul
  • 849
  • 1
  • 7
  • 18
3
votes
1 answer

Is there a way to broadcast boolean masks?

I'm trying to reduce the number of calculations I do based on search distance. I have N nodes and an [NxN] boolean mask that tells me what nodes are within X distance of the other nodes with T true values. I also have [Nx(d)] data for each node,…
Daniel F
  • 13,620
  • 2
  • 29
  • 55
3
votes
1 answer

Understanding Numpy Multi-dimensional Array Indexing

Please, can anyone explain the difference between these three indexing operations: y = np.arange(35).reshape(5,7) # Operation 1 y[np.array([0,2,4]),1:3] # Operation 2 y[np.array([0,2,4]), np.array([[1,2]])] # Operation 3 y[np.array([0,2,4]),…
Evann Courdier
  • 333
  • 2
  • 8
3
votes
4 answers

What is the definition of multiplication of jagged/ragged Numpy.array?

I do not understand what happens when multiplying Numpy.arrays. For example, with with jagged (or ragged) arrays import numpy as np a = np.array([[1,2,3],[100,200]]) b = np.array([2, 4]) print(a * b) I get [[1, 2, 3, 1, 2, 3] [100, 200, 100, 200,…
tiankonghewo
  • 133
  • 7
3
votes
2 answers

Numpy collapse columns according to list

In NumPy, I have a d x n array A and a list L of length n, describing where I want each column of A to end up in matrix B. The idea is that column i of matrix B is the sum of all columns of A for which the corresponding value in L is i. I can do…
Kevin Yang
  • 167
  • 7
3
votes
1 answer

NumPy ndarray broadcasting - shape (X,) vs (X, 1) to operate with (X,Y)

I have a NumPy ndarray which is shaped (32, 1024) and holds 32 signal measurements which I would like to combine into a single 1024 element long array, with a different weight for each of the 32. I was using numpy.average but my weights are complex…
3
votes
2 answers

Explanation on Numpy Broadcasting Answer

I recently posted a question here which was answered exactly as I asked. However, I think I overestimated my ability to manipulate the answer further. I read the broadcasting doc, and followed a few links that led me way back to 2002 about numpy…
MadisonCooper
  • 236
  • 2
  • 15
3
votes
2 answers

`np.dot` without cartesian product on remaining axes

According to the documentation: For N dimensions dot is a sum product over the last axis of a and the second-to-last of b: dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m]) I would like to compute the sum product over the last axis of a and the…
Till Hoffmann
  • 9,479
  • 6
  • 46
  • 64