How does one plot a running average without importing external modules (other than matplotlib)?

1.1k views Asked by At

Here is a link to the file with the information in 'sunspots.txt'. With the exception of external modules matploblib.pyplot and seaborn, how could one compute the running average without importing external modules like numpy and future? (If it helps, I can linspace and loadtxt without numpy.)

If it helps, my code thus far is posted below:

## open/read file
f2 =     open("/Users/location/sublocation/sunspots.txt", 'r')
## extract data
lines = f2.readlines()
## close file
f2.close()

t = [] ## time
n = [] ## number
## col 1 == col[0] -- number identifying which month
## col 2 == col[1] -- number of sunspots observed
for col in lines: ## 'col' can be replaced by 'line' iff change below is made
    new_data = col.split() ## 'col' can be replaced by 'line' iff change above is made
    t.append(float(new_data[0]))
    n.append(float(new_data[1]))
## extract data ++ close file

## check ##
# print(t)
# print(n)
## check ##

## import
import matplotlib.pyplot as plt
import seaborn as sns

## plot
sns.set_style('ticks')
plt.figure(figsize=(12,6))
plt.plot(t,n, label='Number of sunspots oberved monthly' )
plt.xlabel('Time')
plt.ylabel('Number of Sunspots Observed')
plt.legend(loc='best')
plt.tight_layout()
plt.savefig("/Users/location/sublocation/filename.png", dpi=600)

The question is from the weblink from this university (p.11 of the PDF, p.98 of the book, Exercise 3-1).

Before marking this as a duplicate:

A similar question was posted here. The difference is that all posted answers require importing external modules like numpy and future whereas I am trying to do without external imports (with the exceptions above).

1

There are 1 answers

0
wwii On BEST ANSWER

Noisy data that needs to be smoothed

y = [1.0016, 0.95646, 1.03544, 1.04559, 1.0232,
     1.06406, 1.05127, 0.93961, 1.02775, 0.96807,
     1.00221, 1.07808, 1.03371, 1.05547, 1.04498,
     1.03607, 1.01333, 0.943, 0.97663, 1.02639]

Try a running average with a window size of n

n = 3

Each window can by represented by a slice

window = y[i:i+n]

Need something to store the averages in

averages = []

Iterate over n-length slices of the data; get the average of each slice; save the average in another list.

from __future__ import division  # For Python 2
for i in range(len(y) - n):
    window = y[i:i+n]
    avg = sum(window) / n
    print(window, avg)
    averages.append(avg)

When you plot the averages you'll notice there are fewer averages than there are samples in the data.


Maybe you could import an internal/built-in module and make use of this SO answer -https://stackoverflow.com/a/14884062/2823755


Lots of hits searching with running average algorithm python