Skip to content

Instantly share code, notes, and snippets.

@MechCoder
Created June 25, 2014 06:30
Show Gist options
  • Save MechCoder/edda4ff2f04b48fd15ee to your computer and use it in GitHub Desktop.
Save MechCoder/edda4ff2f04b48fd15ee to your computer and use it in GitHub Desktop.
Beching to measure the effect of random descent
import numpy as np
from scipy import signal
rng = np.random.RandomState(42)
n_samples, n_features, k = 500, 1000, 10
h = signal.gaussian(50, 15)
X = signal.convolve2d(np.eye(n_features), h[:, np.newaxis], 'same') # convolutional design
X = X[::n_features // n_samples]
coef0 = rng.randn(n_features)
coef0[np.abs(coef0) < np.sort(np.abs(coef0))[-k]] = 0
y = np.dot(X, coef0)
alpha = 0.001
from sklearn.linear_model import Lasso
clf = Lasso(alpha=alpha, random_state=0)
clf.fit(X, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment