numpy-broadcasting

Reoder the columns of each row of a numpy array based on another numpy array

送分小仙女□ 提交于 2019-12-24 10:27:10
问题 We have a main array called main_arr , and we want to transform it into another array called result with the same size, using a guide_arr , again with the same size: import numpy as np main_arr = np.array([[3, 7, 4], [2, 5, 6]]) guide_arr = np.array([[2, 0, 1], [0, 2, 1]]) result = np.zeros(main_arr.shape) we need the result to equal to: if np.array_equal(result, np.array([[7, 4, 3], [2, 6, 5]])): print('success!') How should we use guide_arr ? guide_arr[0,0] is 2, meaning that result[0,2] =

Element wise dot product of matrices and vectors [duplicate]

喜欢而已 提交于 2019-12-23 22:22:16
问题 This question already has an answer here : python: Multiply slice i of a matrix stack by column i of a matrix efficiently (1 answer) Closed 2 years ago . There are really similar questions here, here, here, but I don't really understand how to apply them to my case precisely. I have an array of matrices and an array of vectors and I need element-wise dot product. Illustration: In [1]: matrix1 = np.eye(5) In [2]: matrix2 = np.eye(5) * 5 In [3]: matrices = np.array((matrix1,matrix2)) In [4]:

Numpy: Finding minimum and maximum values from associations through binning

限于喜欢 提交于 2019-12-23 19:53:22
问题 Prerequisite This is a question derived from this post. So, some of the introduction of the problem will be similar to that post. 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 minimum and maximum of the values grouped by associations. An

Numpy 3d array indexing

时光毁灭记忆、已成空白 提交于 2019-12-22 10:45:51
问题 I have a 3d numpy array ( n_samples x num_components x 2 ) in the example below n_samples = 5 and num_components = 7. I have another array ( indices ) which is the selected component for each sample which is of shape ( n_samples ,). I want to select from the data array given the indices so that the resulting array is n_samples x 2 . The code is below: import numpy as np np.random.seed(77) data=np.random.randint(low=0, high=10, size=(5, 7, 2)) indices = np.array([0, 1, 6, 4, 5]) #how can I

Numpy: Replace every value in the array with the mean of its adjacent elements

烈酒焚心 提交于 2019-12-22 06:58:10
问题 I have an ndarray, and I want to replace every value in the array with the mean of its adjacent elements. The code below can do the job, but it is super slow when I have 700 arrays all with shape (7000, 7000) , so I wonder if there are better ways to do it. Thanks! a = np.array(([1,2,3,4,5,6,7,8,9],[4,5,6,7,8,9,10,11,12],[3,4,5,6,7,8,9,10,11])) row,col = a.shape new_arr = np.ndarray(a.shape) for x in xrange(row): for y in xrange(col): min_x = max(0, x-1) min_y = max(0, y-1) new_arr[x][y] = a

Use numpy.frompyfunc to add broadcasting to a python function with argument

♀尐吖头ヾ 提交于 2019-12-19 10:23:03
问题 From an array like db (which will be approximately (1e6, 300) ) and a mask = [1, 0, 1] vector, I define the target as a 1 in the first column. I want to create an out vector that consists of ones where the corresponding row in db matches the mask and target==1 , and zeros everywhere else. db = np.array([ # out for mask = [1, 0, 1] # target, vector # [1, 1, 0, 1], # 1 [0, 1, 1, 1], # 0 (fit to mask but target == 0) [0, 0, 1, 0], # 0 [1, 1, 0, 1], # 1 [0, 1, 1, 0], # 0 [1, 0, 0, 0], # 0 ]) I

How to keep numpy from broadcasting when creating an object array of different shaped arrays

被刻印的时光 ゝ 提交于 2019-12-17 20:23:44
问题 I try to store a list of different shaped arrays as a dtype=object array using np.save (I'm aware I could just pickle the list but I'm really curious how to do this). If I do this: import numpy as np np.save('test.npy', [np.zeros((2, 2)), np.zeros((3,3))]) it works. But this: np.save('test.npy', [np.zeros((2, 2)), np.zeros((2,3))]) Gives me an error: ValueError: could not broadcast input array from shape (2,2) into shape (2) I guess np.save converts the list into an array first, so I tried: x

Numpy dot product of a 4D array with its transpose fails

扶醉桌前 提交于 2019-12-14 03:56:13
问题 For a 4D array A with dimensions of (60,64,2,2), need to calculate the dot product with its transpose A_t. A_t is of dimension(2,2,64,60). Below is what I do. A_t = np.transpose(A) A_At = A_t.dot(A) The dot product throws an error ValueError: shapes (2,2,64,60) and (60,64,2,2) not aligned: 60 (dim 3) != 2 (dim 2) Am I taking the transpose incorrectly? I have also tried converting the individual arrays to numpy matrices(even though not recommended as per several posts) and then computing the

2D Numpy Array Fancy Indexing + Masking

风流意气都作罢 提交于 2019-12-13 07:04:11
问题 I have: import numpy as np a = np.array([[ 4, 99, 2], [ 3, 4, 99], [ 1, 8, 7], [ 8, 6, 8]]) Why is a[[True, True, False, False], [1,2]] Equal to array([99, 99]) And not array([99, 2], [4, 99]) Since I am selecting the first two rows using a boolean mask and the 2nd and 3rd columns using fancy indexing? Especially since calling a[[True, True, False, False],:][:, [1,2]] gives me my expected result. Im guessing its some sort of broadcasting rule but it isn't apparent to me. Thanks! 回答1: A

Vectorization and matrix multiplication by scalars

杀马特。学长 韩版系。学妹 提交于 2019-12-13 03:19:12
问题 I am new to python/numpy. I need to do the following calculation: for an array of discrete times t, calculate $e^{At}$ for a $2\times 2$ matrix $A$ What I did: def calculate(t_,x_0,v_0,omega_0,c): # define A a_11,a_12, a_21, a_22=0,1,-omega_0^2,-c A =np.matrix([[a_11,a_12], [a_21, a_22]]) print A # use vectorization temps = np.array(t_) A_ = np.array([A for k in range (1,n+1,1)]) temps*A_ x_=scipy.linalg.expm(temps*A) v_=A*scipy.linalg.expm(temps*A) return x_,v_ n=10 omega_0=1 c=1 x_0=1 v_0=1