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

ValueError: operands could not be broadcast together with shapes (200,49000) (10,49000) (200,49000)

This a=np.zeros((20,4900)) b=np.zeros((1,4900)) a+=b works perfectly fine. However, this: a=np.zeros((200,49000)) b=np.zeros((10,49000)) a+=b shows this error: ValueError: operands could not be broadcast together with shapes (200,49000)…
muglikar
  • 146
  • 2
  • 18
0
votes
0 answers

How can I improve numpy's broadcast

I'm trying implementing k-NN with Mahalanobis's distance in python with numpy. However, the code below works very slowly when I use broadcasting. Please teach me how can I improve numpy speed or implement this better. from __future__ import…
0
votes
1 answer

Python numpy ValueError when I load image data and transfer it to an array

I couldn't find the solution.My image shape is 128*128*3,it has three channel,but it also cause the error File "E:/ML/keras_test/vgg.py", line 30, in load_data data[i,:,:,:] = arr ValueError: could not broadcast input array from shape (128,128)…
sky
  • 103
  • 1
  • 7
0
votes
1 answer

compute matrix product for multiple inputs

I am trying to compute a transform given by b = A*x. A is a (3,4) matrix. If x is one (4,1) vector the result is b (3,1). Instead, for x I have a bunch of vectors concatenated into a matrix and I am trying to evaluate the transform for each value…
user1159898
  • 25
  • 1
  • 6
0
votes
1 answer

Multiple Parameter Estimation. Problems with broadcasting

I need to get the parameters(kf, beta1, beta2, gamma) with a nonlinear least squares regression. The error message is: "ValueError: operands could not be broadcast together with shapes (4,7) (0,)" I did 4 experiments with the next data…
Mat S
  • 41
  • 5
0
votes
1 answer

Vectorization of cumulative sum in python

I'm trying to vectorize/broadcast (Not sure what it is called formally) my code in order to make it faster, but I can't quite get it. What I think I should be using is numpy.cumsum (with axis=0) but I don't know how to get it (fast) in the correct…
Exclu
  • 1
  • 2
0
votes
1 answer

How to understand the result of this np.einsum('kij',A)?

For example, A = np.arange(24).reshape((2, 3, 4)) print np.einsum('ijk', A) this is still A with no problem. But if I do print np.einsum('kij', A) the shape is (3, 4, 2). Shouldn't it be (4, 2, 3)? The result of print np.einsum('cab', A) shape is…
Crazymage
  • 114
  • 9
0
votes
1 answer

Numpy: broadcasting + boolean indexing

I have the following numpy arrays A: shape (n1, n2) array of float B: shape (n2,) array of float M: shape (n1, n2) array of bool How do I turn the following pseduo-code inte efficient real code? The arrays may be huge, possibly > 100 million…
Johan Råde
  • 20,480
  • 21
  • 73
  • 110
-1
votes
1 answer

Matrix Inverse broadcasting

I am trying to calculate Rij = Aij * Bij/Cij by Numpy broadcasting. B1 * np.linalg.inv(C1) gives a singular matrix error. I have also tried doing this. It gave me some values but I am not super sure if it is correct. D = B1 / C1[..., None] import…
sansa
  • 21
  • 4
-1
votes
1 answer

Pairwise difference of vectors

Say i have a matrix that is composed of N vectors as columns: matrix=[v_1, v_2, .. v_N] where v is from R^N. I want a new matrix that gives: for all (r in N, s in N) v(r)-v(s). For example if N=3, then i want a 3x3x3 and for a particular index…
sgk525
  • 132
  • 2
  • 13
-1
votes
2 answers

ValueError: operands could not be broadcast together with shapes (540,2500) (540,)

i am trying to divide each x-value by its row mean train_rows_mean = train_data.mean(axis=1) #calculate the mean row_wise #divide each value by row mean train_data/train_rows_mean #broadcasting issue print(train_data.shape) #shape of train…
sahuno
  • 321
  • 1
  • 8
-1
votes
1 answer

Vectorwise iteration of nD array with nditer

Given I have two arrays, say A (shape K,L,M) and B (shape K,M). I want to iterate vectorwise and construct an output C (shape equal to A) by running a function f on each input vector a and scalar b and then reassembling it into the output (i.e. for…
mueslo
  • 708
  • 1
  • 8
  • 20
-1
votes
3 answers

Is there a Numpy array broadcasting expression simpler than this list comprehension?

In this code snippet: triangles = np.float32([[[0, -2], [-2, 3], [1, 1]], [[0, -1], [-1, 3], [1, 1]]]) centers = np.average(triangles, axis=1) samples = np.float32([t-centers[i] for i, t in enumerate(triangles)]) I would like to express samples as…
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72
-1
votes
1 answer

Numpy broadcasting is not supported by the operant /=

I am following the Deep Learning course on Coursera. I found out that when making a matrix division, like the following: x_norm = np.linalg.norm(x, axis=1, keepdims=True) x = x / x_norm It works fine. But when I was the statement instead: x /=…
-1
votes
1 answer

Python - Checking for common cooardinate between 3D ndarray and 2D ndarray without using loops! (Numpy)

Im trying to solve a problem in which I am given 2 ndarray of booleans . one of shape (n,m,z), denote as A, and the other (n,m), denote as B. If I am thinking of the 3D array as an array of 'z' 2D arrays, I would like to check if for every i between…