numpy-slicing

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

Replace elements in numpy array avoiding loops

好久不见. 提交于 2021-02-07 03:00:16
问题 I have a quite large 1d numpy array Xold with given values. These values shall be replaced according to the rule specified by a 2d numpy array Y: An example would be Xold=np.array([0,1,2,3,4]) Y=np.array([[0,0],[1,100],[3,300],[4,400],[2,200]]) Whenever a value in Xold is identical to a value in Y[:,0], the new value in Xnew should be the corresponding value in Y[:,1]. This is accomplished by two nested for loops: Xnew=np.zeros(len(Xold)) for i in range(len(Xold)): for j in range(len(Y)): if

Replace elements in numpy array avoiding loops

瘦欲@ 提交于 2021-02-07 02:59:44
问题 I have a quite large 1d numpy array Xold with given values. These values shall be replaced according to the rule specified by a 2d numpy array Y: An example would be Xold=np.array([0,1,2,3,4]) Y=np.array([[0,0],[1,100],[3,300],[4,400],[2,200]]) Whenever a value in Xold is identical to a value in Y[:,0], the new value in Xnew should be the corresponding value in Y[:,1]. This is accomplished by two nested for loops: Xnew=np.zeros(len(Xold)) for i in range(len(Xold)): for j in range(len(Y)): if

Replace elements in numpy array avoiding loops

走远了吗. 提交于 2021-02-07 02:59:35
问题 I have a quite large 1d numpy array Xold with given values. These values shall be replaced according to the rule specified by a 2d numpy array Y: An example would be Xold=np.array([0,1,2,3,4]) Y=np.array([[0,0],[1,100],[3,300],[4,400],[2,200]]) Whenever a value in Xold is identical to a value in Y[:,0], the new value in Xnew should be the corresponding value in Y[:,1]. This is accomplished by two nested for loops: Xnew=np.zeros(len(Xold)) for i in range(len(Xold)): for j in range(len(Y)): if

Numpy multidimensional indexing for np.ufunc.at and np.ix_

拜拜、爱过 提交于 2021-01-29 10:01:00
问题 I would like to know how I can take index from an array and multiply with another array. I have two 4d arrays and one 2d index array: base = np.ones((2, 3, 5, 5)) to_multiply = np.arange(120).reshape(2, 3, 4, 5) index = np.array([[0, 2, 4, 2], [0, 3, 3, 2]]) The row index of the index array corresponds to the 1st dimension of base and to_multiply, and the value of the index array corresponds to the 3rd dimension of base. I want to take the slice from base according to the index and multiply

Indexing different sized ranges in a 2D numpy array using a Pythonic vectorized code

有些话、适合烂在心里 提交于 2021-01-27 04:07:17
问题 I have a numpy 2D array, and I would like to select different sized ranges of this array, depending on the column index. Here is the input array a = np.reshape(np.array(range(15)), (5, 3)) example [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11] [12 13 14]] Then, list b = [4,3,1] determines the different range sizes for each column slice, so that we would get the arrays [0 3 6 9] [1 4 7] [2] which we can concatenate and flatten to get the final desired output [0 3 6 9 1 4 7 2] Currently, to perform

Indexing different sized ranges in a 2D numpy array using a Pythonic vectorized code

落爺英雄遲暮 提交于 2021-01-27 04:07:13
问题 I have a numpy 2D array, and I would like to select different sized ranges of this array, depending on the column index. Here is the input array a = np.reshape(np.array(range(15)), (5, 3)) example [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11] [12 13 14]] Then, list b = [4,3,1] determines the different range sizes for each column slice, so that we would get the arrays [0 3 6 9] [1 4 7] [2] which we can concatenate and flatten to get the final desired output [0 3 6 9 1 4 7 2] Currently, to perform

Indexing different sized ranges in a 2D numpy array using a Pythonic vectorized code

扶醉桌前 提交于 2021-01-27 04:07:07
问题 I have a numpy 2D array, and I would like to select different sized ranges of this array, depending on the column index. Here is the input array a = np.reshape(np.array(range(15)), (5, 3)) example [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11] [12 13 14]] Then, list b = [4,3,1] determines the different range sizes for each column slice, so that we would get the arrays [0 3 6 9] [1 4 7] [2] which we can concatenate and flatten to get the final desired output [0 3 6 9 1 4 7 2] Currently, to perform

Slice and change numpy 2D array with list of column indices different for each row [duplicate]

江枫思渺然 提交于 2020-01-11 14:40:54
问题 This question already has answers here : Indexing one array by another in numpy (3 answers) Closed yesterday . I would like to slice a 2D numpy array using a list of column indices. The difficulty is the column indices are different for each row. For example: x = np.array([[0, 1, 2] [3, 4, 5] [6, 7, 8]]) and I have a list of column indices as indices = [[0, 1], [2, 1], [2, 2]] which means I would like to get column [0,1] for row 0, column [2, 1] for row 1, and column [2, 2] for row 2. The

Performance decreases with increasing nesting of array elements

耗尽温柔 提交于 2019-12-11 10:53:33
问题 A short note: This question relates to another I asked previously, but since asking multiple questions within a single Q&A is concidered bad SO-style I splitted it up. Setup I have the following two implementations of a matrix-calculation: The first implementation uses a matrix of shape (n, m) and the calculation is repeated in a for-loop for repetition -times: import numpy as np def foo(): for i in range(1, n): for j in range(1, m): _deleteA = ( matrix[i, j] + #some constants added here )