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

Set opencv images/numpy array values using an array of pixels

Attempting to do forward warping of a homography matrix in OpenCV. You don't have to know what that means to understand the issue though. Assume there are 2 images (an image is a 2D Numpy array of pixel values), A and B, and an array match that…
James L.
  • 12,893
  • 4
  • 49
  • 60
0
votes
2 answers

A fast way to multiply a NumPy array of scalars by an array of arrays

What is the most concise way to carry out multiplication like this? # c's are scalars (or arrays like A's in general) x = np.array([c1, c2, c3]) # A's are NumPy arrays M = np.array([A1, A2, A3]) to get x*M = [c1*A1, c2*A2, c3*A3] c's are scalars,…
0
votes
1 answer

Why can't a 3, 2 matrix be multiplied by a 2, 2 matrix with NumPy?

Using NumPy and attempting to multiply matrices together sometimes doesn't work. For example import numpy as np x = np.matrix('1, 2; 3, 8; 2, 9') y = np.matrix('5, 4; 8, 2') print(np.multiply(x, y)) can return Traceback (most recent call last): …
0
votes
2 answers

numpy broadcasting with 3d arrays

Is it possible to apply numpy broadcasting (with 1D arrays), x=np.arange(3)[:,np.newaxis] y=np.arange(3) x+y= array([[0, 1, 2], [1, 2, 3], [2, 3, 4]]) to 3d matricies similar to the one below, such that each element in a[i] is treated…
Alex
  • 73
  • 1
  • 11
0
votes
2 answers

Python: check if columns of array are within boundaries, if not pick a random number within the boundary

I have an array of the form a = np.array([[1,2],[3,4],[5,6]]) and I have a "domain" or boundary, which is again an array of the form b = np.array([[0, 4], [3,7]]) Basically I want to check that a[:,0] is within the first row of b and that a[:,1]…
Euler_Salter
  • 3,271
  • 8
  • 33
  • 74
0
votes
2 answers

Broadcasting between two same-rank tensors in tensorflow

I have two tensors x and s with shapes: > x.shape TensorShape([Dimension(None), Dimension(3), Dimension(5), Dimension(5)]) > s.shape TensorShape([Dimension(None), Dimension(12), Dimension(5), Dimension(5)]) I want to broadcast the dot product…
John
  • 168
  • 1
  • 10
0
votes
1 answer

Auto broadcasting in Scipy

I have two np.ndarrays, data with shape (8000, 500) and sample with shape (1, 500). What I am trying to achieve is measure various types of metrics between every row in data to sample. When using from sklearn.metrics.pairwise.cosine_distances I was…
bluesummers
  • 11,365
  • 8
  • 72
  • 108
0
votes
2 answers

How to fill a 4D matrix with a 2D matrix for every 2D cross section in Python

So I have a matrix call it Vjunk which is 70x70x70x70. I have another matrix which is 70x70 call it V. What I wanna do is that for every i, j the matrix Vjunk[:,:,i,j] is 70 by 70. I want to change this matrix so that it is replaced by itself +…
0
votes
1 answer

Is it possible to make these matrix operations without loops in python 2.7 (numpy)?

I want to implement the loops below using matrices in Python: import numpy as np n = 5 # samples k = 2 # inputs m = 3 # gaussians # X is nxk X = np.array([[0.0, 10.0], [20.0, 30.0],[40, 50],[60,70],[80,90]]) #locations is mxk locations =…
DanielTheRocketMan
  • 3,199
  • 5
  • 36
  • 65
0
votes
1 answer

User defined function: operands could not be broadcast together

I am working on this code. I understand what the problem is which is indicated clearly by the ValueError. I want to know if there is a good way around my problem. That is to design a function that can take a (400,400) array and for each single…
Loading Zone
  • 177
  • 2
  • 12
0
votes
1 answer

More pythonic way to compute derivatives of broadcast addition in numpy?

Let's say c = a + b, but a and b are ndarrays, whose shapes are not necessarily the same. That is, they could be any two arrays that follow the general broadcasting rules. I have the deriviative of some output dl/dc, and I'd like to compute dl/da.…
michaelsnowden
  • 6,031
  • 2
  • 38
  • 83
0
votes
1 answer

Numpy/PyTorch method for partial tiling

I seem to recall encountering a Numpy or PyTorch method similar to numpy.tile, except that it allowed partial tiling to reach specified dimensions. So if I had a = np.array([[5, 6, 7, 8], [1, 2, 3, 4]]) (or, correspondingly, t =…
perigon
  • 2,160
  • 11
  • 16
0
votes
1 answer

Numpy broadcasting of fancy index

How does the np.newaxis work within the index of numpy array in program 1? Why it works like this? Program 1: import numpy as np x_id = np.array([0, 3])[:, np.newaxis] y_id = np.array([1, 3, 4, 7]) A = np.zeros((6,8)) A[x_id, y_id] += 1…
hamster on wheels
  • 2,771
  • 17
  • 50
0
votes
1 answer

Is adding a dimension broadcasting?

Given a = tf.constant([[1, 2, 3], [10, 20, 30], [100, 200, 300], [1000, 2000, 3000]]) all of the following are equivalent b = tf.constant([100000, 200000, 300000]) print((a+b).eval()) bb = tf.constant([[100000, 200000,…
orome
  • 45,163
  • 57
  • 202
  • 418
0
votes
1 answer

Counting values along an axis in a 3D array that are greater than threshold values from a 2D array

I have a 3D array of dimensions (200,200,3). These are images of dimensions (200,200) stacked using numpy.dstack. I would like to count the number of values along axis=2 that are greater than a corresponding 2D threshold array of dimensions…