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

How do I use np.newaxis?

What is numpy.newaxis and when should I use it? Using it on a 1-D array x produces: >>> x array([0, 1, 2, 3]) >>> x[np.newaxis, :] array([[0, 1, 2, 3]]) >>> x[:, np.newaxis] array([[0], [1], [2], [3]])
32
votes
2 answers

Can't use /= on numpy array

On a numpy array, why is it I can successfully use / 2: >>> a=np.array([2, 4, 6]) >>> a = a / 2 >>> a array([ 1., 2., 3.]) But I cannot use a /= 2? >>> a=np.array([2, 4, 6]) >>> a /= 2 Traceback (most recent call last): File "", line 1,…
Tom Hale
  • 40,825
  • 36
  • 187
  • 242
27
votes
3 answers

Numpy array broadcasting rules

I'm having some trouble understanding the rules for array broadcasting in Numpy. Obviously, if you perform element-wise multiplication on two arrays of the same dimensions and shape, everything is fine. Also, if you multiply a multi-dimensional…
Channel72
  • 24,139
  • 32
  • 108
  • 180
24
votes
4 answers

Matlab equivalent of Numpy broadcasting?

I'm trying to find some way to substract a size 3 vector from each column of a 3*(a big number) matrix in Matlab. Of course I could use a loop, but I'm trying to find some more efficient solution, a bit like numpy broadcasting. Oh, and I can't use…
antony
  • 2,877
  • 4
  • 31
  • 43
24
votes
1 answer

Numpy `ValueError: operands could not be broadcast together with shape ...`

Im using python 2.7 and am attempting a forcasting on some random data from 1.00000000 to 3.0000000008. There are approx 196 items in my array and I get the error ValueError: operands could not be broadcast together with shape (2) (50) I do not…
23
votes
1 answer

Numpy - create matrix with rows of vector

I have a vector [x,y,z,q] and I want to create a matrix: [[x,y,z,q], [x,y,z,q], [x,y,z,q], ... [x,y,z,q]] with m rows. I think this could be done in some smart way, using broadcasting, but I can only think of doing it with a for loop.
fulaphex
  • 2,879
  • 3
  • 19
  • 26
22
votes
2 answers

Finding max /min value of individual columns

I am a beginner to Data Analysis using Python and stuck on the following: I want to find maximum value from individual columns (pandas.dataframe) using Broadcasting /Vectorization methodology. A snapshot of my data Frame is as follows:
SeleCoder
  • 233
  • 1
  • 2
  • 7
18
votes
2 answers

How does pytorch broadcasting work?

torch.add(torch.ones(4,1), torch.randn(4)) produces a Tensor with size: torch.Size([4,4]). Can someone provide a logic behind this?
aerin
  • 20,607
  • 28
  • 102
  • 140
16
votes
2 answers

When does Pandas default to broadcasting Series and Dataframes?

I came across something curious (to me) while trying to answer this question. Say I want to compare a series of shape (10,) to a df of shape (10,10): np.random.seed(0) my_ser = pd.Series(np.random.randint(0, 100, size=10)) my_df =…
Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75
16
votes
4 answers

Broadcast an operation along specific axis in python

In python, suppose I have a square numpy matrix X, of size n x n and I have a numpy vector a of size n. Very simply, I want to perform a broadcasting subtraction of X - a, but I want to be able to specify along which dimension, so that I can…
Spacey
  • 2,941
  • 10
  • 47
  • 63
15
votes
3 answers

Broadcasted NumPy arithmetic - why is one method so much more performant?

This question is a follow up to my answer in Efficient way to compute the Vandermonde matrix. Here's the setup: x = np.arange(5000) # an integer array N = 4 Now, I'll compute the Vandermonde matrix in two different ways: m1 = (x **…
cs95
  • 379,657
  • 97
  • 704
  • 746
14
votes
1 answer

Subtracting numpy arrays of different shape efficiently

Using the excellent broadcasting rules of numpy you can subtract a shape (3,) array v from a shape (5,3) array X with X - v The result is a shape (5,3) array in which each row i is the difference X[i] - v. Is there a way to subtract a shape (n,3)…
MaxPowers
  • 5,235
  • 2
  • 44
  • 69
12
votes
1 answer

When broadcasting is a bad idea ? (numpy)

The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Example 1: from numpy import array a = array([1.0,2.0,3.0]) b = array([2.0,2.0,2.0]) # multiply element-by-element () a * b >> array([ 2., …
AnouarZ
  • 1,087
  • 8
  • 23
10
votes
4 answers

Vectorized NumPy linspace for multiple start and stop values

I need to create a 2D array where each row may start and end with a different number. Assume that first and last element of each row is given and all other elements are just interpolated according to length of the rows In a simple case let's say I…
9
votes
2 answers

broadcast views irregularly numpy

Assuming I want have a numpy array of size (n,m) where n is very large, but with a lot of duplication, ie. 0:n1 are identical, n1:n2 are identical etc. (with n2%n1!=0, ie not regular intervals). Is there a way to store only one set of values for…
M.T
  • 4,917
  • 4
  • 33
  • 52
1
2 3
58 59