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

Community
  • 1
  • 1
  • 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.) – Rory Daulton Jan 01 '17 at 00:59
  • Yes, it is 11 measurements (-5 to 5). Here is a [picture of the problem](https://i.imgur.com/64Yybr6.png) if it helps. –  Jan 01 '17 at 01:10
  • Doesn't matplotlib require numpy? – wwii Jan 01 '17 at 02:05
  • Iterate over n-length slices of the data; get the average of each slice; save the average in another list; plot the averages. – wwii Jan 01 '17 at 02: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?) –  Jan 02 '17 at 11:16
  • Is this an exercise for a course that has restrictions on *outside* help? – wwii Jan 02 '17 at 15:25
  • @wwii I took a course in python for credit already. I am now trying to improve my skills even though I am no longer taking the class for credit. I will be starting research in about a month, and am trying to also build a library of useful code that I can refer to. I am also doing Project Euler to supplement this problem set. So this is not for a class and the only restrictions are the ones I place on myself. –  Jan 02 '17 at 21:11

1 Answers1

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

Community
  • 1
  • 1
wwii
  • 23,232
  • 7
  • 37
  • 77