Home > Back-end >  How to perform mathematical operations within a list?
How to perform mathematical operations within a list?

Time:01-10

I have a list where

A 1 
B 2
C 3
D 4
E 5
F 6
G 7
H 8
...
Z 26

I want to perform a running smoothing timeseries to generate another list using the formula:

(A-E) 3*(B D) 4*C 

Where the last value is until z. I cannot find samples where mathematical operations is found within the list. Most references and samples are between two lists. I am using Jupyter Notebook.

CodePudding user response:

You can use a sliding window approach. This means that you will take a window of a certain size, and slide it over the data, performing the calculation at each step.

If you want to use a window size of 5, you can start by performing the calculation for the first 5 elements of the list: (A-E) 3*(B D) 4C. Then you can move the window one element to the right, and recalculate the smoothed value using the next 5 elements: (B-F) 3(C E) 4*D. You can continue this process until you reach the end of the list.

def running_smooth(data, window_size):
  smoothed_data = []
  for i in range(len(data) - window_size   1):
    window = data[i:i window_size]
    #calculation here
    value = (window[0]-window[4])   3*(window[1] window[3])   4*window[2]
    smoothed_data.append(value)
  return smoothed_data
  • Related