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

How can I convert boolean values from np.diff() to the actual values

I have a numpy array of np.shape=(n,) I am attempting to iterate through each value of the array and subtract the 2nd value from the 1st, then see if the difference is greater than 1. So, array[1]-array[0] > 1 ?, then array[2]-array[1] > 1 ?. I read…
nukenine
  • 195
  • 1
  • 4
  • 12
-1
votes
1 answer

Repeat elements from one array based on another

Given a = np.array([1,2,3,4,5,6,7,8]) b = np.array(['a','b','c','d','e','f','g','h']) c = np.array([1,1,1,4,4,4,8,8]) where a & b 'correspond' to each other, how can I use c to slice b to get d which 'corresponds' to c: d =…
-1
votes
2 answers

How to product element-wise a 2-d numpy array into 3-d over the second dimension of the latter?

I have a numpy matrix b = np.array([[1,0,1,0],[0,0,0,1]]) and I want to product it element-wise into a 3-dim array a = np.array([[[1,2,3,4], [5,6,7,8], [9,10,11,12]], [[13,14,15,16], [17,18,19,20], [21,22,23,24]]]) for each index on the second…
mike
  • 1
  • 1
-1
votes
1 answer

Matrix/array operations inside a python function

I am trying to use the content of a python function to make a contour plot. For example I try: import numpy as np import matplotlib.mlab as mlab import matplotlib.pyplot as plt # Building my equation from a series of matrix operation def func_M(X,…
Mac
  • 991
  • 3
  • 11
  • 22
-1
votes
1 answer

Compute distances between 2 dataframes based on boolean matrix as a mask

I have 2 dataframes where columns are features and rows are different items. import pandas as pd import numpy as np import random random.seed(0) data1 = {'x':random.sample(range(1,100), 4), 'y':random.sample(range(1,100), 4),…
-1
votes
1 answer

Adding a border of 1 to a list in python

import numpy as np s=[[0, 0, 0, 0,0,0,0,0], [1, 1, 1, 1, 1,1,1, 0],[1, 1, 1, 1, 1,1,1, 0],[1,0,0,0,0,1,1,0], [0,1,1,1,0,0,0,0],[0,1,1,1,1,1,1,1],[0,1,1,1,1,1,1,1],[0, 0, 0, 0, 0,…
Who am I
  • 109
  • 6
-1
votes
1 answer

Create a function to calculate an equation from a dataframe in pandas

I have a dataframe as shown below Inspector_ID Sector Waste Fire Traffic 1 A 7 2 1 1 B 0 0 0 1 C 18 2 0 2…
Danish
  • 2,719
  • 17
  • 32
-1
votes
1 answer

explain the code in numpy how its working

this is the code that i run : labels=[0,1,1,0,2,1,1,1,0,0] labels_ = np.zeros((10, 3)) labels_ the above code gives the output : array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0.,…
-1
votes
1 answer

Writing columns into a NumPy array

I have initialized a numpy array with zeros for memory management, and I am trying to write data into each column within the loop. I come from a Matlab background, so my code goes something like: myArray = np.zeros((250000, 100)) for i in…
-1
votes
1 answer

Calculating numpy array according to another numpy array values

I need to calculate array Z having array D (only using indexing, slicing and broadcasting, NO LOOPS): D = [0, 0, 0, 0, 12, 36, 24, 24, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 36] Z = [nan, nan, nan, nan, 12., 14.4, 15.36, 16.224, 16.224, 16.224, 16.224,…
-1
votes
1 answer

Compute distances between 2 matrix of vector

I have a problem with Numpy broadcasting between two matrix. I need to compute the euclidean distance between 2 matrix for a knn classifier. I have already done it with two loop and one loop but it too slow. I'm looking for doing it with Numpy…
Zadbar
  • 11
  • 1
-1
votes
1 answer

Is there a difference between adding a scalar to a vector inside a for loop and outside it, using numpy?

I was trying to take advantage of the Broadcasting property of Python while replacing the for loop of this snippet: import numpy as np B = np.random.randn(10,1) k = 25 for i in range(len(B)): B[i][0]= B[i][0] + k with this: for i in…
Ambareesh
  • 324
  • 1
  • 4
  • 18
-1
votes
1 answer

Numpy dot throws error

I have two numpy arrays of the same dimension . When I tried to use dot product on them, I am getting "shapes not aligned" error. import numpy as np A = np.array([[2,4,6]]) Y = np.array([[1,0,1]]) np.dot(Y,A) ValueError: shapes (1,3) and (1,3)…
Abin John Thomas
  • 159
  • 2
  • 13
-1
votes
1 answer

numpy type conversion during broadcast

I'm using the numpy broadcast function to map a set of values over a set of coordinates. The values can be of heterogeneous type, including primitives. My problem is that the broadcast function is converting the primitive types under certain…
MarkNS
  • 3,811
  • 2
  • 43
  • 60
-1
votes
1 answer

how to add elements of a list

I have a list(Y) which contains numpy arrays of different length.The list contain more than 50 element(I just took five for testing).The shape of the list is 5 and I can print the shape of each element as shown below and the outputs are in the…
dm5
  • 350
  • 1
  • 6
  • 18
1 2 3
58
59