0

Below is a plot I made where the y-axis (v) are values contained in a list. As you can see, the list values alternate between segments of high values and segments of low values such that the list looks like:

li = [0.5,0.49,0.5,..,0.5,0.001,0.001,...,0.001,0.49,0.5,...,0.5,]

My goal is to take each of the six segments of the high values and each of the six segments of the low values, and then calculate each segment's average. To do this, I am trying to separate the list above and put each segment into its own list and each list in a respective high value/ low value list. Something along the lines of:

high_segments = [[high_values1],[high_values2],[high_values3]]
low_segments  = [[low_values1],[low_values2],[low_values3]]

I have been trying to construct a for loop to do this but have been struggling with how to deal with changes between groups of low and high values. Any suggestions are greatly appreciated.

Voltage vs Time

2 Answers 2

1

Using numpy and spliting by the mean.

import numpy as np

li = np.array([
    0.5, 0.49, 0.5,
    0.001, 0.001, 0.001,
    0.49, 0.5, 0.5,
    0, 0.002, 0.01,
])

# Split into high/low groups using the mean:
is_high = li >= li.mean()
is_low = li < li.mean()

# Determine the groups:
diff = np.insert(np.diff(is_high), 0, False).astype(np.int)  # array([0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0])
groups = diff.cumsum()  # array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3])

high_segments = np.array([li[groups==kk] for kk in np.unique(groups[is_high])])
low_segments = np.array([li[groups==kk] for kk in np.unique(groups[is_low])])

high_segments_mean = high_segments.mean(axis=1)
low_segments_mean = low_segments.mean(axis=1)
2
  • This looks perfect! Thank you. Could you explain a little bit about what the second to last two lines of code are doing. I am not very familiar with the use of these numpy attributes. For example, what is the insert and diff function doing here?
    – ZacharyC
    Feb 10, 2021 at 3:39
  • is_high is an array of True and False values. You can find the locations of the transitions from high to low and vice versa using np.diff() which calculates the difference between each value and the one before it in the array. Taking the difference means that you end up with one less value in the array, so you need to insert an extra False value at index 0 using np.insert(). The cumulative sum of the diff array then gives you the groups.
    – bafrullaci
    Feb 10, 2021 at 20:00
0

I assume that your input array li is of consecutive 6-high values and 6-low values, leading that the array has 36 elements. Firstly, numpy.reshape function creates a consecutive 6-element sub-arrays. Then, we can select the odd sub-array (high values in the figure), and the even sub-array (low values) by slicing. Stacking two array by the second axis will form a desired shape. The block_reduce function will calculate for each block.

import numpy as np
# conda install -c anaconda scikit-image
from skimage.measure import block_reduce

if __name__ == '__main__':
    li = np.arange(0, 36)
    li = li.reshape(-1, 6)
    high_values = li[::2]
    low_values = li[1::2]
    combined = np.stack((high_values, low_values), axis=1)
    segment_average = block_reduce(combined, block_size=(1,2,6), func=np.mean, cval=np.mean(combined)).flatten()
    print(f"[main] input:\n{li}")
    print(f"[main] high_values:\n{high_values}")
    print(f"[main] low_values:\n{low_values}")
    print(f"[main] combined:\n{combined}")
    print(f"[main] segment average: {segment_average}")

Result:

[main] input:
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]
 [24 25 26 27 28 29]
 [30 31 32 33 34 35]]
[main] high_values:
[[ 0  1  2  3  4  5]
 [12 13 14 15 16 17]
 [24 25 26 27 28 29]]
[main] low_values:
[[ 6  7  8  9 10 11]
 [18 19 20 21 22 23]
 [30 31 32 33 34 35]]
[main] combined:
[[[ 0  1  2  3  4  5]
  [ 6  7  8  9 10 11]]

 [[12 13 14 15 16 17]
  [18 19 20 21 22 23]]

 [[24 25 26 27 28 29]
  [30 31 32 33 34 35]]]
[main] segment average: [ 5.5 17.5 29.5]
1
  • I apologize if my question wasn't completely clear. Unfortunately, there isn't a consistent number of high consecutive numbers and low consecutive numbers. The number of values in each segment vary so that it might be something like 20 high, 23 low, 24 high, 21 low, and so on
    – ZacharyC
    Feb 10, 2021 at 2:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.