0
0
SciPydata~10 mins

Median and uniform filters in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Median and uniform filters
Start with input data array
Choose filter type: median or uniform
Apply filter over sliding window
For each window position:
Median filter
Sort values in window
Pick middle value
Replace center element with filtered value
Move window to next position
Repeat until entire array filtered
Output filtered array
The filter slides a window over data. Median filter picks the middle value in the window. Uniform filter replaces with the average. This smooths or cleans data.
Execution Sample
SciPy
import numpy as np
from scipy.ndimage import median_filter, uniform_filter

arr = np.array([1, 2, 80, 4, 5])
med = median_filter(arr, size=3, mode='nearest')
uni = uniform_filter(arr, size=3, mode='nearest')
This code applies median and uniform filters with window size 3 on a 1D array.
Execution Table
StepWindow PositionWindow ValuesMedian Filter OutputUniform Filter Output
1Indices 0-2[1, 2, 80]227.6667
2Indices 1-3[2, 80, 4]428.6667
3Indices 2-4[80, 4, 5]529.6667
4Edges handled by paddingN/A1 (left), 5 (right)1.3333 (left), 4.6667 (right)
5Final filtered arraysN/A[1, 2, 4, 5, 5][1.3333, 27.6667, 28.6667, 29.6667, 4.6667]
💡 All window positions processed; edges handled by padding; final filtered arrays produced.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arr[1, 2, 80, 4, 5][1, 2, 80, 4, 5][1, 2, 80, 4, 5][1, 2, 80, 4, 5][1, 2, 80, 4, 5]
median_filter outputN/A[?, 2, ?, ?, ?][?, 2, 4, ?, ?][?, 2, 4, 5, ?][1, 2, 4, 5, 5]
uniform_filter outputN/A[?, 27.6667, ?, ?, ?][?, 27.6667, 28.6667, ?, ?][?, 27.6667, 28.6667, 29.6667, ?][1.3333, 27.6667, 28.6667, 29.6667, 4.6667]
Key Moments - 2 Insights
Why does the median filter output at the edges differ from the middle?
Edges have fewer neighbors, so padding is used. This means the window includes repeated edge values, affecting the median. See execution_table row 4.
Why is the uniform filter output a decimal while the median filter output is an integer?
Uniform filter calculates the average, which can be a decimal. Median filter picks a middle value from the window, which is an existing integer from the array. See execution_table rows 1-3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the median filter output at window position indices 1-3?
A2
B4
C5
D80
💡 Hint
Check execution_table row 2 under 'Median Filter Output'
At which step does the uniform filter output first include the value 28.6667?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table rows 1-3 under 'Uniform Filter Output'
If the window size changes from 3 to 5, how would the median filter output change?
AIt would use a larger window, so median values might smooth more
BIt would produce the same output as size 3
CIt would only affect uniform filter, not median
DIt would cause an error
💡 Hint
Window size controls how many neighbors are considered, see concept_flow
Concept Snapshot
Median and uniform filters smooth data by sliding a window.
Median filter picks the middle value in the window.
Uniform filter replaces with the average of values.
Edges are handled by padding.
Window size controls smoothing strength.
Full Transcript
Median and uniform filters work by sliding a window over data. For each window, median filter sorts values and picks the middle one, while uniform filter calculates the average. This helps reduce noise or smooth data. Edges are handled by padding to keep window size consistent. The example code applies both filters on a small array with window size 3. The execution table shows how the window moves, what values it sees, and what outputs it produces. Median filter outputs integers from the data, uniform filter outputs averages which can be decimals. Beginners often wonder why edges differ and why outputs differ in type; this is due to padding and calculation methods. Changing window size changes smoothing effect. This visual trace helps understand step-by-step how these filters work.