numpy-broadcasting

What is the precise specification of `broadcast` in ND4j / DL4j?

廉价感情. 提交于 2020-03-05 03:13:49
问题 What is the precise definition of INDArray.broadcast(long...) in ND4j? The description of both methods only says "Broadcasts this ndarray to be the specified shape", and does not explain the requirements for acceptable shapes to be broadcast. The following code (meant to broadcast an ndarray to a shape of an extra first dimension of length 3) does not work: INDArray array = Nd4j.create(new double[] {1.,2.,3.,4.}, new int[] {2,2}, 'c'); System.out.println("array: " + array); System.out.println

Assign a sequence at irregular intervals in 1D array - Python / NumPy

狂风中的少年 提交于 2020-01-30 08:41:51
问题 I have a sequence of numbers that I would like to insert into a larger array at irregular intervals: dates = np.zeros(15) pattern = np.arange(3) + 1 starts = [2, 6, 11] for start in starts: dates[start:start + pattern.size] = pattern > [0 0 1 2 3 0 1 2 3 0 0 1 2 3 0] I have to do this many (100M+) times on large (10K+) arrays, so I'm looking for a way to do this with broadcasting or another efficient method, avoiding a for loop. pattern will always be a range if that helps. 回答1: Construct a

numpy - einsum notation: dot product of a stack of matrices with stack of vectors

耗尽温柔 提交于 2020-01-17 01:18:34
问题 I want to multiply an n-dim stack of m* m matrices by an n-dim stack of vectors (length m), so that the resulting m*n array contains the result of the dot product of the matrix and vector in the nth entry: vec1=np.array([0,0.5,1,0.5]); vec2=np.array([2,0.5,1,0.5]) vec=np.transpose(n.stack((vec1,vec2))) mat = np.moveaxis(n.array([[[0,1,2,3],[0,1,2,3],[0,1,2,3],[0,1,2,3]],[[-1,2.,0,1.],[0,0,-1,2.],[0,1,-1,2.],[1,0.1,1,1]]]),0,2) outvec=np.zeros((4,2)) for i in range(2): outvec[:,i]=np.dot(mat[:

broadcasting arrays in numpy

我与影子孤独终老i 提交于 2020-01-16 08:50:10
问题 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 3.830653798838] A.reshape(-1,1,1,1): [[[[-0.888788523827]]] [[[ 0.11842529285 ]]] [[[ 0.319928774626]]] [[[ 0.319928774626]]] [[[ 0.378755429421]]] [[[ 1.225877519716]]] [[[ 3.830653798838]]]] A.reshape(-1,1): [[-0.888788523827] [ 0.11842529285 ] [ 0.319928774626] [ 0.319928774626] [ 0.378755429421] [ 1

How to initialize Numpy array of list objects

青春壹個敷衍的年華 提交于 2020-01-04 06:00:10
问题 I'm trying to create a numpy array that looks like array([[list([]), list([])], [list([]), list([])], [list([]), list([])]], dtype=object) This array has shape (3,2) . However, whenever I do np.array([[list(), list()], [list(), list()], [list(), list()]]) I end up getting array([], shape=(3, 2, 0), dtype=float64) How do I solve this? 回答1: You could use the following: np.frompyfunc(list, 0, 1)(np.empty((3,2), dtype=object)) We first turn list into a ufunc that takes no arguments and returns a

Np array dot product of vector and array

 ̄綄美尐妖づ 提交于 2020-01-04 02:19:10
问题 I have a problem in understanding the working behind the numpy dot function and broadcasting.Below is the snippet I am trying to understand a=np.array([[1,2],[3,5]]) if we check the shape of a a.shape it will be (2,2) b=np.array([3,6]) and b.shape is (2,) Question1: is b column vector or row vector? while providing input it seems b is row vector but then shape shows it as a column vector having 2 rows.What is the fault in my understanding? now if do a.dot(b) it result in array([15,39])

Numpy Broadcasting

只愿长相守 提交于 2019-12-31 04:04:09
问题 What happens when i make this operation in Numpy? a = np.ones([500,1]) b = np.ones([5000,])/2 c = a + b # a.shape (500,1) # b.shape (5000, ) # c.shape (500, 5000) I'm having a hard time to figure out what is actually happening in this broadcast. 回答1: Numpy assumes for 1 dimensional arrays row vectors, so your summation is indeed between shapes (500, 1) and (1, 5000), which leads to matrix summation. Since this is not very clear, you should extend your dimensions explicitly: >>> np.arange(5)[:

Vectorized NumPy linspace for multiple start and stop values

会有一股神秘感。 提交于 2019-12-27 12:06:10
问题 I need to create a 2D array where each row may start and end with a different number. Assume that first and last element of each row is given and all other elements are just interpolated according to length of the rows In a simple case let's say I want to create a 3X3 array with same start at 0 but different end given by W below: array([[ 0., 1., 2.], [ 0., 2., 4.], [ 0., 3., 6.]]) Is there a better way to do this than the following: D=np.ones((3,3))*np.arange(0,3) D=D/D[:,-1] W=np.array([2,4

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

自闭症网瘾萝莉.ら 提交于 2019-12-25 08:59:32
问题 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 # resize | outside function ls.resize(ls.shape[0]//2,2) print(ls) >> [[ 1. 2.] [ 3. 4.] . . . [ 97. 98.] [ 99. 100.]] I do not understand my error when trying to throw the routine in a function. My attempt is as follows: # resize | inside function def

Numpy : Grouping/ binning values based on associations

↘锁芯ラ 提交于 2019-12-24 12:17:29
问题 Forgive me for a vague title. I honestly don't know which title will suit this question. If you have a better title, let's change it so that it will be apt for the problem at hand. The problem. Let's say result is a 2D array and values is a 1D array. values holds some values associated with each element in result . The mapping of an element in values to result is stored in x_mapping and y_mapping . A position in result can be associated with different values. Now, I have to find the sum of