numpy-slicing

Numpy summation with sliding window is really slow

时间秒杀一切 提交于 2019-12-02 18:15:43
问题 Code: shape = np.array([6, 6]) grid = np.array([x.ravel() for x in np.meshgrid(*[np.arange(x) for i, x in enumerate(shape)], indexing='ij')]).T slices = [tuple(slice(box[i], box[i] + 2) for i in range(len(box))) for box in grid] score = np.zeros((7,7,3)) column = np.random.randn(36, 12) #just for example column >> array([[ 0, 1, 2, 3, ... 425, 426, 427, 428, 429, 430, 431]]) column = column.reshape((16, 3, 3, 3)) for i, window in enumerate(slices): score[window] += column[i] score >> array([[

Numpy summation with sliding window is really slow

不羁岁月 提交于 2019-12-02 09:37:41
Code: shape = np.array([6, 6]) grid = np.array([x.ravel() for x in np.meshgrid(*[np.arange(x) for i, x in enumerate(shape)], indexing='ij')]).T slices = [tuple(slice(box[i], box[i] + 2) for i in range(len(box))) for box in grid] score = np.zeros((7,7,3)) column = np.random.randn(36, 12) #just for example column >> array([[ 0, 1, 2, 3, ... 425, 426, 427, 428, 429, 430, 431]]) column = column.reshape((16, 3, 3, 3)) for i, window in enumerate(slices): score[window] += column[i] score >> array([[[0.000e+00, 1.000e+00, 2.000e+00], [3.000e+01, 3.200e+01, 3.400e+01], [9.000e+01, 9.300e+01, 9.600e+01]

Why using an array as an index changes the shape of a multidimensional ndarray?

别说谁变了你拦得住时间么 提交于 2019-11-27 15:19:12
I have a 4-D NumPy array, with axis say x,y,z,t. I want to take slice corresponding to t=0 and to permute the order in the y axis. I have the following import numpy as np a = np.arange(120).reshape(4,5,3,2) b = a[:,[1,2,3,4,0],:,0] b.shape I get (5, 4, 3) instead of (4,5,3). When, instead, I enter aa = a[:,:,:,0] bb = aa[:,[1,2,3,4,0],:] bb.shape I get the expected (4,5,3). Can someone explain why does the first version swap the first two dimensions? As @hpaulj mentioned in the comments, this behaviour is because of mixing basic slicing and advanced indexing : a = np.arange(120).reshape(4,5,3

Selecting specific rows and columns from NumPy array

一世执手 提交于 2019-11-27 00:08:19
I've been going crazy trying to figure out what stupid thing I'm doing wrong here. I'm using NumPy, and I have specific row indices and specific column indices that I want to select from. Here's the gist of my problem: import numpy as np a = np.arange(20).reshape((5,4)) # array([[ 0, 1, 2, 3], # [ 4, 5, 6, 7], # [ 8, 9, 10, 11], # [12, 13, 14, 15], # [16, 17, 18, 19]]) # If I select certain rows, it works print a[[0, 1, 3], :] # array([[ 0, 1, 2, 3], # [ 4, 5, 6, 7], # [12, 13, 14, 15]]) # If I select certain rows and a single column, it works print a[[0, 1, 3], 2] # array([ 2, 6, 14]) # But

Why using an array as an index changes the shape of a multidimensional ndarray?

萝らか妹 提交于 2019-11-26 11:33:54
问题 I have a 4-D NumPy array, with axis say x,y,z,t. I want to take slice corresponding to t=0 and to permute the order in the y axis. I have the following import numpy as np a = np.arange(120).reshape(4,5,3,2) b = a[:,[1,2,3,4,0],:,0] b.shape I get (5, 4, 3) instead of (4,5,3). When, instead, I enter aa = a[:,:,:,0] bb = aa[:,[1,2,3,4,0],:] bb.shape I get the expected (4,5,3). Can someone explain why does the first version swap the first two dimensions? 回答1: As @hpaulj mentioned in the comments,

Selecting specific rows and columns from NumPy array

孤者浪人 提交于 2019-11-26 08:00:16
问题 I\'ve been going crazy trying to figure out what stupid thing I\'m doing wrong here. I\'m using NumPy, and I have specific row indices and specific column indices that I want to select from. Here\'s the gist of my problem: import numpy as np a = np.arange(20).reshape((5,4)) # array([[ 0, 1, 2, 3], # [ 4, 5, 6, 7], # [ 8, 9, 10, 11], # [12, 13, 14, 15], # [16, 17, 18, 19]]) # If I select certain rows, it works print a[[0, 1, 3], :] # array([[ 0, 1, 2, 3], # [ 4, 5, 6, 7], # [12, 13, 14, 15]])