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

How to understand the following fancy index behaviour for multi-dimensional arrays?

We noticed that the mixed usage of fancy indexing and slicing is so confusing and undocumented for multi-dimensional arrays, for example: In [114]: x = np.arange(720).reshape((2,3,4,5,6)) In [115]: x[:,:,:,0,[0,1,2,4,5]].shape Out[115]: (2, 3, 4,…
5
votes
1 answer

Julia vectorized operators

I understand that in Julia most operators can be vectorized by prefixing it with .. However I don't understand why some of them worth both ways: julia> a = rand(1_000_000); julia> @time a*2; 0.051112 seconds (183.93 k allocations: 17.849 MiB,…
5
votes
5 answers

How to raise every element of a vector to the power of every element of another vector?

I would like to raise a vector by ascending powers form 0 to 5: import numpy as np a = np.array([1, 2, 3]) # list of 11 components b = np.array([0, 1, 2, 3, 4]) # power c = np.power(a,b) desired results are: c = [[1**0, 1**1, 1**2, 1**3, 1**4],…
Hamza
  • 63
  • 3
5
votes
3 answers

What is the numpy equivalent of expand in pytorch?

Suppose I have a numpy array x of shape [1,5]. I want to expand it along axis 0 such that the resulting array y has shape [10,5] and y[i:i+1,:] is equal to x for each i. If x were a pytorch tensor I could simply do y = x.expand(10,-1) But there is…
ihdv
  • 1,927
  • 2
  • 13
  • 29
5
votes
1 answer

How to solve: ValueError: operands could not be broadcast together with shapes (4,) (4,6)

I have to sum 2 arrays with broadcasting. This is the first: a = [0 1 2 3] And this is the second: A = [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]] This is the code I had tried until now: a = np.array(a) A =…
user12268725
5
votes
1 answer

A broadcasting issue involving where to put the padding

Introduction I have a function func which is vectorizable, and I vectorize it using np.frompyfunc. Rather than using a nested for loop, I want to call it only once, and thus I'll need to pad the inputs with np.newaxis's. My goal is to get rid of the…
5
votes
1 answer

How to broadcast numpy indexing along batch dimensions?

For example, np.array([[1,2],[3,4]])[np.triu_indices(2)] has shape (3,), being a flattened list of the upper triangular entries. However, if I have a batch of 2x2 matrices: foo = np.repeat(np.array([[[1,2],[3,4]]]), 30, axis=0) and I want to…
5
votes
2 answers

Python array indexed with list but array dimensions are permuted

I try to index an array (has five dimensions) using a list. However, under certain situation, the array is permuted. Say, a has the shape of (3,4,5,6,7), i.e., >>> a = np.zeros((3,4,5,6,7)) >>> a.shape (3, 4, 5, 6, 7) Using a list to index this…
Liang Guo
  • 51
  • 4
5
votes
2 answers

Multiply DataFrame by Different shape DataFrame (or series)

I have this DataFrame like this: 1 2 1 3 1 4 2 4 5 1 1 4 1 3 5 3 1 4 1 3 1 3 1 4 Another like this 1 1 0 0 0 0 I want to multiply them such as that I get 1 2 0 0 0 0 2 4 0 0 0 0 1 3 0 0 0 0 1 3 0 0 0 …
EGM8686
  • 1,492
  • 1
  • 11
  • 22
5
votes
2 answers

np.dot 3x3 with N 1x3 arrays

I have an ndarray of N 1x3 arrays I'd like to perform dot multiplication with a 3x3 matrix. I can't seem to figure out an efficient way to do this, as all the multi_dot and tensordot, etc methods seem to recursively sum or multiply the results of…
iceblueorbitz
  • 920
  • 1
  • 7
  • 17
5
votes
1 answer

Linear interpolation of two 2D arrays

In a previous question (fastest way to use numpy.interp on a 2-D array) someone asked for the fastest way to implement the following: np.array([np.interp(X[i], x, Y[i]) for i in range(len(X))]) assume X and Y are matrices with many rows so the for…
digbyterrell
  • 3,449
  • 2
  • 24
  • 24
5
votes
3 answers

pandas apply typeError: 'float' object is not subscriptable

I have a dataframe df_tr like this: item_id target target_sum target_count 0 0 0 1 50 1 0 0 1 50 I'm trying to find the mean of the…
Chia Yi
  • 562
  • 2
  • 7
  • 21
5
votes
1 answer

How to get elements from a 2D numpy array with a list 2d indices using broadcasting?

If I have a 2D numpy array that I want to extract elements using a list of row,col index pairs. xy = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) idx = np.array([[0, 0], [1, 1], [2, 2]]) The for loop solution: elements = list() for i in idx: …
dranobob
  • 796
  • 1
  • 5
  • 19
5
votes
2 answers

Forcing multiplication to use __rmul__() instead of Numpy array __mul__() or bypassing the broadcasting

This question is close to what is asked in Overriding other __rmul__ with your class's __mul__ but I am under the impression that this is a more general problem then only numerical data. Also that is not answered and I really don't want to use the…
percusse
  • 3,006
  • 1
  • 14
  • 28
5
votes
3 answers

Numpy: Replace every value in the array with the mean of its adjacent elements

I have an ndarray, and I want to replace every value in the array with the mean of its adjacent elements. The code below can do the job, but it is super slow when I have 700 arrays all with shape (7000, 7000) , so I wonder if there are better ways…
Chiefscreation
  • 181
  • 3
  • 12