0

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).

7
  • There are multiple "moving averages". Which one in particular do you mean, with what weights and time windows? (We should not have to look at your external link, but it looks like the 11 measurements centered at the current measurement, with equal weights.) Commented Jan 1, 2017 at 0:59
  • Yes, it is 11 measurements (-5 to 5). Here is a picture of the problem if it helps. Commented Jan 1, 2017 at 1:10
  • Doesn't matplotlib require numpy? Commented Jan 1, 2017 at 2:05
  • Iterate over n-length slices of the data; get the average of each slice; save the average in another list; plot the averages. Commented Jan 1, 2017 at 2:12
  • @wwii Using matplotlib does not require numpy. It is true that numpy has a linspace function, but I prefer coding it myself without importing if possible. So instead of importing linspace from numpy, I can do the following: 'def linspace(lower,upper,length):' 'return [lower + x*(upper-lower)/length for x in range(length)]' 'Ubound = 5' 'Lbound = -5' 'R = linspace( Ubound, Lbound - 1, abs(Ubound - Lbound) + 1 ) ## +-1 since not inclusive # # print(R)' (Can you elaborate a little on your answer?) Commented Jan 2, 2017 at 11:16

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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