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

Theano: Restoring broadcastable settings after dense -> sparse -> dense transformation

Background: I'm working on a project that historically has relied on sparse matrices for a lot of the math, and developing a plugin to outsource some of the heavy lifting to theano. Since theano's sparse support is limited, we're building a dense…
krivard
  • 652
  • 6
  • 17
0
votes
0 answers

Error while reshaping an array of images during CNN training using numpy

I am trying to train a model on some set of images. But while training, i am getting following error: ValueError: could not broadcast input array from shape (64,64,3) into shape (64,64) I have resized all images to shape (64,64,3) using…
0
votes
1 answer

How to vectorize getting sub arrays from numpy array using indexing arrays

I want to get a numpy array of sub arrays from a base array using some type of indexing arrays (style/format of indexing arrays open for suggestions). I can easily do this with a for loop, but wondering if there is a clever way to use numpy…
0
votes
0 answers

How to average sub arrays in Numpy using broadcasting?

If there is a numpy array that is a list of 2d arrays, is there more efficient way than calling the mean function twice? z = np.array([[[0, 0, 0], [10, 10, 10]], [[0, 0, 0], [5, 5, 5]], [[0,…
dranobob
  • 796
  • 1
  • 5
  • 19
0
votes
0 answers

Handling the Broadcasting without loosing info (2d array and 3d array operations)

I have a SDF which is a 2d numpy array and also an image feature array which is a 3d array, it has the various texture in the third dimension. So in my equation I have this: ux = np.sum(cv2.blur(phi0*txtre, (3, 3)))/np.sum(cv2.blur(phi0, (3,…
0
votes
2 answers

Why does this array-reshaping routine work outside of a function but not inside of a function?

I am trying to convert a list into a numpy array with a specified number of columns. I can get the code to work outside the function as follows: import numpy as np ls = np.linspace(1,100,100) # Data Sample ls = np.array(ls) # list --> array #…
user7345804
0
votes
0 answers

numpy multiplying arrays of shape (20L,), (20L,) gives broadcast error?

I have two arrays, which a printout claims are both of the same size ((20L,)). I want to multiply them element-wise. Using A*B, or np.multiply(A,B) give the same error: ValueError: non-broadcastable output operand with shape (20,) doesn't match the…
Dan
  • 11
  • 2
0
votes
1 answer

numpy - do operation along specified axis

So I want to implement a matrix standardisation method. To do that, I've been told to subtract the mean and divide by the standard deviation for each dimension And to verify: after this processing, each dimension has zero mean and unit…
User1291
  • 7,664
  • 8
  • 51
  • 108
0
votes
0 answers

Sort an array with numerical values: the good way

First, let me give some context: I teach a first course in numerics for students who major in biology and mathematics. In this course, I try to give the students good habits for manipulating array of numbers, such as using the ndarray data structure…
bela83
  • 268
  • 1
  • 10
0
votes
2 answers

masked softmax in theano

I am wondering if it possible to apply a mask before performing theano.tensor.nnet.softmax? This is the behavior I am looking for: >>>a = np.array([[1,2,3,4]]) >>>m = np.array([[1,0,1,0]]) # ignore index 1 and…
A.D
  • 1,480
  • 2
  • 18
  • 33
0
votes
1 answer

ValueError: could not broadcast input array from shape (256,340,3) into shape (256,340,30)

I'm trying to save in python a stack of 10 rgb images, but i get the following error: rgb[:,:,:, i] = img ValueError: could not broadcast input array from shape (256,340,3) into shape (256,340,30) I tried: img = cv2.imread(img_file,…
AMayer
  • 415
  • 5
  • 19
0
votes
2 answers

How to construct the following matrix elegantly in numpy?

Suppose I have a 5 dimensional matrix v and now I want a new matrix D fulfilling D[a, b, n, m, d] = v[a, b, n, n, d]-v[a, b, m, m, d]. How do I elegantly do this in numpy?
atbug
  • 818
  • 6
  • 26
0
votes
1 answer

Python numpy array multiplication

If I have to arrays X (X has n rows and k columns) and Y (Y has n rows and q columns) how do I multiply the two in the vector form, such that I obtain array Z with following…
1nsg
  • 109
  • 1
  • 9
0
votes
1 answer

How to use broadcasting to speed up this code?

I have the following multi-dimensional array. The first axis denotes a 3-dimensional vector. I want to calculate the 3-by-3 matrix x⋅x' for each of those. My current solution: arr.shape # (3, 64, 64, 33, 187) dm = arr.reshape(3,-1) dm.shape # (3,…
Thomas Möbius
  • 1,702
  • 2
  • 17
  • 23
0
votes
3 answers

How to get constant function to keep shape in NumPy

I have a NumPy array A with shape (m,n) and want to run all the elements through some function f. For a non-constant function such as for example f(x) = x or f(x) = x**2 broadcasting works perfectly fine and returns the expected result. For f(x) =…
Ben
  • 465
  • 2
  • 6
  • 17