Skip to content

Instantly share code, notes, and snippets.

View ahwillia's full-sized avatar

Alex Williams ahwillia

View GitHub Profile
@ahwillia
ahwillia / make_nmf_fig.py
Last active April 27, 2023 12:55
PCA and NMF with cross-validation (using Numpy)
from pca_crossval import *
from tqdm import tqdm
import itertools
np.random.seed(1111)
m, n, r = 100, 101, 3
Utrue, Vtrue = rand(m,r), rand(r,n)
data = np.dot(Utrue, Vtrue) + .25*randn(m,n)
data[data < 0] = 0
ranks = []
@ahwillia
ahwillia / elastic_net.py
Created November 8, 2017 02:13
Elastic Net Regression via ADMM in Python
import numpy as np
import scipy.linalg
def elastic_net(A, B, x=None, l1=1, l2=1, lam=1, tol=1e-6, maxiter=10000):
"""Performs elastic net regression by ADMM
minimize ||A*x - B|| + l1*|x| + l2*||x||
Args:
A (ndarray) : m x n matrix
@ahwillia
ahwillia / tsp.py
Created January 23, 2018 04:12
tsp_2opt.py
def reverse_segment(path, n1, n2):
"""Reverse the nodes between n1 and n2.
"""
q = path.copy()
if n2 > n1:
q[n1:(n2+1)] = path[n1:(n2+1)][::-1]
return q
else:
seg = np.hstack((path[n1:], path[:(n2+1)]))[::-1]
brk = len(q) - n1
@ahwillia
ahwillia / cv.py
Last active November 29, 2023 22:19
Cross-validation for matrix factorization models
import numpy as np
from numpy.random import randn, rand
from scipy.optimize import minimize
import matplotlib.pyplot as plt
from nnls import nnlsm_blockpivot as nnlstsq
import itertools
from scipy.spatial.distance import cdist
def censored_lstsq(A, B, M):
"""Solves least squares problem with missing data in B
@ahwillia
ahwillia / bcd_cnmf.py
Created April 10, 2018 23:54
Convolutive NMF by block coordinate descent
import numpy as np
from tqdm import trange
import matplotlib.pyplot as plt
# TODO: subclass np.ndarray?
class ShiftMatrix(object):
"""
Thin wrapper around a numpy matrix to support shifting along the second
axis and padding with zeros.
@ahwillia
ahwillia / poiss_reg.py
Last active September 26, 2018 22:35
Poisson Regression via scipy.optimize
"""
A simple implementation of Poisson regression.
"""
import numpy as np
from scipy.optimize import minimize
n = 1000 # number of datapoints
p = 5 # number of features
@ahwillia
ahwillia / poiss_tf.py
Created September 26, 2018 23:04
Computing hessian-vector products in tensorflow
"""
Computing hessian-vector products in tensorflow.
For simplicity, we demonstrate the idea on a Poisson regression model.
"""
import tensorflow as tf
import numpy as np
from scipy.optimize import minimize
@ahwillia
ahwillia / bandpass.py
Created February 20, 2019 23:29
Bandpass filter in python... (I have no idea why scipy does not provide this)
def bandpass(x, lowcut, highcut, fs, order=5, axis=-1, kind='butter'):
"""
Parameters
----------
x : ndarray
1d time series data
lowcut : float
Defines lower frequency cutoff (e.g. in Hz)
highcut : float
Defines upper frequency cutoff (e.g. in Hz)
@ahwillia
ahwillia / rojo.py
Last active August 29, 2019 18:02
Fast solver for a symmetric tridiagonal circulant linear system in Python.
import numpy as np
from scipy.linalg import solve_circulant, circulant
from numpy.testing import assert_array_almost_equal
import numba
@numba.jit(nopython=True, cache=True)
def rojo_method(c, a, f, x, z):
"""
Solves symmetric, tridiagonal circulant system, assuming diagonal
@ahwillia
ahwillia / kronshuff.py
Last active October 18, 2020 18:21
Kronecker vector product via Shuffle algorithm
"""
References:
- B. Plateau, On the stochastic structure of parallelism and synchronization models for distributed algorithms.
Perform. Eval. Rev., 13 (1985), pp. 147–154.
- Dayar, T., & Orhan, M. C. (2015). On vector-Kronecker product multiplication with rectangular factors.
SIAM Journal on Scientific Computing, 37(5), S526-S543.
"""