Skip to content

Instantly share code, notes, and snippets.

Avatar

Alex Williams ahwillia

View GitHub Profile
@ahwillia
ahwillia / multi_set_perm_match.py
Last active Jun 26, 2021
Greedy heuristic for finding K-permutations that match a set of K matrices
View multi_set_perm_match.py
import numpy as np
from scipy.optimize import linear_sum_assignment
from sklearn.utils import check_random_state
import scipy.sparse
def perm_alignment(X, Y):
"""
Given two matrix X and Y. Returns sparse matrix P, holding permutation
matrix that minimizes norm(X @ P - Y).
@ahwillia
ahwillia / supervised_pca.py
Created May 5, 2021
Supervised PCA model via manifold optimization
View supervised_pca.py
"""
Supervised PCA model.
Ritchie, A., Balzano, L., Kessler, D., Sripada, C. S., & Scott, C.
(2020). Supervised PCA: A Multiobjective Approach. arXiv:2011.05309.
"""
import numpy as onp
import autograd.numpy as np
from pymanopt.manifolds import Grassmann, Euclidean, Product
@ahwillia
ahwillia / pytorch_nmf.py
Last active Mar 28, 2021
Simple Nonnegative Matrix Factorization in Pytorch
View pytorch_nmf.py
import numpy as np
import torch
import matplotlib.pyplot as plt
from torch_nonneg_linesearch import nonneg_projected_gradient_step
# Data dimensions
m, n = 100, 101
rank = 3
# Data matrix, detached from the graph.
@ahwillia
ahwillia / msplines.py
Created Mar 15, 2021
Generate M-spline functions in Python
View msplines.py
"""
Python code to generate M-splines.
References
----------
Ramsay, J. O. (1988). Monotone regression splines in action.
Statistical science, 3(4), 425-441.
"""
import numpy as np
@ahwillia
ahwillia / simple_cmap.py
Created Feb 18, 2021
Simple formula for constructing a matplotlib colormap
View simple_cmap.py
from matplotlib.colors import LinearSegmentedColormap, colorConverter
def simple_cmap(colors, name='none'):
"""Create a colormap from a sequence of rgb values.
cmap = simple_cmap([(1,1,1), (1,0,0)]) # white to red colormap
cmap = simple_cmap(['w', 'r']) # white to red colormap
cmap = simple_cmap(['r', 'b', 'r']) # red to blue to red
"""
# check inputs
@ahwillia
ahwillia / permtest.py
Last active Feb 7, 2021
Two-sample permutation test in Python
View permtest.py
"""
A simple implementation of a permutation test among two
independent samples.
"""
import numpy as np
from sklearn.utils.validation import check_random_state
from more_itertools import distinct_permutations
from scipy.stats import percentileofscore
from math import factorial
@ahwillia
ahwillia / matlab.py
Created Oct 17, 2020
Helper function for loading nested MATLAB structs into python
View matlab.py
import scipy.io as spio
import numpy as np
def loadmat(filename):
'''
this function should be called instead of direct spio.loadmat
as it cures the problem of not properly recovering python dictionaries
from mat files. It calls the function check keys to cure all entries
which are still mat-objects
'''
@ahwillia
ahwillia / hclust_sort.py
Created Jun 22, 2020
Sort data points by hierarchical clustering
View hclust_sort.py
from sklearn.datasets import make_biclusters
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
def resort_rows_hclust(U):
"""Sorts the rows of a matrix by hierarchical clustering
Parameters:
U (ndarray) : matrix of data
View cca.py
import numpy as np
from sklearn.utils.extmath import randomized_svd
def partial_whiten(X, alpha, eigval_tol=1e-7):
"""
Return regularized whitening transform for a matrix X.
Parameters
----------
@ahwillia
ahwillia / circ_regression.py
Last active Jan 16, 2020
Linear Regression with a circular dependent variable
View circ_regression.py
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats
normcdf = scipy.stats.norm.cdf
normpdf = scipy.stats.norm.pdf
from scipy.linalg import cho_factor, cho_solve
from sklearn.base import BaseEstimator