📖 Scenario: You are analyzing two simple time series data sets representing daily sales of two products in a small store. You want to find how these two sales patterns relate to each other by calculating their correlation.
🎯 Goal: Build a Python program that uses scipy.correlate to find the correlation between two lists of sales data.
📋 What You'll Learn
Create two lists named sales_product_a and sales_product_b with exact daily sales values.
Create a variable named mode with the value 'full' to configure the correlation.
Use scipy.correlate with sales_product_a, sales_product_b, and mode to calculate the correlation and store it in correlation_result.
Print the correlation_result to see the correlation values.
💡 Why This Matters
🌍 Real World
Correlation helps find relationships between two sets of data, like sales of two products over time, to understand if they move together.
💼 Career
Data scientists and analysts use correlation to find patterns and connections in data, which helps businesses make better decisions.
Progress0 / 4 steps
1
Create sales data lists
Create two lists called sales_product_a and sales_product_b with these exact values: sales_product_a = [10, 20, 30, 40, 50] and sales_product_b = [15, 25, 35, 45, 55].
SciPy
Hint
Use square brackets to create lists and separate numbers with commas.
2
Set correlation mode
Create a variable called mode and set it to the string 'full' to specify the correlation mode.
SciPy
Hint
Assign the string 'full' to the variable mode.
3
Calculate correlation using scipy.correlate
Import correlate from scipy.signal. Then use correlate with sales_product_a, sales_product_b, and mode to calculate the correlation. Store the result in a variable called correlation_result.
SciPy
Hint
Use from scipy.signal import correlate to import. Then call correlate() with the two lists and the mode.
4
Print the correlation result
Write a print statement to display the value of correlation_result.
SciPy
Hint
Use print(correlation_result) to show the correlation values.
Practice
(1/5)
1. What does the scipy.signal.correlate function measure between two arrays?
easy
A. The difference between the maximum values of two arrays
B. The sum of elements in both arrays
C. The similarity between two arrays at different shifts
D. The product of the two arrays element-wise
Solution
Step 1: Understand correlation concept
Correlation measures how similar two data sets are when one is shifted over the other.
Step 2: Identify function purpose
scipy.signal.correlate computes this similarity at all possible shifts.
Final Answer:
The similarity between two arrays at different shifts -> Option C
Quick Check:
Correlation = similarity at shifts [OK]
Hint: Correlation = similarity at shifts between arrays [OK]
Common Mistakes:
Confusing correlation with element-wise multiplication
Thinking it sums elements instead of comparing shifts
Mixing correlation with difference or subtraction
2. Which of the following is the correct way to import the correlate function from scipy.signal?
easy
A. import correlate from scipy.signal
B. from scipy.signal import correlate
C. from scipy import correlate
D. import scipy.correlate
Solution
Step 1: Recall correct import syntax
Python imports functions using from module import function syntax.
Step 2: Match function and module
correlate is in scipy.signal, so correct import is from scipy.signal import correlate.
Final Answer:
from scipy.signal import correlate -> Option B
Quick Check:
Correct import syntax = from scipy.signal import correlate [OK]
Hint: Use 'from scipy.signal import correlate' to import [OK]
Incorrect order like 'import correlate from scipy.signal'
3. What is the output of this code?
import numpy as np
from scipy.signal import correlate
x = np.array([1, 2, 3])
y = np.array([0, 1, 0.5])
result = correlate(x, y, mode='full')
print(result)
medium
A. [0.5 2. 3.5 3. 0. ]
B. [0. 1. 2.5 3. 1.5]
C. [0. 1. 2. 3. 4.]
D. [1. 2. 3. 0. 0.5]
Solution
Step 1: Understand correlation calculation
Correlation slides the reversed y over x and sums element-wise products at each shift. Equivalent to np.convolve(x, np.flip(y), mode='full').
You want to find the shift where sensor2 best matches sensor1 using correlation. Which code snippet correctly finds the shift index with the highest correlation?
hard
A. import numpy as np
from scipy.signal import correlate
corr = correlate(sensor1, sensor2, mode='full')
shift = np.argmax(corr) - (len(sensor2) - 1)
print(shift)
B. import numpy as np
from scipy.signal import correlate
corr = correlate(sensor2, sensor1, mode='valid')
shift = np.argmax(corr)
print(shift)
C. import numpy as np
from scipy.signal import correlate
corr = correlate(sensor1, sensor2, mode='same')
shift = np.argmax(corr)
print(shift)
D. import numpy as np
from scipy.signal import correlate
corr = correlate(sensor1, sensor2)
shift = np.argmin(corr)
print(shift)
Solution
Step 1: Use 'full' mode for complete correlation
To find best matching shift, full correlation output is needed to cover all shifts.
Step 2: Calculate shift index correctly
Shift index = position of max correlation minus (length of second array - 1) to center shifts around zero.
Step 3: Verify code correctness
import numpy as np
from scipy.signal import correlate
corr = correlate(sensor1, sensor2, mode='full')
shift = np.argmax(corr) - (len(sensor2) - 1)
print(shift) uses full mode, np.argmax, and correct shift calculation.
Final Answer:
import numpy as np
from scipy.signal import correlate
corr = correlate(sensor1, sensor2, mode='full')
shift = np.argmax(corr) - (len(sensor2) - 1)
print(shift) -> Option A
Quick Check:
Full mode + argmax - (len(y)-1) = correct shift [OK]
Hint: Use full mode and adjust argmax by (len(y)-1) for shift [OK]
Common Mistakes:
Using 'valid' or 'same' mode which limits shifts
Using argmin instead of argmax
Not adjusting shift index by array length
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.