Skip to content

Instantly share code, notes, and snippets.

@bayerj
Created March 31, 2013 11:08
Show Gist options
  • Select an option

  • Save bayerj/5280297 to your computer and use it in GitHub Desktop.

Select an option

Save bayerj/5280297 to your computer and use it in GitHub Desktop.
Window moving average implementation using numba with speed tests.
import time
import numpy as np
from numba import jit, float64, int64
def mean_filter(X, window_size):
filtered = np.empty(X.shape)
starts = window_size * [0] + range(1, X.shape[0] - window_size + 1)
for i in range(X.shape[0]):
start = starts[i]
filtered[i] = (X[start:i + 1]).mean(axis=0)
return filtered
jit_mean_filter = jit(float64[:, :](float64[:, :], int64))(mean_filter)
# Run once to make it compile.
jit_mean_filter(np.empty((3, 1)), 2)
if __name__ == '__main__':
X = np.random.normal(0, 1, (10000, 5))
start = time.time()
for i in range(10):
mean_filter(X, 10)
print 'no jit', time.time() - start
start = time.time()
for i in range(10):
jit_mean_filter(X, 10)
print 'jit', time.time() - start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment