{{ message }}

Instantly share code, notes, and snippets.

# Alex Williams ahwillia

Last active Jun 26, 2021
Greedy heuristic for finding K-permutations that match a set of K matrices
View multi_set_perm_match.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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).
Created May 5, 2021
Supervised PCA model via manifold optimization
View supervised_pca.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 """ 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
Last active Mar 28, 2021
Simple Nonnegative Matrix Factorization in Pytorch
View pytorch_nmf.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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.
Last active Oct 12, 2021
Generate M-spline functions in Python
View msplines.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 """ 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
Created Feb 18, 2021
Simple formula for constructing a matplotlib colormap
View simple_cmap.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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
Last active Feb 7, 2021
Two-sample permutation test in Python
View permtest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 """ 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
Created Oct 17, 2020
View matlab.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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 '''
Created Jun 22, 2020
Sort data points by hierarchical clustering
View hclust_sort.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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
Created Feb 11, 2020
Ridge CCA
View cca.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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 ----------
Last active Feb 19, 2022
Linear Regression with a circular dependent variable
View circ_regression.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 # The MIT License (MIT) # # Copyright (c) Alex H. Williams # # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation # files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, # modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the