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 in scipy t test_ind

I have following csv file: SRA ID ERR169499 ERR169498 ERR169497 Label 1 0 1 TaxID PRJEB3251_ERR169499 PRJEB3251_ERR169499 PRJEB3251_ERR169499 333046 0.05 0.99 …
K.S
  • 113
  • 13
0
votes
0 answers

Unusal Function

I am working on an interesting project, which requires I use the huber function on a set of variables and just the square function on the other variables. For example, I have a vector function looking like this f(x, z) = (H(x),S(z)), where H(x) is…
Ed Turner
  • 1
  • 2
0
votes
1 answer

Python Broadcasting: Use 1D vector as index in one dimension of 2d array

Suppose I have a 2D array, table of shape m x n. Suppose I have a 1D array, col_indices of shape m, and range [0,n) in terms of the values. Finally, suppose I have a value array, vals of shape u, indexed by i. In pseudo code, I would like to…
Chris
  • 28,822
  • 27
  • 83
  • 158
0
votes
1 answer

Optimization for making a null class in a one-hot pixel-wise label

I'm preparing data for an image segmentation model. I have 5 classes per pixel that do not cumulatively cover the entire image so I want to create a 'null' class as the 6th class. Right now I have a one-hot encoded ndarray and a solution that makes…
0
votes
1 answer

python: broadcast sliced np.array assignment to any number of dimensions

I have np.arrays C, R and S of shapes (?, d), (?, n) and (?, d) respectively; where d<=n and the question mark represents any number of matching dimensions. Now I would like to do the following assignment (this is of course not proper python code,…
Tashi Walde
  • 163
  • 4
0
votes
1 answer

Advanced indexing in 2d tensor in pytorch

I have a 2d tensor X. and two lists of indexes that is first index and second call a and b. I want to do X[a[i],b[i]] = 0 for i in range(len(a)) How can I do this. If i directly do X[a,b] the error is IndexError: The advanced indexing objects could…
0
votes
1 answer

How to broadcast a row to a column in Python NumPy?

I have a row vector R and a column vector C. I want to add them to create an array A with height equal to size of R and width equal to size of C as follows: A[i,j] = R[i] + C[j] What's the most efficient way of doing this?
0
votes
0 answers

Declaring vectorized ufunc with numba, signature error: 'Float' instance not allowed

I'm trying to write a function that can be vectorized by numba to act as a ufunc on a set of arrays: from numba import float32, float64, vectorize @vectorize([float32, float32, float32, float32(float32, float32, float32, float32, float32, float32,…
James Adams
  • 8,448
  • 21
  • 89
  • 148
0
votes
0 answers

unwanted broadcasting in numpy

I want to execute for b, w in zip(self.biases, self.weights): a= sigmoid(np.dot(w, a) + b) where, a is a tuple of length 784, self.biases contains a list of matrices of dimensions (15, 1) and (10, 1), self.weights contains a list of matrices…
0
votes
1 answer

Perform mulitple comparisons (interval) in a Numpy array in a single pass

I have a situation similar to the "Count values in a certain range" question, but instead of a column-vector I have a matrix intervals with two columns [upper, lower] and another column vector true_values. I want to check whether the values in the…
Bar
  • 2,736
  • 3
  • 33
  • 41
0
votes
1 answer

Indexing numpy array with bounds checking

Suppose I have some random numpy array: >>> x = np.random.randn(10, 4, 4) So this can be viewed as 10 4x4 arrays. Now, I also have some coordinates with respect to each of these arrays: >>> coords.shape (10, 4, 4, 2) and a particular element of…
MadMonty
  • 807
  • 2
  • 7
  • 15
0
votes
1 answer

Dimension mismatch with numpy broadcasting

I am having trouble with numpy broadcasting. When I try to do a matrix-matrix multiplication, I get the following error: "operands could not be broadcast together with shapes (2,6) (2,7)" Here is the code that won't run: import numpy as np pointsX…
0
votes
2 answers

How to slice image by broadcasting slices? Error: 'only integer scalar arrays can be converted to a scalar index' in python?

I have a simple task: I have an image and an array of points. For each point I want to slice boxes out of the image. I can do this in a for loop, but for thousands of points it is very slow, so I need to do this without loops. I'm trying to…
john k
  • 6,268
  • 4
  • 55
  • 59
0
votes
1 answer

Sum of indexed numpy arrays

Suppose I have an array of indices: I=[0 1 2 3 0 3] And array of values: W=[w0, w1, w2, w3] How can I create an array X using vectorized numpy expressions produced as follows: X = np.zeros(I.max() + 1) for i in range(len(I)): X[I[i]] +=…
DikobrAz
  • 3,557
  • 4
  • 35
  • 53
0
votes
1 answer

numpy ravel on inconsistent dimensional object

I have below code that allows me to iterate over a flattened list and maps the updates back to the structured array - a = np.asarray([1,2,3]) b = np.asarray([4,5,6]) c = np.asarray([a, b]) print c r = c.ravel() print r r[1] = 10 print c setting…
massphoenix
  • 199
  • 1
  • 2
  • 11