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
@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 / map_reduce.py
Created July 26, 2018 17:34
map reduce using nditer
#
# A Map reduce made from ufuncs and with nditer, a C-Api version
# would be very similar (but cleaner in some cases). This still creates
# some unnecessary temporaries, which may or may not slow down things a
# a bit for no reason
#
# WARNING : Mostly untested and I am not sure I would use it except to
# get the idea of how to implement a specialized version maybe.
@seberg
seberg / numpy_casting.md
Last active February 15, 2019 19:29
Numpy Casting Rules

Numpy Promotion Table

This table includes the promotion rules for the basic numeric types. The only suprising rule is that 8-byte integers are considered to cast "safely" to f8 (and complex), which also shows up in these promotion rules (If there was an int128, it would be allowed to safely cast to float128!):

i1 u1 i2 u2 i4 u4 i8 u8 f2 f4 f8 f16 c8 c16 c32
@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 / index_normalization_numpy.py
Created June 13, 2019 01:38
Python code to preprocess non-advanced indexes.
import operator
def normalize_index(index, ndim):
"""Does basic index normalization like numpy, disregards advanced indexing.
Parameters
----------
index : tuple
Indexing tuple to normalize
ndim : int
@seberg
seberg / numpy_logo_design_brainstorm.svg
Created September 20, 2019 23:10
Some brainstorming, based on Inessas designs, as SVG (the SVG is not tidied up, but traced bitmap and just moved around by hand)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@seberg
seberg / README.md
Last active November 27, 2019 18:45
Assessing the NumPy namespace

Downloading both files and running the cells (takes a while) should give you drop downs with the NumPy namespace (dark green names should be the ones that end up in the main numpy namespace).

After clicking one of them, tab will go to the next, and 1, 2, 3 , and 4 will allow to fairly quickly choose how you feel about the function. I left many blank (at least the ones that are not part of the main namespace). Then you press "save" and after gathering a few of them, maybe we can get some nice result out of the data.

Unfortunately the Jupyter widgets are failry slow, although with the keyboard it was not an issue for me.

My take on the categories: