跳过numpy数组的第n个索引 [英] Skip every nth index of numpy array

查看:267
本文介绍了跳过numpy数组的第n个索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了进行K折验证,我想对numpy数组进行切片,以便制作原始数组的视图,但删除第n个元素.

In order to do K-fold validation I would like to use slice a numpy array such that a view of the original array is made but with every nth element removed.

例如:[0、1、2、3、4、5、6、7、8、9]

For example: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

如果n = 4,则结果将为[1、2、4、5、6、8、9]

If n = 4 then the result would be [1, 2, 4, 5, 6, 8, 9]

注意:numpy的要求是因为它用于固定依赖关系的机器学习分配.

Note: the numpy requirement is due to this being used for a machine learning assignment where the dependencies are fixed.

推荐答案

使用modulus

Approach #1 with modulus

a[np.mod(np.arange(a.size),4)!=0]

样品运行-

In [255]: a
Out[255]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [256]: a[np.mod(np.arange(a.size),4)!=0]
Out[256]: array([1, 2, 3, 5, 6, 7, 9])


使用masking的方法#2:要求为view


Approach #2 with masking : Requirement as a view

考虑到视图的需求,如果要节省内存,我们可以存储等效的布尔数组,该数组在Linux系统上占用的内存要少8倍.因此,这种基于掩码的方法将像这样-

Considering the views requirement, if the idea is to save on memory, we could store the equivalent boolean array that would occupy 8 times less memory on Linux system. Thus, such a mask based approach would be like so -

# Create mask
mask = np.ones(a.size, dtype=bool)
mask[::4] = 0

这是内存需求统计信息-

Here's the memory requirement stat -

In [311]: mask.itemsize
Out[311]: 1

In [312]: a.itemsize
Out[312]: 8

然后,我们可以使用布尔索引作为视图-

Then, we could use boolean-indexing as a view -

In [313]: a
Out[313]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [314]: a[mask] = 10

In [315]: a
Out[315]: array([ 0, 10, 10, 10,  4, 10, 10, 10,  8, 10])


使用NumPy array strides的方法#3:要求为view


Approach #3 with NumPy array strides : Requirement as a view

您可以使用 np.lib.stride_tricks.as_strided 创建给定输入数组长度的这种视图是n的倍数.如果不是倍数,它将仍然有效,但不是安全的做法,因为我们将超出为输入数组分配的内存.请注意,这样创建的视图将为2D.

You can use np.lib.stride_tricks.as_strided to create such a view given the length of the input array is a multiple of n. If it's not a multiple, it would still work, but won't be a safe practice, as we would be going beyond the memory allocated for input array. Please note that the view thus created would be 2D.

因此,获得这样一个视图的实现是-

Thus, an implementaion to get such a view would be -

def skipped_view(a, n):
    s = a.strides[0]
    strided = np.lib.stride_tricks.as_strided
    return strided(a,shape=((a.size+n-1)//n,n),strides=(n*s,s))[:,1:]

样品运行-

In [50]: a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) # Input array

In [51]: a_out = skipped_view(a, 4)

In [52]: a_out
Out[52]: 
array([[ 1,  2,  3],
       [ 5,  6,  7],
       [ 9, 10, 11]])

In [53]: a_out[:] = 100 # Let's prove output is a view indeed

In [54]: a
Out[54]: array([  0, 100, 100, 100,   4, 100, 100, 100,   8, 100, 100, 100])

这篇关于跳过numpy数组的第n个索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