0
0
ML Pythonml~10 mins

Moving averages in ML Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the simple moving average of a list of numbers.

ML Python
def simple_moving_average(data, window_size):
    moving_averages = []
    for i in range(len(data) - window_size + 1):
        window = data[i:i + window_size]
        average = sum(window) / [1]
        moving_averages.append(average)
    return moving_averages
Drag options to blanks, or click blank then click option'
Ai
Blen(data)
Cwindow_size
Dlen(window)
Attempts:
3 left
💡 Hint
Common Mistakes
Using the length of the whole data list instead of the window size.
Dividing by the index variable instead of the window size.
2fill in blank
medium

Complete the code to calculate the exponential moving average (EMA) smoothing factor.

ML Python
def calculate_ema_smoothing_factor(window_size):
    smoothing_factor = 2 / ([1] + 1)
    return smoothing_factor
Drag options to blanks, or click blank then click option'
Awindow_size * 2
Bwindow_size - 1
Cwindow_size + 1
Dwindow_size
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting 1 instead of adding 1 to the window size.
Multiplying the window size instead of adding.
3fill in blank
hard

Fix the error in the code to update the EMA value correctly.

ML Python
def update_ema(current_value, previous_ema, smoothing_factor):
    ema = [1] * current_value + (1 - smoothing_factor) * previous_ema
    return ema
Drag options to blanks, or click blank then click option'
Asmoothing_factor
Bprevious_ema
Ccurrent_value
D1 - smoothing_factor
Attempts:
3 left
💡 Hint
Common Mistakes
Using previous EMA instead of smoothing factor for the current value.
Using current value without multiplying by smoothing factor.
4fill in blank
hard

Fill both blanks to create a dictionary of moving averages for each window size.

ML Python
def moving_averages_dict(data, window_sizes):
    averages = {size: simple_moving_average(data, [1]) for size in [2]
    return averages
Drag options to blanks, or click blank then click option'
Asize
Bdata
Cwindow_sizes
Daverages
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' instead of 'window_sizes' in the loop.
Using 'averages' inside the comprehension which is not defined yet.
5fill in blank
hard

Fill all three blanks to filter data points and compute their moving averages.

ML Python
filtered_data = [x for x in data if x [1] threshold]
moving_avg = simple_moving_average(filtered_data, [2])
result = {i: moving_avg[i] for i in range(len(moving_avg)) if moving_avg[i] [3] 0}
Drag options to blanks, or click blank then click option'
A>
Bwindow_size
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for filtering data points.
Using wrong variable for window size.
Filtering moving averages less than zero instead of greater.