Skip to content

Instantly share code, notes, and snippets.

@seberg
seberg / new-fancy-indexing.rst
Last active August 29, 2015 14:24
NEP: New ways to index NumPy arrays
import numpy as np
def object_einsum(string, *arrays):
"""Simplified object einsum, not as much error checking
does not support "..." or list input and will see "...", etc. as three times
an axes identifier, tries normal einsum first!
NOTE: This is untested, and not fast, but object type is
never really fast anyway...
@seberg
seberg / vectorized_percentile.py
Last active March 18, 2019 21:53
Vectorized version of percentile
import numpy as np
from numpy import asarray, add, rollaxis, sort, arange
def percentile(a, q, limit=None, interpolation='linear', axis=None,
out=None, overwrite_input=False):
"""
Compute the qth percentile of the data along the specified axis.
Returns the qth percentile of the array elements.
@seberg
seberg / rolling_window.py
Created October 10, 2012 14:38
Multidimensional rolling_window for numpy
def rolling_window(array, window=(0,), asteps=None, wsteps=None, axes=None, toend=True):
"""Create a view of `array` which for every point gives the n-dimensional
neighbourhood of size window. New dimensions are added at the end of
`array` or after the corresponding original dimension.
Parameters
----------
array : array_like
Array to which the rolling window is applied.
window : int or tuple
@seberg
seberg / correlate_with_einsum+stride_tricks.py
Created August 23, 2012 00:07
Numpy tricks I stumbled upon
"""Impressive little thing how einsum + stride_tricks can beat numpys build in C functions
for correlate (for large data). (ok depending on the implementation of np.correlate, the
comparison is not fair, but still rather impressive that you can get comparable speeds)
"""
import numpy as np
import stride_tricks as st # stride_tricks.py gist
a = np.random.random((100,100,100)).ravel()
stamp = np.random.random((3,3,3)).ravel()
@seberg
seberg / stride_tricks.py
Created August 22, 2012 22:39
numy stride tricks based functions for changing the shape and axis order of arrays.
import numpy as np
from collections import defaultdict as _dd
def rolling_window(array, window=(0,), asteps=None, wsteps=None, axes=None, toend=True):
"""Create a view of `array` which for every point gives the n-dimensional
neighbourhood of size window. New dimensions are added at the end of
`array` or after the corresponding original dimension.
Parameters
----------