1

I have a problem that i don't know how to solve this question.

I have made some code who plot a graph:

import csv
import datetime
import matplotlib.pyplot as plt


data = open('iphonevsandroid.csv', 'r')
reader = csv.reader(data, delimiter=',')

iphone_data = []
android_data = []
dateTime = []
stringdates = []

for row in reader:

    first_date_row = row[0]
    first_date = row[0][:-13]

    if row[1] != 'iphone':
        iphone_data.append(row[1])

    if row[1] != 'iphone':
        android_data.append(row[1])

    if row[0] != 'week':
        stringdates.append(row[0][:-13])


for item in stringdates:
    dateTime.append(datetime.datetime.strptime(item, '%Y-%m-%d'))

x = iphone_data 
y = androiddata  

plt.ylabel('Interesse over tid')
plt.plot(dateTime,x)
plt.plot(dateTime,y)
plt.show()

Now i have to answer the question in top: Question: Smooth the trends using a moving average function with different window sizes.

I'm not an expert, so could someone please tell me what they mean about this?

1 Answer 1

1

A moving average function averages the last n data samples. You could achieve this in Python by storing the last n samples in a list and calculating the average:

data = range(100)
WINDOW_SIZE = 10
window = []
for i in data:
    window.append(i) # add the current data point into the window
    if len(window) > WINDOW_SIZE:
        window.pop(0) # remove the oldest sample from the window once it reaches the desired size
    avg = sum(window) / float(len(window)) # convert to float for python2.x
    print(i, window, avg)

Here's the first few lines of output. You can see the data point on the left, the window in the middle and the average on the right. Notice how once the window reaches 10 items in size, old items start being discarded to keep the window a fixed size.

0 [0] 0.0
1 [0, 1] 0.5
2 [0, 1, 2] 1.0
3 [0, 1, 2, 3] 1.5
4 [0, 1, 2, 3, 4] 2.0
5 [0, 1, 2, 3, 4, 5] 2.5
6 [0, 1, 2, 3, 4, 5, 6] 3.0
7 [0, 1, 2, 3, 4, 5, 6, 7] 3.5
8 [0, 1, 2, 3, 4, 5, 6, 7, 8] 4.0
9 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 4.5
10 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 5.5
11 [2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 6.5
12 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 7.5
13 [4, 5, 6, 7, 8, 9, 10, 11, 12, 13] 8.5
14 [5, 6, 7, 8, 9, 10, 11, 12, 13, 14] 9.5
15 [6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 10.5
16 [7, 8, 9, 10, 11, 12, 13, 14, 15, 16] 11.5
17 [8, 9, 10, 11, 12, 13, 14, 15, 16, 17] 12.5
18 [9, 10, 11, 12, 13, 14, 15, 16, 17, 18] 13.5
19 [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 14.5
20 [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] 15.5
21 [12, 13, 14, 15, 16, 17, 18, 19, 20, 21] 16.5
22 [13, 14, 15, 16, 17, 18, 19, 20, 21, 22] 17.5
Sign up to request clarification or add additional context in comments.

2 Comments

Well, i don't understand what you are doing.. I have to make some avage of the iphone_data and android_data?? how should i do that?
@Raaydk Yes, you need to apply a moving average function (such as the one I posted) to your data sets (iphone_data and android_data). What specifically are you having trouble understanding in my code?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.