简体   繁体   English

从Python中的另一个数组切片给定范围的数组

[英]Slice Array Given Range from another array in Python

To be more clear, let me reword my question that I initially asked below. 更清楚地说,让我改写我最初在下面提出的问题。

I have a series of data points that rise and fall in amplitude as someone takes a step and then another step. 我有一系列的数据点,随着某人迈出一步,然后又迈出一步,幅度在上升和下降。 Zero is when the foot is off the ground. 零是指脚离开地面。

A simple example would look like this: 一个简单的示例如下所示:

 data_array = (0,0,0,10,20,50,40,30,10,0,0,0,0,0,10,20,50,40,30,10,0,0)

I determine when each step starts and stop by recording the starts (all the indexes that start) in one array and the stops in another array. 我通过在一个数组中记录开始(所有开始的索引)并在另一个数组中记录停止来确定何时开始和停止每个步骤。

starts = (4 15) 开始=(4 15)

stops = (9 20) 停止=(9 20)

THE QUESTION: Now, I want to slice the actual data for each step out of the initial array and them in columns. 问题:现在,我想将每个步骤的实际数据切出初始数组,并将它们分成列。

[Note: That, if necessary, we know the number of steps taken by the amount of data in the starts or stops arrays.] [注意:如果有必要,我们知道由start或Stops数组中的数据量所采取的步骤数。

10 10 10 10

20 20 20 20

50 50 50 50

40 40 40 40

30 30 30 30

10 10 10 10

I can not figure out how to use those starts and stops indices to slice the initial array. 我不知道如何使用这些开始和停止索引来切片初始数组。 OR, I did not find a filter function to slice out the steps. 或者,我没有找到过滤器功能以分拆步骤。

BTW (2nd Edit) Here is some of the code I am using if it helps: BTW(第二次编辑)如果可以帮助,这是我正在使用的一些代码:

sigma = 5

threshold = 30

above_threshold = gaussian_filter(Fz, sigma=sigma) > threshold

#---INDEX ALL STATE CHANGES---

ind = np.where(np.diff(above_threshold))[0] + 1

print ind

ORIGINAL QUESTION 原始问题

In this type of array: 在这种类型的数组中:

data_array = (0,0,0,10,20,50,40,30,10,0,0)

I determine when values are above 20 and below 20. I return those indexes as starts (going above 20) and stops (going below 20) by making the following: 我确定何时值大于20和小于20。通过执行以下操作,返回那些索引作为开始(大于20)和停止(小于20):

startstop = np.vstack((ind[::2], ind[1::2])).T   
starts1=np.vstack((ind[::2])).T
stops1=np.vstack((ind[1::2])).T

Can someone point me in the right direction using numpy (or not) so that I can extract all those values in data_array using one of these arrays (startstop, starts1, stops1) to get this: 有人可以使用numpy(或不使用)将我指向正确的方向,以便我可以使用以下数组之一(startstop,starts1,stops1)提取data_array中的所有这些值以获取此信息:

new_array = (50,40,30)

Thanks, Scott 谢谢,斯科特

I don't understand the way you are trying to go about it, but can you use filter ?: 我不了解您尝试执行此操作的方式,但是可以使用filter吗:

>>> data_array = (0,0,0,10,20,50,40,30,10,0)
>>> filter(lambda x: x>20,data_array)

(50, 40, 30)

Works with numpy arrays too. 也适用于numpy数组。

take() will do what you wants, i think. 我想take()会做你想要的。

data_array = np.array([0,0,0,10,20,50,40,30,10,0])


b = data_array.take([1,2,3])
print b

output: 输出:

[ 0  0 10]

read more: take() more on take 了解更多: 采取() 更多在起飞

Your question is not perfectly clear, but if you are asking how to slice a Python list or iterable, use the built-in slice function : 您的问题不是很清楚,但是如果您要问如何切片Python列表或可迭代,请使用内置的slice函数

>>> data_array = (0,0,0,10,20,50,40,30,10,0)
>>> data_array[slice(5,8)]
(50, 40, 30)
>>> new_list=list(data_array[slice(5,8)])
>>> new_list
[50, 40, 30]

Python lists are defined with square brackets, and we want to generate a list of lists (where each piece contains one of your defined segments). Python列表用方括号定义,我们想生成一个列表列表(其中每段都包含一个定义的段)。 Since computers start counting at 0, your "4th element is the start" translates to array index = 3. 由于计算机从0开始计数,因此“第四个元素是开始”转换为数组索引= 3。

One quirk is that to query the 4th to 9th element we'd use data_array[3:9]: this slice notation gives you every element starting at the first specified, up to (but not including) the last. 一个怪癖是要查询第4到第9个元素,我们将使用data_array [3:9]:此切片符号为您提供每个元素,从指定的第一个开始,直至(但不包括)最后一个。 The rest is a list comprehension that goes over any arbitrary number of step segments. 其余的是列表推导,遍历任意数量的步骤段。

starts = [4, 15]
stops = [9, 20]
data_array = [0,0,0,10,20,50,40,30,10,0,0,0,0,0,10,20,50,40,30,10,0,0]
segments = [ data_array[starts[i] - 1: stops[i]  ]  for i in range( len(starts) ) ]

returns 退货

>>> segments
[[10, 20, 50, 40, 30, 10], [10, 20, 50, 40, 30, 10]]

Each set of steps (segment) can be accessed individually: 每组步骤(段)都可以单独访问:

>>>segments[0]
[10, 20, 50, 40, 30, 10]

EDIT: Alternately... if you need to use numpy array, then try: segments = array([ data_array[starts[i] - 1: stops[i] ] for i in range( len(starts) ) ]) 编辑:或者...如果您需要使用numpy数组,请尝试:segments = array([data_array [starts [i]-1:stops [i]] for i in range(len(starts))])

I tried working in additional modules to get the array directly (using itertools.islice with itertools.chain , numpy.fromiter , etc). 我尝试在其他模块中工作以直接获取数组(将itertools.isliceitertools.chainnumpy.fromiter等配合使用)。 But even if the fancier solution worked, I'm not sure it'd offer significant speed advantages over converting to an array, and it would be a lot less concise. 但是,即使更出色的解​​决方案行得通,但我不确定与转换为阵列相比,它是否具有明显的速度优势,而且不够简洁。 See: How do I build a numpy array from a generator? 请参阅: 如何从生成器构建numpy数组?

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

相关问题 从另一个数组大小的numpy数组获取范围/切片 - Get range/slice from numpy array the size of another array 在python中,我想通过给定的字符串从数组中选择一个范围 - in python, I want to select from array an range by given string 如何使用python将数组内的字符串切成另一个数组 - how to slice strings inside array into another array using python python:在给定维度索引的情况下提取多维数组的一个切片 - python: extracting one slice of a multidimensional array given the dimension index 带逗号的 Python 数组切片? - Python Array Slice With Comma? 如何用从另一个数组中采样的数字填充数组的切片? - How to fill a slice of an array with numbers sampled from another array? 用另一个数组中的值替换numpy数组的切片 - Replace slice of a numpy array with values from another array 在给定轴上从最大值切分多维 numpy 数组 - Slice multidimensional numpy array from max in a given axis 如何根据另一个二维数组中给出的索引对二维数组进行切片 - how to slice a 2d array based on indices given in another 2d array 在Python中:给定一个原始的数字数组,我如何创建一个包含特定范围内原始数组值的新数组? - In Python: Given an original array of numbers, how would I create a new array that contains the values from the original array within a certain range?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM