Bird
Raised Fist0
ML Pythonml~20 mins

Moving averages in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Moving Average Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Simple Moving Average (SMA)

Which of the following best describes the Simple Moving Average (SMA) in time series data?

AIt calculates the average of all past data points up to the current point.
BIt calculates the average of a fixed number of the most recent data points.
CIt weights recent data points more heavily than older ones.
DIt predicts future values by fitting a linear trend to the data.
Attempts:
2 left
💡 Hint

Think about how SMA uses a window of data points.

Predict Output
intermediate
2:00remaining
Output of Exponential Moving Average Calculation

What is the output of the following Python code that calculates an exponential moving average (EMA) with alpha=0.5 on the data [10, 20, 30]?

ML Python
data = [10, 20, 30]
alpha = 0.5
ema = [data[0]]
for x in data[1:]:
    ema.append(alpha * x + (1 - alpha) * ema[-1])
print([round(v, 2) for v in ema])
A[10, 15.0, 22.5]
B[10, 20.0, 30.0]
C[10, 15.0, 25.0]
D[10, 17.5, 23.75]
Attempts:
2 left
💡 Hint

EMA updates by weighting the new value and previous EMA.

Model Choice
advanced
2:00remaining
Choosing Moving Average Type for Noisy Data

You have a noisy sensor data stream and want to smooth it while giving more importance to recent readings. Which moving average method is best?

AExponential Moving Average (EMA)
BCumulative Moving Average (CMA)
CSimple Moving Average (SMA)
DWeighted Moving Average with equal weights
Attempts:
2 left
💡 Hint

Consider which method weights recent data more.

Hyperparameter
advanced
2:00remaining
Effect of Alpha in Exponential Moving Average

In an Exponential Moving Average (EMA), what is the effect of increasing the smoothing factor alpha from 0.1 to 0.9?

AThe EMA becomes smoother and reacts slower to recent changes.
BThe EMA becomes equivalent to a Simple Moving Average.
CThe EMA ignores recent data and focuses on older data.
DThe EMA becomes less smooth and reacts faster to recent changes.
Attempts:
2 left
💡 Hint

Alpha controls the weight of the newest data point.

Metrics
expert
2:00remaining
Comparing Moving Average Smoothing with MSE

You apply two smoothing methods on a noisy time series: SMA with window size 3 and EMA with alpha=0.3. Which metric best compares their smoothing quality against the original clean signal?

ASum of absolute differences between noisy and smoothed signals
BAccuracy score of smoothed signal classification
CMean Squared Error (MSE) between smoothed and clean signals
DNumber of data points in the smoothed signal
Attempts:
2 left
💡 Hint

Think about measuring how close the smoothed signal is to the true clean signal.

Practice

(1/5)
1. What is the main purpose of using a moving average in data analysis?
easy
A. To smooth out short-term fluctuations and highlight longer-term trends
B. To increase the number of data points in a dataset
C. To remove all noise from the data completely
D. To predict exact future values without error

Solution

  1. Step 1: Understand the role of moving averages

    Moving averages smooth data by averaging nearby points, reducing short-term ups and downs.
  2. Step 2: Identify the main goal

    The goal is to reveal longer-term trends by reducing noise, not to remove noise completely or predict exact values.
  3. Final Answer:

    To smooth out short-term fluctuations and highlight longer-term trends -> Option A
  4. Quick Check:

    Moving average = smoothing trends [OK]
Hint: Moving averages smooth data to show trends clearly [OK]
Common Mistakes:
  • Thinking moving averages increase data points
  • Believing moving averages remove all noise
  • Assuming moving averages predict exact future values
2. Which of the following Python code snippets correctly computes a simple moving average with window size 3 for a list data?
easy
A. [data[i] / 3 for i in range(len(data))]
B. [sum(data[i:i+3]) for i in range(len(data)-3)]
C. [sum(data[i:i+3]) / 3 for i in range(len(data)-3)]
D. [(data[i] + data[i+1] + data[i+2]) / 3 for i in range(len(data)-2)]

Solution

  1. Step 1: Understand moving average calculation

    A simple moving average with window 3 averages each group of 3 consecutive elements.
  2. Step 2: Check each option's correctness

    [(data[i] + data[i+1] + data[i+2]) / 3 for i in range(len(data)-2)] correctly sums three consecutive elements and divides by 3, iterating till len(data)-2.
    [sum(data[i:i+3]) for i in range(len(data)-3)] sums but does not divide by 3.
    [sum(data[i:i+3]) / 3 for i in range(len(data)-3)] divides but uses range(len(data)-3), which is too short.
    [data[i] / 3 for i in range(len(data))] divides single elements by 3, not averaging groups.
  3. Final Answer:

    [(data[i] + data[i+1] + data[i+2]) / 3 for i in range(len(data)-2)] -> Option D
  4. Quick Check:

    Sum 3 elements / 3, range correct = [(data[i] + data[i+1] + data[i+2]) / 3 for i in range(len(data)-2)] [OK]
