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

Numpy: Find minimum of an expression over several parameters

Is there a numpy way to get the minimum of an expression over many parameters without explicit loops? #Randomly initialize samples SAMPLES_NUM = 200 L = np.random.rand(SAMPLES_NUM) q1 = np.random.rand(SAMPLES_NUM) q2 =…
AspiringMat
  • 2,161
  • 2
  • 21
  • 33
0
votes
2 answers

Algorithm in Python equivalent to Q to manually generate identity matrix

I know about np.eye which generates identity matrix. This question is about the algorithm rather than about the final result. In Q (kdb+ language) I can generate identity matrix using the following code: `float${x =\: x} til 12000 Python numpy…
Konstantin Spirin
  • 20,609
  • 15
  • 72
  • 90
0
votes
1 answer

Numpy/Keras: ValueError: could not broadcast input array from shape (7,5) into shape (7)

I am trying to convert some categorical features into one hot encodings for use in Keras. However, when I try to map these features, I end up receiving an error indicating the shapes are incompatible. Here is my code: import numpy import pandas from…
marked-down
  • 9,958
  • 22
  • 87
  • 150
0
votes
2 answers

python - broadcasting between arrays with the same 'outer' size

Numpy seems to have some (to me) unintuitive behaviour with broadcasting arrays. Let's say we have two arrays a = numpy.ones((2,2,3)) b = numpy.array([[1],[2]]) I would expect to be able to multiply these together, with the output being >>>…
Notso
  • 115
  • 6
0
votes
3 answers

ValueError: Dimensions must be equal, but are 4096 and 9 for 'mul'. Why no broadcasting here?

I have a very simple example: import tensorflow as tf import pdb number_features = tf.random_uniform((4096,22)) probs = number_features probs_L = probs[:,:3] probs_S1 = probs[:,3:12] probs_S2 = probs[:,12:22] confidence_no_digits =…
Derk
  • 1,385
  • 3
  • 19
  • 38
0
votes
1 answer

Using 2d numpy mask np.where to address a 3d numpy array (pythonic??)

Say I have an image array: raster.shape => (3,100,100) I generate a mask of all the places where red is saturated: mask = np.where(raster[0,:,:] == 255) I want to modify the slice of those locations... Which I can do like…
AAmes
  • 333
  • 1
  • 2
  • 11
0
votes
1 answer

Why can't I reshape numpy string arrays and ctype arrays?

Maybe there's a way around this that I'm missing. Long story short, I have a need for shared memory access, read only, of a large text file. Working with strings of course is necessary. So I'm trying to do this: import numpy from multiprocessing…
Will
  • 677
  • 3
  • 11
  • 21
0
votes
1 answer

Numpy broadcasting bitwise union upon no bitwise intersection

I am writing an algorithm that has a common scenario. I have two large arrays of integers. Call them k and j (because thats what I called them in my test code). I take each element of k, and I take it's union with each element of j, BUT ONLY IF…
0
votes
0 answers

Get different result from broadcasting when changing dimension

I'm using sum(self.eval_h(self.x) * self.w.reshape(1, self.n)) / sum(self.w) to do a dot product and where eval_h is defined as def eval_h(self, x): """ evaluate h """ if "h_params" in self.kwargs: if x.ndim == 1: …
monotonic
  • 394
  • 4
  • 20
0
votes
1 answer

Vectorisation of numpy.linalg.lstsq

I have two sets of points (A1, A2, B1, B2) for which I want to calculate the affine transformation (from A1 to B1, from A2 to B2). Using numpy.linalg.lstsq this is very straightforward for a single case: A1 = [[100 0 0] [ 0 100 0] …
0
votes
2 answers

Numpy: Can you use broadcasting to replace values by row?

I have a M x N matrix X and a 1 x N matrix Y. What I would like to do is replace any 0-entry in X with the appropriate value from Y based on its column. So if X = np.array([[0, 1, 2], [3, 0, 5]]) and Y = np.array([10, 20, 30]) The desired end…
David R
  • 994
  • 1
  • 11
  • 27
0
votes
1 answer

Unable to divide a matrix and vector in keras

The matrix a has shape (4,3) and z has shape (4,). My intent is I want to divide every 3 dim vector in a with scalar in z. Consider the below example: Input: a = [[1,1,1], [2,2,2], [2,2,2], [5,5,5]] z = [10,10,10,5] Expected…
claudius
  • 1,112
  • 1
  • 10
  • 23
0
votes
2 answers

ValueError: operands could not be broadcast together with shapes (2501,201) (2501,)

I am new to python so please be nice. I am trying to compare two Numpy arrays with the np.logical_or function. When I run the below code an error appears on the Percentile = np.logical_or(data2 > Per1, data2 < Per2) line stating ValueError:…
0
votes
1 answer

broadcasting arrays in numpy

I got an array and reshaped it to the following dimentions: (-1,1,1,1) and (-1,1): Array A: [-0.888788523827 0.11842529285 0.319928774626 0.319928774626 0.378755429421 1.225877519716 …
Monica
  • 1,030
  • 3
  • 17
  • 37
0
votes
1 answer

Remove NaN from 2D numpy array

For example, if I have the 2D array as follows. [[1,2,3,NAN], [4,5,NAN,NAN], [6,NAN,NAN,NAN] ] The desired result is [[1,2,3], [4,5], [6] ] How should I transform? I find using x = x[~numpy.isnan(x)] can only generate [1,2,3,4,5,6], which…
XIN LIU
  • 87
  • 1
  • 9