array-broadcasting

Replacing for loops with function call inside with broadcasting/vectorized solution

a 夏天 提交于 2021-02-11 13:26:41
问题 Problem: When using broadcasting, rather than broadcasting scalars to match the arrays, the vectorized function is instead, for some reason, shrinking the arrays to scalars. MWE: Below is a MWE. It contains a double for loop. I am having trouble writing faster code that does not use the for loops, but instead, uses broadcasting/vectorized numpy. import numpy as np def OneD(x, y, z): ret = np.exp(x)**(y+1) / (z+1) return ret def ThreeD(a,b,c): value = OneD(a[0],b[0], c) value *= OneD(a[1],b[1]

Replacing for loops with function call inside with broadcasting/vectorized solution

不羁的心 提交于 2021-02-11 13:26:09
问题 Problem: When using broadcasting, rather than broadcasting scalars to match the arrays, the vectorized function is instead, for some reason, shrinking the arrays to scalars. MWE: Below is a MWE. It contains a double for loop. I am having trouble writing faster code that does not use the for loops, but instead, uses broadcasting/vectorized numpy. import numpy as np def OneD(x, y, z): ret = np.exp(x)**(y+1) / (z+1) return ret def ThreeD(a,b,c): value = OneD(a[0],b[0], c) value *= OneD(a[1],b[1]

Broadcasting/Vectorizing inner and outer for loops in python/NumPy

允我心安 提交于 2021-02-11 06:30:32
问题 Purpose I have turned a double for loop into a single for loop using vectorization . I would like to now get rid of the last loop . I want to slice an Nx3 array of coordinates and calculate distances between the sliced portion and the remaining portion without using a for loop . Two cases (1) the slice is always 3x3 . (2) the slice is variable i.e., Mx3 where M is always significantly smaller than N Vectorizing the interaction of 1 row of the slice interacting with the remainder is

Numpy symmetric 4D matrix construction

感情迁移 提交于 2021-02-10 06:38:15
问题 I would like to construct an array with the following structure: A[i,j,i,j,] = B[i,j] with all other entries 0: A[i,j,l,k]=0 # (i,j) =\= (l,k) I.e. if I have the B matrix constructed how can I create the matrix A , preferably in a vectorized manner. Explicitly, let B = [[1,2],[3,4]] Then: A[1,1,:,:]=[[1,0],[0,0]] A[1,2,:,:]=[[0,2],[0,0]] A[2,1,:,:]=[[0,0],[3,0]] A[2,2,:,:]=[[0,0],[0,4]] 回答1: We can use an open grid to assign to A broadcasting the indexing arrays across the axes: B = np.array(

Numpy symmetric 4D matrix construction

廉价感情. 提交于 2021-02-10 06:37:07
问题 I would like to construct an array with the following structure: A[i,j,i,j,] = B[i,j] with all other entries 0: A[i,j,l,k]=0 # (i,j) =\= (l,k) I.e. if I have the B matrix constructed how can I create the matrix A , preferably in a vectorized manner. Explicitly, let B = [[1,2],[3,4]] Then: A[1,1,:,:]=[[1,0],[0,0]] A[1,2,:,:]=[[0,2],[0,0]] A[2,1,:,:]=[[0,0],[3,0]] A[2,2,:,:]=[[0,0],[0,4]] 回答1: We can use an open grid to assign to A broadcasting the indexing arrays across the axes: B = np.array(

A broadcasting issue involving where to put the padding

时光毁灭记忆、已成空白 提交于 2021-02-04 15:41:09
问题 Introduction I have a function func which is vectorizable, and I vectorize it using np.frompyfunc . Rather than using a nested for loop, I want to call it only once, and thus I'll need to pad the inputs with np.newaxis 's. My goal is to get rid of the two nested for loops and use the numpy.array broadcasting feature instead. Here is the MWE for loops (I want to get rid of the for loops and instead pad the variables c_0 , c_1 , rn_1 , rn_2 , and factor when calling myfunc . MWE of the problem

How to convert array into special items of structured array and revert it back?

 ̄綄美尐妖づ 提交于 2021-01-29 07:11:50
问题 I want to perform some numpy methods on items of structured array instead of numbers. So, for example, while working with array of integers of shape (4, 3), I need to convert it to array of items of length 3 and perform some operations as it were a single one dimensional array of shape (4,). These conversions itself, unfortunately, looks really complicated for me. Let's take another example: n, m, r = 2, 3, 4 array = np.arange(n*m).reshape((n,m)) dt = np.dtype(','.join('i'*m)) arr1 = np.array

Broadcasting function calls in np.array

こ雲淡風輕ζ 提交于 2021-01-27 17:06:29
问题 I am trying creating an NumPy array filled with an object, and I was wondering if there was a way I could broadcast to the entire array for each object to do something. Code: class player: def __init__(self,num = 5): self.num = num def printnum(): print(self.num) ... objs = np.array([player(5),player(6)],dtype=Object) objs.printnum() As it stands this returns an error. I have tried changing the dtype to: _object as per the manual, but nothing seems to work. 回答1: A numpy array of objects does

Why the following operands could not be broadcasted together?

安稳与你 提交于 2020-12-30 08:58:14
问题 The arrays are of following dimensions: dists : (500,5000) train : (5000,) test :(500,) Why does the first two statements throw an error whereas the third one works fine? dists += train + test Error: ValueError: operands could not be broadcast together with shapes (5000,) (500,) dists += train.reshape(-1,1) + test.reshape(-1,1) Error: ValueError: operands could not be broadcast together with shapes (5000,1) (500,1) dists += train + test.reshape(-1,1) This works fine! Why does this happen? 回答1