更有效的方法来删除np

更有效的方法来删除np

本文介绍了更有效的方法来删除np.array中的最后N个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用np.arrays.我正在尝试删除最后n个元素,其中n也可以是1.

I am working with np.arrays. I am trying to remove the last n elements, where n can be also 1.

n=5
corr=np.full(10,10)

通常,我将这种方法用于数组切片:

Usually I use this approach with array slicing:

corr=corr[:-n]

但是我正在考虑使用np.delete来提高性能:

But I was thinking of using np.delete to increase the performance:

np.delete(corr,range(-n,0))

但是它不起作用,与数组切片相比,还有更好的解决方案吗?(该方法还可以处理n = 0的情况,这将是一个优势)

But it doesn't work, is there any better solution compare with array slicing?(method able to deal also with case in which n=0, would be a point of advantage)

推荐答案

数组是具有诸如 shape dtype 和数据缓冲区之类的属性的对象.像 A [:-5] 之类的视图是另一个具有自己的 shape 等形状但具有共享数据缓冲区的数组.它正在查看相同的数据,但只看到一个切片.

An array is an object with attributes like shape, dtype, and a data buffer. A view like A[:-5] is another array with its own shape, etc, but with a shared data buffer. It's looking at the same data, but only sees a slice.

A [:-5] .copy()看起来是相同的,但是将具有自己的数据缓冲区,即从 A 中选择的元素的副本.

A[:-5].copy() will appear to be the same, but will have its own data buffer, a copy of selected elements from A.

无法更改 A 的数据缓冲区的大小.

There's no way of changing the size of the data buffer of A.

np.delete 返回具有其自己的数据缓冲区的新数组.它根据形状和删除图案使用各种方法.在所有情况下,它都是副本,并且比切片要慢.

np.delete returns a new array with its own data buffer. It uses various methods depending on the shape and delete pattern. It all cases it is a copy, and slower than slicing.

这篇关于更有效的方法来删除np.array中的最后N个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:52