Hint: Sum 3 elements and divide by 3, loop till len-2 [OK]
Common Mistakes:
  • Forgetting to divide by window size
  • Using wrong range length causing index errors
  • Averaging single elements instead of groups
3. Given the code below, what is the output?
data = [2, 4, 6, 8, 10]
window = 2
moving_avg = [sum(data[i:i+window]) / window for i in range(len(data) - window + 1)]
print(moving_avg)
medium
A. [2.0, 4.0, 6.0, 8.0, 10.0]
B. [3.0, 5.0, 7.0]
C. [3.0, 5.0, 7.0, 9.0]
D. [6.0, 8.0, 10.0]

Solution

  1. Step 1: Calculate moving averages manually

    Window size is 2, so average pairs:
    (2+4)/2=3.0
    (4+6)/2=5.0
    (6+8)/2=7.0
    (8+10)/2=9.0
  2. Step 2: Confirm output list length and values

    Length is len(data)-window+1 = 5-2+1=4, matching 4 values above.
  3. Final Answer:

    [3.0, 5.0, 7.0, 9.0] -> Option C
  4. Quick Check:

    Pairs averaged = [3.0, 5.0, 7.0, 9.0] [OK]
Hint: Average pairs sliding by one, length = len - window + 1 [OK]
Common Mistakes:
  • Confusing window size with output length
  • Calculating sums but forgetting to divide
  • Off-by-one errors in range length
4. The following code is intended to compute a moving average with window size 3, but it misses the last window. What is the problem?
data = [1, 2, 3, 4, 5]
window = 3
moving_avg = [sum(data[i:i+window]) / window for i in range(len(data)-window)]
print(moving_avg)
medium
A. The range should be len(data) - window + 1 to include the last window
B. The window size is too large for the data list
C. sum() cannot be used on list slices
D. Division by window size should be outside the list comprehension

Solution

  1. Step 1: Analyze the range length

    Range is len(data)-window = 5-3=2, but to cover all windows it should be len(data)-window+1 = 3.
  2. Step 2: Understand impact of incorrect range

    Using len(data)-window misses the last valid window slice, causing incomplete results.
  3. Final Answer:

    The range should be len(data) - window + 1 to include the last window -> Option A
  4. Quick Check:

    Range length = len - window + 1 [OK]
Hint: Use range(len(data) - window + 1) for full coverage [OK]
Common Mistakes:
  • Using len(data) - window instead of +1
  • Thinking sum() can't handle slices
  • Misplacing division outside comprehension
5. You have daily sales data for 10 days: [10, 12, 11, 14, 13, 15, 16, 14, 13, 12]. You want to smooth this data using a moving average with window size 4 but only want to keep averages where the window's average is greater than 13. Which Python code correctly computes this filtered moving average?
hard
A. [sum(data[i:i+4])/4 for i in range(len(data)-4) if sum(data[i:i+4])/4 > 13]
B. [avg for i in range(len(data)-3) if (avg := sum(data[i:i+4])/4) > 13]
C. [sum(data[i:i+4])/4 for i in range(len(data)-3) if sum(data[i:i+4]) > 13]
D. [sum(data[i:i+4])/4 for i in range(len(data)-3) if sum(data[i:i+4])/4 < 13]

Solution

  1. Step 1: Understand window size and range

    Window size 4 means averaging groups of 4 elements, so range is len(data)-3 = 10-3=7.
  2. Step 2: Filter averages greater than 13

    [avg for i in range(len(data)-3) if (avg := sum(data[i:i+4])/4) > 13] uses assignment expression to compute average once and filter if > 13.
    [sum(data[i:i+4])/4 for i in range(len(data)-4) if sum(data[i:i+4])/4 > 13] uses wrong range (len(data)-4=6), missing last window.
    [sum(data[i:i+4])/4 for i in range(len(data)-3) if sum(data[i:i+4]) > 13] filters sum > 13, not average > 13.
    [sum(data[i:i+4])/4 for i in range(len(data)-3) if sum(data[i:i+4])/4 < 13] filters averages less than 13, opposite condition.
  3. Final Answer:

    [avg for i in range(len(data)-3) if (avg := sum(data[i:i+4])/4) > 13] -> Option B
  4. Quick Check:

    Use assignment expression to filter averages > 13 [OK]
Hint: Use assignment expression (walrus) to filter averages [OK]
Common Mistakes:
  • Using wrong range length missing last windows
  • Filtering sum instead of average
  • Using wrong comparison operator