简体   繁体   English

有没有办法仅使用 NumPy 来查找 2D 数组中最后 n 个元素和前 n 个元素的总和?

[英]Is there a way to find the sum of the last n and first n elements in a 2D array using only NumPy?

Lets say that n is a variable and I use a simple matrix example.假设n是一个变量,我使用一个简单的矩阵示例。

import numpy as np

n = 2

matrix = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
                   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

desired_output = np.array([[nan, nan, 10, 15, 20, 25, 30, 35, nan, nan], 
                           [nan, nan, 10, 15, 20, 25, 30, 35, nan, nan]])

So desired_output[i] = sum of elements in the interval matrix[i - n, i + n] both inclusive.所以desired_output[i] = 区间矩阵[i - n, i + n]中的元素之和,包括两者。 Is there a way to do this using NumPy and without python iteration?有没有办法使用 NumPy 而没有 python 迭代来做到这一点? The numbers in the array can be arbitrary.数组中的数字可以是任意的。

You can use one of my absolute favorite things to be added to numpy in 1.20: numpy.lib.stride_tricks.sliding_window_view您可以使用我最喜欢的东西之一在 1.20 中添加到 numpy: numpy.lib.stride_tricks.sliding_window_view

import numpy as np

matrix = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
                   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
n = 2

First, apply the sliding window view:首先,应用滑动 window 视图:

window_size = 2 * n + 1
arr = np.lib.stride_tricks.sliding_window_view(matrix, window_size, axis=1)

Then sum it on the last axis:然后在最后一个轴上求和:

arr = arr.sum(axis=2)

This gives you:这给了你:

>>> arr
array([[10, 15, 20, 25, 30, 35],
       [10, 15, 20, 25, 30, 35]])

To the best of my knowledge there's no integer NaN in numpy so your integer/nan output is impossible.据我所知,numpy 中没有 integer NaN 所以你的整数/nan output 是不可能的。 If you want floats you can pad easily with nans though:如果你想要浮动,你可以很容易地用 nans 填充:

arr_padded = np.full((arr.shape[0], arr.shape[1] + 2 * n), np.nan)
arr_padded[:, n:-1 * n] = arr

>>> arr_padded
array([[nan, nan, 10., 15., 20., 25., 30., 35., nan, nan],
       [nan, nan, 10., 15., 20., 25., 30., 35., nan, nan]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 二维 Numpy 数组的前 N 个元素,其中 N 是一个列表 - First N elements of a 2D Numpy array where N is a list 在 numpy 二维数组中查找前 n 个非零值 - Find first n non zero values in in numpy 2d array 在 2d numpy 数组的每一行中选择前“n”个非重复元素的有效方法 - Efficient way to pick first 'n' non-repeating elements in every row of a 2d numpy array 在 numpy 二维数组上 - 当 N 行更改时如何将每行中的最后 N 个数组元素设置为零 - on a numpy 2D array - how to set last N array elements in each row to zero when N changes over rows Numpy 2D 数组:我需要检查数组的前 n-2 个元素中是否有任何一个是 nan 并确保 n-1 不是 - Numpy 2D array: I need to check if any of the first n-2 elements of an array are nan and make sure n-1 is not Numpy 二维数组 - 从指定索引中获取 N 个元素 - Numpy 2d array - take N elements from specified index 在二维 numpy 数组的每行中找到 N 个最高值 - Find N highest values per row of a 2d numpy array 获取除 numpy 数组的第一个和最后 n 个元素之外的所有元素 - Get all except first and last n elements of a numpy array 删除numpy数组的最后n个元素仅在某些情况下有效 - Deleting the last n elements of numpy array works only in some cases 过滤 ND numpy 数组并仅保留特定元素 - filter a N-D numpy array and keep only specific elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM