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)
stops = (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.]
10 10
20 20
50 50
40 40
30 30
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:
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:
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:
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 ?:
>>> 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.
take() will do what you wants, i think.
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 :
>>> 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). Since computers start counting at 0, your "4th element is the start" translates to array index = 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. 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) ) ])
I tried working in additional modules to get the array directly (using itertools.islice
with itertools.chain
, numpy.fromiter
, etc). 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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.