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
Median and Uniform Filters with SciPy
📖 Scenario: You have a noisy 1D signal from a sensor. You want to clean it up to see the true pattern better.
🎯 Goal: Apply median and uniform filters to smooth the noisy signal and compare the results.
📋 What You'll Learn
Create a noisy signal as a list
Set a filter size variable
Apply median and uniform filters using scipy.ndimage
Print the filtered results
💡 Why This Matters
🌍 Real World
Sensors often produce noisy data. Filters help clean data so we can understand the true signal.
💼 Career
Data scientists use filtering techniques to preprocess data before analysis or machine learning.
Progress0 / 4 steps
1
Create a noisy signal
Create a list called signal with these exact values: [2, 80, 6, 3, 2, 75, 5, 3, 2]
SciPy
Hint
Use square brackets and commas to create the list exactly as shown.
2
Set the filter size
Create a variable called filter_size and set it to 3
SciPy
Hint
Just assign the number 3 to the variable filter_size.
3
Apply median and uniform filters
Import median_filter and uniform_filter from scipy.ndimage. Then create two variables: median_filtered and uniform_filtered. Use median_filter on signal with size filter_size for median_filtered. Use uniform_filter on signal with size filter_size for uniform_filtered.
SciPy
Hint
Use the exact function names and variable names as shown.
4
Print the filtered results
Print median_filtered and uniform_filtered each on its own line.
SciPy
Hint
Use two print statements, one for each filtered array.
Practice
(1/5)
1. What is the main purpose of applying a median filter to a dataset?
easy
A. To find the maximum value in the dataset
B. To calculate the average of all values in the dataset
C. To remove noise by replacing each value with the middle value in its neighborhood
D. To sort the dataset in ascending order
Solution
Step 1: Understand median filter function
A median filter replaces each data point with the median (middle) value of its neighbors, reducing spikes and noise.
Step 2: Compare options with median filter purpose
Only To remove noise by replacing each value with the middle value in its neighborhood describes replacing values with the middle value in a neighborhood, which matches the median filter's role.
Final Answer:
To remove noise by replacing each value with the middle value in its neighborhood -> Option C
Quick Check:
Median filter = middle value replacement [OK]
Hint: Median filter picks middle value to reduce spikes [OK]
Common Mistakes:
Confusing median filter with averaging
Thinking median filter sorts entire dataset
Assuming median filter finds max or min values
2. Which of the following is the correct way to import the median filter function from scipy?
easy
A. from scipy.ndimage import median_filter
B. import scipy.median_filter
C. from scipy import median_filter
D. import median_filter from scipy
Solution
Step 1: Recall scipy median filter import syntax
The median_filter function is in scipy.ndimage module, so it is imported as from scipy.ndimage import median_filter.
Step 2: Check each option's correctness
from scipy.ndimage import median_filter matches the correct syntax. The other options are invalid Python import statements.
Final Answer:
from scipy.ndimage import median_filter -> Option A
Quick Check:
Correct import = from scipy.ndimage import median_filter [OK]
Hint: Import median_filter from scipy.ndimage module [OK]
Common Mistakes:
Trying to import median_filter directly from scipy
Using invalid import syntax
Confusing module names
3. What is the output of this code snippet?
import numpy as np
from scipy.ndimage import uniform_filter
arr = np.array([1, 2, 3, 4, 5])
result = uniform_filter(arr, size=3)
print(result)
medium
A. [1 2 3 4 5]
B. [1.66666667 2. 3. 4. 4.33333333]
C. [1 2 3 3 3 3]
D. [1 2 2 3 4]
Solution
Step 1: Understand uniform_filter with size=3
The uniform_filter computes the average over a sliding window of size 3. For edges, it uses 'reflect' mode by default.
Step 2: Calculate each element in result
Using reflect padding: - index 0: [2, 1, 2] avg = 1.66666667 - index 1: [1, 2, 3] avg = 2.0 - index 2: [2, 3, 4] avg = 3.0 - index 3: [3, 4, 5] avg = 4.0 - index 4: [4, 5, 4] avg = 4.33333333 print(result) shows [1.66666667 2. 3. 4. 4.33333333]
Final Answer:
[1.66666667 2. 3. 4. 4.33333333] -> Option B
Quick Check:
Uniform filter smooths values with reflect padding [OK]
Hint: Uniform filter averages neighbors in window size [OK]
Common Mistakes:
Confusing median_filter output with uniform_filter
Ignoring edge effects in uniform_filter
Expecting original array unchanged
4. Identify the error in this code that applies a median filter:
import numpy as np
from scipy.ndimage import median_filter
arr = np.array([1, 2, 100, 4, 5])
filtered = median_filter(arr, size=0)
print(filtered)
medium
A. missing import for numpy
B. median_filter requires 2D arrays only
C. numpy array must be float type
D. size parameter cannot be zero
Solution
Step 1: Check median_filter size parameter
The size parameter defines the window size and must be a positive integer. Zero is invalid and causes an error.
Step 2: Verify other code parts
Array is 1D which is allowed. numpy is imported. Data type can be int. So only size=0 is wrong.
Final Answer:
size parameter cannot be zero -> Option D
Quick Check:
Window size > 0 for median_filter [OK]
Hint: Window size must be positive integer [OK]
Common Mistakes:
Using zero or negative size values
Assuming median_filter only works on 2D arrays
Forgetting to import numpy
5. You have a noisy 2D image array with salt-and-pepper noise. Which filter and parameters would best reduce noise while preserving edges?