Skip to content

Instantly share code, notes, and snippets.

View ahwillia's full-sized avatar

Alex Williams ahwillia

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / cv_pca.py
Created June 20, 2017 21:21
Tensorflow PCA with cross-validation
import numpy as np
import tensorflow as tf
from tqdm import tqdm
# N, size of matrix. R, rank of data
N = 100
R = 5
# generate data
W_true = np.random.randn(N,R).astype(np.float32)
@ahwillia
ahwillia / randomized_pca.py
Last active May 10, 2017 17:51
Randomized Matrix Factorization in TensorFlow
import tensorflow as tf # works on version 1.0.0
import numpy as np
from tqdm import trange
# create fake data (low-rank matrix X)
A = np.random.randn(100, 3).astype(np.float32)
B = np.random.randn(3, 100).astype(np.float32)
X = np.dot(A, B)
# create tensorflow variables to predict low-rank decomposition
@ahwillia
ahwillia / pca_alt_min.py
Created October 30, 2016 06:52
Alternating Minimization in Tensorflow (PCA example)
import numpy as np
import tensorflow as tf
# N, size of matrix. R, rank of data
N = 100
R = 5
# generate data
W_true = np.random.randn(N,R)
C_true = np.random.randn(R,N)
@ahwillia
ahwillia / tensorflow_pca.py
Created October 27, 2016 07:25
PCA in TensorFlow
import numpy as np
import tensorflow as tf
# N, size of matrix. R, rank of data
N = 100
R = 5
# generate data
W_true = np.random.randn(N,R)
C_true = np.random.randn(R,N)
@ahwillia
ahwillia / altgradpca.jl
Created September 11, 2016 06:19
Prototype for alternating gradient descent with Optim.jl
using Optim
# low-dimensional data embedded in high-dimension
data = randn(100,5)*randn(5,100);
# a container for the parameters we fit
immutable PCA{T<:Real}
X::Matrix{T}
Y::Matrix{T}
end