Skip to content

Instantly share code, notes, and snippets.

View teoliphant's full-sized avatar
💭
I may be slow to respond.

Travis E. Oliphant teoliphant

💭
I may be slow to respond.
View GitHub Profile
@teoliphant
teoliphant / simple_interpolation.py
Last active April 16, 2023 09:30
Vectorized linear interpolation
# The kernel function is
# roughtly equivalent to new[:] = np.interp(xnew, xvals, yvals)
# But this is broadcast so that it can be run many, many times
# quickly.
# Call using ynew = interp1d(xnew, xdata, ydata)
# ynew.shape will be xnew.shape
# Also, ydata.shape[-1] must be xdata.shape[-1]
# and if ydata or xdata have ndim greater than 1, the initial dimensions
# must be xnew.shape:
@teoliphant
teoliphant / Best median-fit line
Last active October 13, 2016 03:49
Find a good linear relationship between data using the 3 Median Method.
My 11 year-old son is learning about regression in his 9th grade math class.
For them regression is two tables of data and a calculator button.
The graphing calculators also provide a button to find the best "median-fit" line and
the students were asked to find it as well as the regression line. The regression line can
easily be found with numpy.polyfit(x, y, 1).
I did not know of a function to calculate the best "median-fit" line. I had to review a few
online videos to learn exactly what a best "median-fit" line is and found the 3-median method
for determining the best "median-fit" line. It's sometimes called the median median fit.
@teoliphant
teoliphant / rolling.py
Last active October 23, 2019 19:54
Create a function to make a "sliding_window" output array from an input array and a rolling_window size.
import numpy as np
def array_for_sliding_window(x, wshape):
"""Build a sliding-window representation of x.
The last dimension(s) of the output array contain the data of
the specific window. The number of dimensions in the output is
twice that of the input.
Parameters
@teoliphant
teoliphant / circ_mask.png
Last active May 16, 2022 01:30
Create masks (circle and ellipse) starting with a simple level cutoff.
circ_mask.png
@teoliphant
teoliphant / using_numba.ipynb
Created March 10, 2013 15:33
Using Numba to implement a summation
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@teoliphant
teoliphant / life.py
Created August 14, 2012 22:03
Array-oriented version of the game of life
from numpy.random import rand
from numpy import r_, ix_, uint8, roll
import matplotlib.pyplot as plt
import time
size = 200
GRID = (rand(size,size) > 0.75).astype(uint8)
# Rotate indices because the world is round
indx = r_[0:size]
up = roll(indx, -1)