Bird
Raised Fist0
ML Pythonml~10 mins

Autocorrelation analysis in ML Python - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the autocorrelation of a time series using numpy.

ML Python
import numpy as np

data = np.array([1, 2, 3, 4, 5])
mean = np.mean(data)
variance = np.var(data)

autocorr = np.correlate(data - mean, data - mean, mode=[1])[len(data)-1] / (variance * len(data))
print(autocorr)
Drag options to blanks, or click blank then click option'
A'full'
B'valid'
C'same'
D'none'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'valid' or 'same' mode which returns smaller arrays and misses some lags.
Not subtracting the mean before correlation.
2fill in blank
medium

Complete the code to compute the autocorrelation function for lags up to max_lag.

ML Python
def autocorrelation(data, max_lag):
    n = len(data)
    mean = sum(data) / n
    var = sum((x - mean) ** 2 for x in data) / n
    result = []
    for lag in range(max_lag + 1):
        cov = sum((data[i] - mean) * (data[i + lag] - mean) for i in range(n - lag)) / n
        result.append(cov / [1])
    return result
Drag options to blanks, or click blank then click option'
An
Bmean
Cmax_lag
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Dividing by mean or length instead of variance.
Not normalizing covariance, resulting in incorrect scale.
3fill in blank
hard

Fix the error in the code to correctly compute the autocorrelation using pandas.

ML Python
import pandas as pd

data = pd.Series([1, 2, 3, 4, 5])
autocorr_value = data.autocorr(lag=[1])
print(autocorr_value)
Drag options to blanks, or click blank then click option'
A-1
B0
C1
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using lag=0 which returns 1 always.
Using lag greater than series length causing NaN.
4fill in blank
hard

Fill both blanks to create a dictionary of autocorrelations for lags 0 to max_lag.

ML Python
def autocorr_dict(data, max_lag):
    return {lag: data.autocorr(lag=[1]) for lag in range([2])}
Drag options to blanks, or click blank then click option'
Alag
Bmax_lag + 1
Cmax_lag
Dlag + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using fixed lag instead of loop variable.
Using range(max_lag) which excludes max_lag.
5fill in blank
hard

Fill all three blanks to compute and plot the autocorrelation function using matplotlib.

ML Python
import matplotlib.pyplot as plt

def plot_autocorr(data, max_lag):
    autocorrs = [data.autocorr(lag=[1]) for lag in range([2])]
    plt.stem(range([3]), autocorrs, use_line_collection=True)
    plt.xlabel('Lag')
    plt.ylabel('Autocorrelation')
    plt.title('Autocorrelation Function')
    plt.show()
Drag options to blanks, or click blank then click option'
Alag
Bmax_lag + 1
Cmax_lag
Dlag + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using max_lag instead of max_lag + 1, missing last lag.
Using fixed lag instead of loop variable.

Practice

(1/5)
1. What does autocorrelation measure in a time series dataset?
easy
A. The difference between the highest and lowest values in the data
B. The total sum of all data points in the series
C. The average value of the dataset
D. The relationship between current data points and past data points at different time lags

Solution

  1. Step 1: Understand autocorrelation concept

    Autocorrelation checks how current values relate to past values at various time gaps (lags).
  2. Step 2: Compare options to definition

    Only The relationship between current data points and past data points at different time lags correctly describes this relationship; others describe unrelated statistics.
  3. Final Answer:

    The relationship between current data points and past data points at different time lags -> Option D
  4. Quick Check:

    Autocorrelation = relationship with past points [OK]
Hint: Autocorrelation links current data to past data points [OK]
Common Mistakes:
  • Confusing autocorrelation with average or sum
  • Thinking it measures difference between max and min
  • Assuming it only looks at immediate previous point
2. Which of the following Python code snippets correctly computes the autocorrelation at lag 1 for a list data?
easy
A. import numpy as np np.corrcoef(data[:-1], data[1:])[0,1]
B. np.corrcoef(data, data)[0,1]
C. np.mean(data) - np.mean(data[1:])
D. np.sum(data) / len(data)

