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

Numpy - normalize RGB pixel array

I have a numpy array with shape (34799, 32, 32, 3)which means (num examples, width, height, channels). Now I normalize the image data with the following code: def normalize(x): return (x - 128) / 128 X_train_norm = normalize(X_train) But the…
jetta
  • 93
  • 1
  • 1
  • 6
9
votes
4 answers

Calculate Distances Between One Point in Matrix From All Other Points

I am new to Python and I need to implement a clustering algorithm. For that, I will need to calculate distances between the given input data. Consider the following input data - [[1,2,8], [7,4,2], [9,1,7], [0,1,5], …
Adhish Thite
  • 463
  • 2
  • 5
  • 20
9
votes
2 answers

Broadcast 1D array against 2D array for lexsort : Permutation for sorting each column independently when considering yet another vector

Consider the array a np.random.seed([3,1415]) a = np.random.randint(10, size=(5, 4)) a array([[0, 2, 7, 3], [8, 7, 0, 6], [8, 6, 0, 2], [0, 4, 9, 7], [3, 2, 4, 3]]) I can create b which contains the permutation to sort…
piRSquared
  • 285,575
  • 57
  • 475
  • 624
9
votes
1 answer

How to keep numpy from broadcasting when creating an object array of different shaped arrays

I try to store a list of different shaped arrays as a dtype=object array using np.save (I'm aware I could just pickle the list but I'm really curious how to do this). If I do this: import numpy as np np.save('test.npy', [np.zeros((2, 2)),…
rich
  • 403
  • 3
  • 9
9
votes
2 answers

What's the difference between "numpy.add(a,b)" and "a+b"?

Is there any difference between numpy.add(a,b) and a+b when adding two ndarrays a and b? The documentation says that numpy.add is the "Equivalent to x1 + x2 in terms of array broadcasting.". But I don't unserstand what this means, since…
Manu
  • 283
  • 3
  • 7
9
votes
3 answers

What are the rules for comparing numpy arrays using ==?

For example, trying to make sense of these results: >>> x array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> (x == np.array([[1],[2]])).astype(np.float32) array([[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 1., 0., 0., 0., 0., …
yalis
  • 1,508
  • 1
  • 16
  • 24
9
votes
2 answers

how to multiply pandas dataframe with numpy array with broadcasting

I have a dataframe of shape (4, 3) as following: In [1]: import pandas as pd In [2]: import numpy as np In [3]: x = pd.DataFrame(np.random.randn(4, 3), index=np.arange(4)) In [4]: x Out[4]: 0 1 2 0 0.959322 0.099360 …
Wei Li
  • 597
  • 3
  • 5
  • 13
8
votes
1 answer

Using numpy isin element-wise between 2D and 1D arrays

I have quite a simple scenario where I'd like to test whether both elements of a two-dimensional array are (separately) members of a larger array - for example: full_array = np.array(['A','B','C','D','E','F']) sub_arrays = np.array([['A','C','F'], …
Chris J Harris
  • 1,597
  • 2
  • 14
  • 26
8
votes
1 answer

Numpy: assignment destination is read-only - broadcast

I start with a 2D array and want to broadcast it to a 3D array (eg. from greyscale image to rgb image). This is the code I use. >>> img_grey = np.random.randn(4, 4) >>> img_rgb = np.broadcast_to(np.expand_dims(img_grey, axis=-1), (4, 4, 3)) This…
wouterdobbels
  • 478
  • 1
  • 4
  • 12
8
votes
3 answers

NumPy Broadcasting: Calculating sum of squared differences between two arrays

I have the following code. It is taking forever in Python. There must be a way to translate this calculation into a broadcast... def euclidean_square(a,b): squares = np.zeros((a.shape[0],b.shape[0])) for i in range(squares.shape[0]): …
Chris
  • 28,822
  • 27
  • 83
  • 158
8
votes
3 answers

Memory Efficient L2 norm using Python broadcasting

I am trying to implement a way to cluster points in a test dataset based on their similarity to a sample dataset, using Euclidean distance. The test dataset has 500 points, each point is a N dimensional vector (N=1024). The training dataset has…
user1462351
  • 133
  • 1
  • 1
  • 6
7
votes
4 answers

How to convert 3D RGB label image (in semantic segmentation) to 2D gray image, and class indices start from 0?

I have a rgb semantic segmentation label, if there exists 3 classes in it, and each RGB value is one of: [255, 255, 0], [0, 255, 255], [255, 255, 255] respectively, then I want to map all values in RGB file into a new 2D label image according to…
Shouyu Chen
  • 655
  • 8
  • 16
7
votes
0 answers

numpy '==' switches semantics as operands grow in size

Just stumbled over the following: Python 3.6.0 (default, Jan 9 2017, 22:01:27) [GCC 4.8.5] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as np >>> >>> np.version.version '1.14.2' >>> >>> a =…
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
7
votes
3 answers

np.newaxis with Numba nopython

Is there a way to use np.newaxis with Numba nopython ? In order to apply broadcasting function without fallbacking on python ? for example @jit(nopython=True) def toto(): a = np.random.randn(20, 10) b = np.random.randn(20) c =…
EntrustName
  • 421
  • 6
  • 19
7
votes
2 answers

broadcast an array to different shape (adding "fake" dimensions)

In python (using numpy), I can broadcast an array to a different shape: >>> import numpy as np >>> a = np.array([2,3,4]) >>> b = np.zeros((3,2)) >>> b[:,:] = np.zeros((3,2)) >>> b[:,:] = a[:,np.newaxis] #<-- np.newaxis allows `a` to be…
mgilson
  • 300,191
  • 65
  • 633
  • 696
1
2
3
58 59