0
0
SciPydata~10 mins

Correlation (correlate) in SciPy - Interactive Code Practice

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

Complete the code to import the correlate function from scipy.signal.

SciPy
from scipy.signal import [1]

result = correlate([1, 2, 3], [0, 1, 0.5])
print(result)
Drag options to blanks, or click blank then click option'
Acorrelate
Bconvolve
Cfftconvolve
Dcorrelation
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'convolve' instead of 'correlate'.
Using a non-existent function name.
2fill in blank
medium

Complete the code to compute the full correlation of two lists.

SciPy
from scipy.signal import correlate

x = [1, 2, 3]
y = [0, 1, 0.5]
result = correlate(x, y, mode=[1])
print(result)
Drag options to blanks, or click blank then click option'
A'valid'
B'partial'
C'full'
D'same'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'valid' which returns only points where signals fully overlap.
Using 'same' which returns output of the same size as the first input.
3fill in blank
hard

Fix the error in the code to correctly compute correlation with 'valid' mode.

SciPy
from scipy.signal import correlate

x = [1, 2, 3, 4]
y = [1, 0, 1]
result = correlate(x, y, mode=[1])
print(result)
Drag options to blanks, or click blank then click option'
A'valid'
B'full'
C'same'
D'invalid'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an invalid mode string causes an error.
Using 'full' or 'same' when 'valid' is required.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each element in x to its correlation with y using 'same' mode.

SciPy
from scipy.signal import correlate

x = [1, 2, 3]
y = [0, 1, 0.5]
correlations = [1]: correlate([[2]], y, mode='same')[1] for [1] in x}
print(correlations)
Drag options to blanks, or click blank then click option'
Aval
Bx
Cy
Delem
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inside and outside the comprehension.
Using 'x' or 'y' as variable names inside the comprehension causing confusion.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each word in words to the correlation length with a pattern, only if the correlation peak is greater than 1.

SciPy
from scipy.signal import correlate

words = ['cat', 'dog', 'bird']
pattern = [1, 0, 1]
result = [1]: len([2]) for [1] in words if max(correlate([ord(c) for c in [1]], pattern)) > 1}
print(result)
Drag options to blanks, or click blank then click option'
Aword
Dpattern
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using 'pattern' instead of the word variable inside the comprehension.