Solution

  1. Step 1: Understand autocorrelation calculation

    Autocorrelation at lag 1 compares data points with the next point, so we correlate data[:-1] with data[1:].
  2. Step 2: Check code correctness

    import numpy as np np.corrcoef(data[:-1], data[1:])[0,1] uses np.corrcoef correctly on shifted slices; others do not compute correlation at lag 1.
  3. Final Answer:

    import numpy as np\nnp.corrcoef(data[:-1], data[1:])[0,1] -> Option A
  4. Quick Check:

    Shifted slices correlation = import numpy as np np.corrcoef(data[:-1], data[1:])[0,1] [OK]
Hint: Use shifted slices for lag correlation in numpy [OK]
Common Mistakes:
  • Using correlation of data with itself (option B)
  • Calculating mean difference instead of correlation
  • Using sum or mean instead of correlation
3. Given the time series data = [2, 4, 6, 8, 10], what is the autocorrelation at lag 1 using numpy's correlation coefficient?
medium
A. 0.9
B. 1.0
C. 0.8
D. 0.0

Solution

  1. Step 1: Prepare shifted data slices

    data[:-1] = [2,4,6,8], data[1:] = [4,6,8,10]
  2. Step 2: Calculate correlation coefficient

    These slices are perfectly linearly increasing, so correlation is 1.0.
  3. Final Answer:

    1.0 -> Option B
  4. Quick Check:

    Perfect linear increase = autocorrelation 1.0 [OK]
Hint: Perfect linear sequences have autocorrelation 1.0 [OK]
Common Mistakes:
  • Calculating correlation with full data instead of shifted slices
  • Confusing correlation with difference or ratio
  • Rounding errors leading to wrong decimals
4. The following code attempts to compute autocorrelation at lag 2 but gives an error. What is the error?
import numpy as np
data = [1, 3, 5, 7, 9]
result = np.corrcoef(data[:-2], data[2:])[0,2]
medium
A. IndexError because index 2 is out of bounds for the correlation matrix
B. TypeError because data is a list, not a numpy array
C. ValueError because data slices have different lengths
D. No error, code runs correctly

Solution

  1. Step 1: Analyze np.corrcoef output shape

    np.corrcoef returns a 2x2 matrix for two input arrays, so valid indices are 0 or 1.
  2. Step 2: Check indexing in code

    Accessing [0,2] is invalid and causes IndexError.
  3. Final Answer:

    IndexError because index 2 is out of bounds for the correlation matrix -> Option A
  4. Quick Check:

    Correlation matrix max index = 1, so index 2 causes error [OK]
Hint: Correlation matrix for two arrays is 2x2, max index 1 [OK]
Common Mistakes:
  • Assuming list input causes TypeError
  • Thinking slices have different lengths (they are equal)
  • Believing code runs without error
5. You have daily sales data showing a weekly pattern. How can autocorrelation analysis help you detect this seasonality?
hard
A. By plotting sales against time without any lag analysis
B. By calculating the average sales over the entire dataset
C. By computing autocorrelation at lag 7 to check if sales on a day relate to sales 7 days before
D. By computing autocorrelation only at lag 1

Solution

  1. Step 1: Understand weekly seasonality

    Weekly seasonality means patterns repeat every 7 days.
  2. Step 2: Use autocorrelation at lag 7

    Computing autocorrelation at lag 7 checks if sales today relate to sales 7 days ago, revealing weekly patterns.
  3. Final Answer:

    By computing autocorrelation at lag 7 to check if sales on a day relate to sales 7 days before -> Option C
  4. Quick Check:

    Weekly pattern detected by lag 7 autocorrelation [OK]
Hint: Match lag to season length to find repeating patterns [OK]
Common Mistakes:
  • Using lag 1 only misses weekly pattern
  • Ignoring lag and just averaging data
  • Plotting without lag analysis misses seasonality