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

Element-wise minimum of two numpy arrays indexed by another array

I have three arrays of shapes: A = a = np.random.exponential(1, [10, 1000000]) # of shape (10, 1000000) B = a = np.random.exponential(1, [10, 1000000]) # of shape (10, 1000000) I computed another array IND[ ] as below. Each element of IND[ ] is…
0
votes
1 answer

Is NumPy broadcasting associative?

Say I have three numpy.ndarray's a,b,c such that when I multiply them a broadcasting happens. Does the result depend on the order of the multiplication? In other words, do there exist a,b,c such that: (a * b) * c != a * (b * c) ?
nivniv
  • 3,421
  • 5
  • 33
  • 40
0
votes
1 answer

numpy.where() returns inconsisten dimensions

I pass an array of size (734,814,3) to a function but numpy.where() gives one dimensional result instead of the two-dimensional one, which it should for a 2D array def hsi2rgb(img): img_rgb = np.empty_like(img) h = img[:,:,0] #(734,814) …
Vahni
  • 272
  • 1
  • 10
0
votes
1 answer

Use selected Pandas columns with a function to create a matrix

I am trying to create a matrix of the results of a function, which involves a crosstab of dataframe columns. The function operates on a pair of dataframe columns in turn, so that the end result is a matrix of the results applied to each pair. The…
LucieCBurgess
  • 759
  • 5
  • 12
  • 26
0
votes
3 answers

Multiplication of list of lists

Assume that I have a dataframe of two rows and 13 columns. I have used df.itertuples() and formed two lists as an output for row in test.itertuples(index = False): a = np.asarray(row) print(a) let us assume that the Output of the above loop…
learner
  • 35
  • 8
0
votes
1 answer

Numpy - many matrices to same vector

Is there an efficient way to multiply many different rotation matrices to the same vector? Right now I am doing the following, extremely slow procedure for i, rm in enumerate(ray_rotation_matrices): scan_point = rm * np.vstack([scale, 0, 0,…
El Dude
  • 5,328
  • 11
  • 54
  • 101
0
votes
1 answer

python : np.where() and broadcasting

Can someone please help me to understand how broadcasting works below in np.where() function ? x = np.arange(9.).reshape(3, 3) np.where(x < 5, x, -1) # Note: broadcasting. array([[ 0., 1., 2.], [ 3., 4., -1.], [-1., -1., -1.]])
Kumar
  • 41
  • 5
0
votes
1 answer

How to create a 3-D array by multiplying vectors from two 2-D matrices

I have two 2-D matrices which have a shared axis. I want to get a 3-D array that holds the results of every pairwise multiplication made between all the combinations of vectors from each matrix along that shared axis. What is the best way to…
PandaZ
  • 127
  • 3
  • 12
0
votes
2 answers

Numpy Matmul while ignoring first axis

I have a bunch of 5-dimensional vectors of shape (5,) in a 2-dimensional array of shape (1000, 5), each of which which I want to multiply by a matrix of shape (6, 5). I would have assumed that broadcasting would allow me to do A =…
JAustin
  • 890
  • 10
  • 16
0
votes
1 answer

Simulate numpy vectorized function on a meshgrid

This is the example given on how to use numpy.meshgrid x = np.arange(-5, 5, 0.1) y = np.arange(-5, 5, 0.1) xx, yy = np.meshgrid(x, y, sparse=True) z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2) What if I have a meshgrid like xx,yy above, but my…
Ivan
  • 7,448
  • 14
  • 69
  • 134
0
votes
1 answer

Broadcasting index operations in numpy

How can I take elements from a NumPy array given multiple index arrays with broadcasting? Or: how can I simplify/vectorize this loop: elems = np.random.rand(3, 10, 7) # shape N x I x M ind = np.array([[1, 2], [3, 4], [0, 9]]) # shape N x J res =…
Philipp H.
  • 1,513
  • 3
  • 17
  • 31
0
votes
1 answer

numpy array - efficiently subtract each row of B from A

I have two numpy arrays a and b. I want to subtract each row of b from a. I tried to use: a1 - b1[:, None] This works for small arrays, but takes too long when it comes to real world data sizes. a = np.arange(16).reshape(8,2) a Out[35]: array([[…
mistakeNot
  • 743
  • 2
  • 10
  • 24
0
votes
1 answer

How to add every four rows as column

I have a very large numpy array like this. How can I convert this into array([[1, 1, 0, 0, 1, 0, 0, 1], [0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 1, 0, 1, 1, 0], [0, 0,…
0
votes
1 answer

How to calculate dot product with broadcasting?

a=np.arange(18).reshape(2,3,3) b=np.arange(6).reshape(2,3) I wanna calculate the dot product a[0]@b[0] array([ 5, 14, 23]) a[1]@b[1] array([122, 158, 194]) With broadcasting, I tried c=a@b[...,None] c array([[[ 5], [ 14], […
kinder chen
  • 1,371
  • 5
  • 15
  • 25
0
votes
2 answers

Vectorization and matrix multiplication by scalars

I am new to python/numpy. I need to do the following calculation: for an array of discrete times t, calculate $e^{At}$ for a $2\times 2$ matrix $A$ What I did: def calculate(t_,x_0,v_0,omega_0,c): # define A a_11,a_12, a_21,…
Conjecture
  • 353
  • 4
  • 18