Python Forum
How to create a moving average backward of a serie of sumbers?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a moving average backward of a serie of sumbers?
#6
I just saw your post way after the one from Pedrosky.

Its a bit easier to understand. Still try to get for each ma the numbers above each one to insert equations.

(Jun-25-2024, 02:00 PM)snippsat Wrote:
(Jun-25-2024, 06:10 AM)Pedroski55 Wrote:
range(len(nums) - 1, -1, -1)]
Not the best way to move backward in Python there are own method for this.
>>> Quodown = [9, 7, 6, 9, 7, 6, 4, 3, 0]
>>> reversed(Quodown)
<list_reverseiterator object at 0x000001D98D2BC130>
>>> list(reversed(Quodown))
[0, 3, 4, 6, 7, 9, 6, 7, 9]

>>> # The more famous one in Python
>>> Quodown[::-1]
[0, 3, 4, 6, 7, 9, 6, 7, 9]
So something like this.
Quodown = [9, 7, 6, 9, 7, 6, 4, 3, 0]
# Calculate cumulative sums from end to start
cumulative_sums = []
sumi = 0
for value in reversed(Quodown):
    sumi += value
    cumulative_sums.append(sumi)

# Calculate moving averages based on cumulative sums
moving_averages = [value / (index + 1) for index, value in enumerate(cumulative_sums)]
for index, value in enumerate(cumulative_sums, start=1):
    print(f"Cumulative sum of the first {index} elements: {value}")
# Print moving averages results
print('-' * 40)
for index, value in enumerate(moving_averages, start=1):
    print(f"Moving average of the first {index} elements: {value:.3f}")
Output:
Cumulative sum of the first 1 elements: 0 Cumulative sum of the first 2 elements: 3 Cumulative sum of the first 3 elements: 7 Cumulative sum of the first 4 elements: 13 Cumulative sum of the first 5 elements: 20 Cumulative sum of the first 6 elements: 29 Cumulative sum of the first 7 elements: 35 Cumulative sum of the first 8 elements: 42 Cumulative sum of the first 9 elements: 51 ---------------------------------------- Moving average of the first 1 elements: 0.000 Moving average of the first 2 elements: 1.500 Moving average of the first 3 elements: 2.333 Moving average of the first 4 elements: 3.250 Moving average of the first 5 elements: 4.000 Moving average of the first 6 elements: 4.833 Moving average of the first 7 elements: 5.000 Moving average of the first 8 elements: 5.250 Moving average of the first 9 elements: 5.667
Reply


Messages In This Thread
RE: How to create a moving average backward of a serie of sumbers? - by Frankd - Jun-25-2024, 04:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Moving average strategy irina_shubina 2 1,973 Jul-31-2022, 05:11 PM
Last Post: paulyan
  from Json Time Serie file to python dictionnary Reims 1 2,143 Sep-11-2019, 08:17 AM
Last Post: DeaD_EyE
  how do i create average of 1000 frames per second from video with yolo Rupen_gurung 0 1,564 Jul-20-2019, 01:55 AM
Last Post: Rupen_gurung
  Arabic language is backward in python charts bandar 3 3,801 Feb-23-2018, 07:10 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020