Skip to content

Instantly share code, notes, and snippets.

@eickenberg
eickenberg / gist:4136238
Created November 23, 2012 15:57
Patch extractor for numpy arrays
import numpy as np
from numpy.lib.stride_tricks import as_strided
import numbers
def make_patches(arr, patch_shape=2, extraction_step=1):
arr_ndim = arr.ndim
if isinstance(patch_shape, numbers.Number):
patch_shape = tuple([patch_shape] * arr_ndim)
@eickenberg
eickenberg / ridge_sgd_comparison.py
Last active August 29, 2015 14:02
Scaling comparison Ridge vs SGDRegressor(penalty='l2')
import numpy as np
from sklearn.linear_model import Ridge, SGDRegressor
rng = np.random.RandomState(42)
n_samples, n_features = 2000, 50
X = rng.randn(n_samples, n_features)
w = rng.randn(n_features)
@eickenberg
eickenberg / ridge_path.py
Created July 13, 2014 17:07
Ridge Path sketches
import numpy as np
# X.dot(X.T) + alpha * eye = U.dot(S ** 2 + alpha).dot(U.T)
# X.T.dot(inv(X.dot(X.T) + alpha * eye)) = V.dot(S / (S **2 + alpha).dot(U.T))
def _linear_kernel_ridge_path_svd(
U_train, S_train, VT_train, Y_train, alphas, X_test=None):
UTY = U_train.T.dot(Y_train)
@eickenberg
eickenberg / gist:7533ee9dc9fb2a314eaa
Created October 10, 2014 11:51
traceback: SystemError: NULL result without error in PyObject_Call
---------------------------------------------------------------------------
SystemError Traceback (most recent call last)
/home/parietal/eickenbe/software/python_pkg/anaconda/lib/python2.7/site-packages
/IPython/utils/py3compat.pyc in execfile(fname, *where)
202 else:
203 filename = fname
--> 204 __builtin__.execfile(filename, *where)
/home/parietal/eickenbe/code/ofv/ofb.py in <module>()
369 dump_theano_r_layer(l)
@eickenberg
eickenberg / coordinate_descent.py
Last active November 18, 2015 15:27
Dynamic screening rules, coordinate descent
# Coordinate descent algorithm for Lasso in pure python
# Goal: evaluate speed up potential when using dynamic screening
# rules every few coordinate sweeps, on all types of design
# See Bonnefoy et al., 2014, https://hal.inria.fr/hal-00880787v4
# Author: Michael Eickenberg, michael.eickenberg@nsup.org
import numpy as np
def screen_dictionary(X, residual, y, alpha,
current_support=None,
current_radius=None,
@eickenberg
eickenberg / svd_X_vs_svd_XTX.py
Last active August 29, 2015 14:10
Comparing decompositions of X and X.T.dot(X)
import numpy as np
rng = np.random.RandomState(42)
n_samples, n_features = 100, 50
X = rng.randn(n_samples, n_features)
V1, S1, VT1 = np.linalg.svd(X.T.dot(X), full_matrices=True)
U2, S2, VT2 = np.linalg.svd(X, full_matrices=True)
@eickenberg
eickenberg / theano_mgrid.py
Last active August 14, 2016 21:53
mgrid and ogrid for theano
# Implements functionality corresponding to numpy.mgrid / numpy.ogrid for symbolic theano variables
# Author: Michael Eickenberg, michael.eickenberg@nsup.org
import theano
import theano.tensor as T
class _nd_grid(object):
"""Implements the mgrid and ogrid functionality for theano tensor
variables.
@eickenberg
eickenberg / fancy_max_pool.py
Last active August 29, 2015 14:13
Maxpooling with arbitrary pooling strides and pooling shapes
# Maxpooling with arbitrary pooling strides and pooling shapes
# Based on theano.tensor.signal.downsample.max_pool_2d. This
# operation is repeated the minimum necessary times to account for
# all stride steps.
#Author: Michael Eickenberg, michael.eickenberg@nsup.org
import theano
import numpy as np
@eickenberg
eickenberg / theano_l1.py
Last active August 29, 2015 14:14
checking theano l1 gradient == sign
import theano
import theano.tensor as T
import numpy as np
# 0 and some arbitrarily small positive and negative numbers
test_vector = np.array(
[0., 1., -1., .1, -.1, 10., -10., 1e-8, -1e-8,
1e-10, -1e-10, 1e-16, -1e-16]).astype(np.float32)
@eickenberg
eickenberg / spam.py
Created February 2, 2015 17:26
An implementation of Sparse Additive Models in Coordinate Descent
# Author Michael Eickenberg <michael.eickenberg@nsup.org>, Fabian Pedregosa
# Coded in 2012, another era, pure python, no guarantees for 1000% correctness or speed
# requires loess.py
import numpy as np
from loess import lowess
VERBOSE = 100