Skip to content

Instantly share code, notes, and snippets.

View joferkington's full-sized avatar
🏠
Working from home

Joe Kington joferkington

🏠
Working from home
View GitHub Profile
import random
import matplotlib.pyplot as plt
from mpldatacursor import datacursor
def main():
accounts = generate_accounts()
plot(accounts)
datacursor(formatter='Account #\n{label}'.format, bbox=dict(alpha=1))
plt.show()
@joferkington
joferkington / masked_hist.py
Created June 10, 2015 21:03
Quick example of masking 0-count portions of a 2D histogram
import numpy as np
import matplotlib.pyplot as plt
x, y = np.random.normal(0, 1, (2, 1000))
counts, ybins, xbins = np.histogram2d(y, x, bins=30)
counts = np.ma.masked_equal(counts, 0)
fig, ax = plt.subplots()
ax.pcolormesh(xbins, ybins, counts)
import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes
fig = plt.figure()
plot_extents = 0, 10, 0, 10
transform = Affine2D().rotate_deg(45)
helper = floating_axes.GridHelperCurveLinear(transform, plot_extents)
ax = floating_axes.FloatingSubplot(fig, 111, grid_helper=helper)
import matplotlib.pyplot as plt
import cPickle as pickle
def main():
fig, ax = plt.subplots()
ax.plot(range(10))
ax.bar(range(10), range(10))
fig2 = copy_figure(fig)
fig2.axes[0].plot(range(10)[::-1], color='red')
import numpy as np
import matplotlib.pyplot as plt
x, y = np.mgrid[:10, :10]
z = np.hypot(x - 4.5, y - 4.5)
#-- Create two masked arrays, one with the upper region and one with the lower.
z1 = np.ma.masked_where(y > 5, z)
# If we just invert the previous masked region, we'll have a gap. There are
# better ways to do this, but for simple cases, we can just ensure a one-pixel
pipeline = sklearn.pipeline.Pipeline([
('Replace nans',
preprocessing.Imputer(strategy='mean')),
('Scale data',
preprocessing.StandardScaler()),
('Feature Selection',
SelectPercentile(f_regression, percentile=60)),
('Regression',
ensemble.ExtraTreesRegressor(
n_estimators=550,
# -*- coding: utf-8 -*-
"""
Benchmark for spline interpolation of a 3D wind field, using the function map_coordinates.
The spline interpolation is about 46000 times slower than a linear interpolation.
It is also about 10000 times slower than an equivalent program, written in the programming
language Julia.
"""
import time
import numpy as np
from scipy import ndimage
import scipy.ndimage as ndimage
import numpy as np
import matplotlib.pyplot as plt
data = np.zeros((40,40), dtype=bool)
data[5:20, 5:20] = True
data[15:35, 15:35] = True
footprint = np.ones((3,3))
outside = ndimage.binary_dilation(data, structure=footprint) - data
@joferkington
joferkington / silly.py
Created August 28, 2015 18:51
Lovely dynamic typing
import random
class BadIdea(object):
def __getattr__(self, key):
return random.randint(0, 1000)
def __setattr__(self, key, value):
pass
x = BadIdea()