Create a new array of all values from an array with step

I am new to Python. I would like to create a new array, that contains all values from an existing array with the step.

I tried to implement it but I think there is another way to have better performance. Any try or recommendation is highly appreciated.

Ex: Currently, I have:

  1. An array: 115.200 values (2D dimension)
  2. Step: 10.000

….

array([[ 0.2735, -0.308 ],
   [ 0.287 , -0.3235],
   [ 0.2925, -0.324 ],
   [ 0.312 , -0.329 ],
   [ 0.3275, -0.345 ],
   [ 0.3305, -0.352 ],
   [ 0.332 , -0.3465],
   ...
   [ 0.3535, -0.353 ],
   [ 0.361 , -0.3445],
   [ 0.3545, -0.329 ]])

Expectation: A new array is sliced the array above by step of 10.000.

Below is my code:

for x in ecg_data:
    number_samples_by_duration_exp_temp = 10000
    # len(ecg_property.sample) = 115200
    times = len(ecg_property.sample) / number_samples_by_duration_exp_temp
    index_by_time = [int(y)*number_samples_by_duration_exp_temp for y in np.arange(1, times, 1)]
    list = []
    temp = 0
    for z in index_by_time:
        arr_samples_by_duration = ecg_property.sample[temp:z]
        list.append(arr_samples_by_duration)
        temp = z

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

numpy can not be used for this purpose as len(ecg_property.sample) #115,200 is not fully divisible by number_samples_by_duration_exp_temp #10,000 and numpy cannot allow elements of varying lengths 🙂

You can try list comprehension.

result_list = [ecg_property.sample[temp :temp+step] for temp in np.arange(times)*step ]

where
step=10000 and times = len(ecg_property.sample) / step

It can be further modified if needed and as per requirement.

(You can try out each step in above line of code in this answer and see the output to understand each step )
Hope this works out.
ty!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x