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

Multiplying Numpy 3D arrays by 1D arrays

I am trying to multiply a 3D array by a 1D array, such that each 2D array along the 3rd (depth: d) dimension is calculated like: 1D_array[d]*2D_array And I end up with an array that looks like,…
6
votes
0 answers

Overload Broadcasting such that it does Async

I am working on a web-API access package in Julia, following fairly standard REST. It has lots of methods like: post_foo(cred::ServiceCred, x, y) get_foo(cred::ServiceCred, x, y) Sometimes I would like to post multiple things to the API at once (or…
Frames Catherine White
  • 27,368
  • 21
  • 87
  • 137
6
votes
1 answer

python list + empty numpy array = empty numpy array?

Today I noticed something odd in my code, and discovered that in certain situations it run down to the execution of the following: my_list = [0] + np.array([]) which results in my_list being the following: array([], dtype=float64) At the beginning…
GivAlz
  • 185
  • 5
6
votes
1 answer

How to initialize Numpy array of list objects

I'm trying to create a numpy array that looks like array([[list([]), list([])], [list([]), list([])], [list([]), list([])]], dtype=object) This array has shape (3,2). However, whenever I do np.array([[list(), list()], [list(),…
Anonymous
  • 295
  • 2
  • 13
6
votes
3 answers

Matrix Multiplication: Multiply each row of matrix by another 2D matrix in Python

I am trying to remove the loop from this matrix multiplication (and learn more about optimizing code in general), and I think I need some form of np.broadcasting or np.einsum, but after reading up on them, I'm still not sure how to use them for my…
LED
  • 129
  • 3
  • 6
6
votes
3 answers

Element-wise broadcasting for comparing two NumPy arrays?

Let's say I have an array like this: import numpy as np base_array = np.array([-13, -9, -11, -3, -3, -4, 2, 2, 2, 5, 7, 7, 8, 7, 12, 11]) Suppose I want to know: "how many elements in base_array are greater than…
dain
  • 672
  • 1
  • 7
  • 22
6
votes
1 answer

NumPy indexing: broadcasting with Boolean arrays

Related to this question, I came across an indexing behaviour via Boolean arrays and broadcasting I do not understand. We know it's possible to index a NumPy array in 2 dimensions using integer indices and broadcasting. This is specified in the…
jpp
  • 159,742
  • 34
  • 281
  • 339
6
votes
1 answer

Why the following operands could not be broadcasted together?

The arrays are of following dimensions: dists: (500,5000) train: (5000,) test:(500,) Why does the first two statements throw an error whereas the third one works fine? dists += train + test Error: ValueError: operands could not be broadcast…
Benedict
  • 360
  • 1
  • 6
  • 15
6
votes
2 answers

Is there something like np.linspace for 3D lines?

I have a 3x1 point vector representing the start point of some line, and a 3x1 point vector representing the end of some line. I would like to sample an arbitrary amount of points along the line connected by these two points. np.linspace does…
Carpetfizz
  • 8,707
  • 22
  • 85
  • 146
6
votes
2 answers

Subtract each row of matrix A from every row of matrix B without loops

Given two arrays, A (shape: M X C) and B (shape: N X C), is there a way to subtract each row of A from each row of B without using loops? The final output would be of shape (M N X C). Example A = np.array([[ 1, 2, 3], [100, 200,…
coolscitist
  • 3,317
  • 8
  • 42
  • 59
6
votes
1 answer

What is going on behind this numpy selection behavior?

Answering this question, some others and I were actually wrong by considering that the following would work: Say one has test = [ [ [0], 1 ], [ [1], 1 ] ] import numpy as np nptest = np.array(test) What is the reason behind >>>…
keepAlive
  • 6,369
  • 5
  • 24
  • 39
6
votes
1 answer

Why do Python/Numpy require a row vector for matrix/vector dot product?

Assume we want to compute the dot product of a matrix and a column vector: So in Numpy/Python here we go: a=numpy.asarray([[1,2,3], [4,5,6], [7,8,9]]) b=numpy.asarray([[2],[1],[3]]) a.dot(b) Results in: array([[13], [31], …
robert
  • 1,921
  • 2
  • 17
  • 27
6
votes
1 answer

Best way to insert values of 3D array inside of another larger array

There must be some 'pythonic' way to do this, but I don't think np.place, np.insert, or np.put are what I'm looking for. I want to replace the values inside of a large 3D array A with those of a smaller 3D array B, starting at location [i,j,k] in…
uhoh
  • 3,713
  • 6
  • 42
  • 95
6
votes
3 answers

Adding a 1-D Array to a 3-D array in Numpy

I am attempting to add two arrays. np.zeros((6,9,20)) + np.array([1,2,3,4,5,6,7,8,9]) I want to get something out that is like array([[[ 1., 1., 1., ..., 1., 1., 1.], [ 2., 2., 2., ..., 2., 2., 2.], [ 3., 3., 3., ..., …
6
votes
4 answers

Is there a better way to broadcast arrays?

I want to broadcast an array b to the shape it would take if it were in an arithmetic operation with another array a. For example, if a.shape = (3,3) and b was a scalar, I want to get an array whose shape is (3,3) and is filled with the scalar. One…
user545424
  • 15,713
  • 11
  • 56
  • 70
1 2
3
58 59