获取numpy的数组的所有子序列 [英] Get all subsequences of a numpy array

查看:613
本文介绍了获取numpy的数组的所有子序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要生成所有连续 M 阵列,$ p $的长度子序列pferably作为二维阵列

Given an a numpy array of size n and an integer m I want to generate all sequential m length subsequences of the array, preferably as a two dimensional array.

例如:

>>> subsequences(arange(10), 4)

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

我可以拿出来做到这一点,最好的方法是

the best way I can come up with to do this is

def subsequences(arr, m):
    n = arr.size
    # Create array of indices, essentially solution for "arange" input
    indices = cumsum(vstack((arange(n - m + 1), ones((m-1, n - m + 1), int))), 0)
    return arr[indices]

有没有更好的,preferably建,函数,我失踪?

Is there a better, preferably built in, function that I'm missing?

推荐答案

下面是一个非常快速和高效的记忆方法,这只是一个说法到原来的数组:

Here's a very fast and memory efficient method, that's just a "view" into the original array:

from numpy.lib.stride_tricks import as_strided

def subsequences(arr, m):
    n = arr.size - m + 1
    s = arr.itemsize
    return as_strided(arr, shape=(m,n), strides=(s,s))

您应该做一个 np.copy 首先,如果你需要写此阵,否则你会修改原始阵列和相应的条目中的子序列阵为好。

You should make a np.copy first if you need to write to this array, otherwise you would modify the original array and the corresponding entries in the "subsequences" array as well.

在此处了解详情: http://stackoverflow.com/a/4924433/2379410

这篇关于获取numpy的数组的所有子序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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