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
-1
votes
2 answers

Numpy Broadcasting Example without Repeat

I'm trying to figure out a good way of doing the following addition operation without using np.repeat to create a large dimension. If using np.repeat and adding is the best solution let me know. I'm also confused about what broadcasting is doing in…
Kevin Zen
  • 111
  • 1
  • 7
-1
votes
1 answer

ValueError: could not broadcast input array from shape (1120,1472,4) into shape (3000,3000)

I have an image with shape (1120,1472,4), am trying change this to (3000, 3000, 4) the code i have written is like this. pad_shape = (3000, 3000) i = np.array('test.tif') result = np.zeros(pad_shape,dtype=int) result[:i.shape[0], :i.shape[1]] =…
-2
votes
2 answers

Combine 2 different sized arrays element-wise based on index pairing array

Say, we had 2 arrays of unique values: a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # any values are possible, b = np.array([0, 11, 12, 13, 14, 15, 16, 17, 18, 19]) # sorted values are for demonstration , where a[0] corresponds to b[0], a[1] to…
Yerbol Sapar
  • 31
  • 1
  • 8
-2
votes
2 answers

Numpy project array of m X coords and array of n Y coords into mxn list of (X,Y) coords

Effectively I want two nested for loops over xs and ys, where each x is paired with each y: xs = [0,1,2] ys = [2,3] desiredResult = [[0,2],[1,2],[2,2],[0,3],[1,3],[2,3]] Wondering if there's a nice numpy solution?
odkken
  • 329
  • 1
  • 9
-2
votes
1 answer

vectorize a loop with iterative conditions to generate a mask

I am trying to vectorize the following problem: time_lag = np.linspace(0, 10, 50) time = np.arange(100) dt = np.abs(time[:,None]-time[None, :]) ## calculate matrix of differences mask = [] for num in range(len(time_lag)-1): m0 = (time_lag[num] <…
Sebastiano1991
  • 867
  • 1
  • 10
  • 26
-2
votes
1 answer

Effective numpy array calculating in Python

I need to calculate this special variance estimate (see pic. below). I have feature matrix X - dxl (d - # features, l - # objects). It's simply to do this in for cycles: var_list = [] for i in range(X.shape[0]): for j in range(i +…
-2
votes
4 answers

How to broadcast a 1d array to an N-D array in Python

I'm missing something here. I have a 1-D array that I want to broadcast to an N-D array, and it's not working: >>> import numpy as np >>> np.broadcast_to(np.arange(12),(12,2,2)) Traceback (most recent call last): File "", line 1, in…
Jason S
  • 184,598
  • 164
  • 608
  • 970
-6
votes
2 answers

Change sign of elements with an odd sum of indices

arr is a n-dimensional numpy array. How to change sign of every element of arr with an odd sum of indices? For example, arr[0, 1, 2] needs a sign change because it has a sum of indices 0 + 1 + 2 = 3, which is odd. When I convert arr to a list, I…
-6
votes
1 answer

how to use python-numpy-broadcasting

I have a trouble with python broadcasting there is two numpy list x = np.array([[1,2,3],[4,5,6]]) y = np.array([0,1,1]) I'd like to calculate x : [[1,2,3], [4,5,6]] to x : [[1-y[0], 2-y[1], 3-y[2]], [4-y[0], 5-y[1], 6-y[2]]] that…
yi han
  • 1
  • 1
1 2 3
58
59