Skip to content

Instantly share code, notes, and snippets.

View mblondel's full-sized avatar

Mathieu Blondel mblondel

View GitHub Profile
# Author: Mathieu Blondel
# License: BSD
import numpy as np
def find_r(w,k):
d = w.shape[0]
beta= np.r_[np.Inf,np.sort(np.abs(w))[::-1]]
@mblondel
mblondel / reconstruct_primal_solution.py
Created September 9, 2022 07:04
Reconstruct transportation plan from dual potentials
# Author: Mathieu Blondel
# License: BSD
import numpy as onp
import scipy
import jaxopt
import jax.numpy as jnp
# Mathieu Blondel, 2022
# BSD license
import numpy as np
from scipy.ndimage import convolve1d
from sklearn.metrics.pairwise import euclidean_distances
def smoothed_conjugate_conv(f, x, eps=1.0):
"""
@mblondel
mblondel / check_convex.py
Last active March 21, 2022 22:25
A small script to get numerical evidence that a function is convex
# Authors: Mathieu Blondel, Vlad Niculae
# License: BSD 3 clause
import numpy as np
def _gen_pairs(gen, max_iter, max_inner, random_state, verbose):
rng = np.random.RandomState(random_state)
# if tuple, interpret as randn
@mblondel
mblondel / projection_simplex_isotonic.py
Created November 2, 2019 22:13
Projection onto the probability simplex using isotonic regression.
# Author: Mathieu Blondel
# License: BSD 3 clause
import numpy as np
from sklearn.isotonic import isotonic_regression
def projection_simplex(x, z=1):
"""
Compute argmin_{p : p >= 0 and \sum_i p_i = z} ||p - x||
@mblondel
mblondel / projection_simplex_vectorized.py
Last active March 11, 2024 12:13
Vectorized projection onto the simplex
# Author: Mathieu Blondel
# License: BSD 3 clause
import numpy as np
def projection_simplex(V, z=1, axis=None):
"""
Projection of x onto the simplex, scaled by z:
P(x; z) = argmin_{y >= 0, sum(y) = z} ||y - x||^2
@mblondel
mblondel / ot_dual_lp.py
Created September 3, 2017 05:36
Optimal transport dual LP
# Author: Mathieu Blondel
# License: BSD 3 clause
import numpy as np
from scipy.optimize import linprog
def dual_lp(a, b, C, verbose=0):
"""Solves the dual optimal transport problem:
@mblondel
mblondel / fista.py
Created November 6, 2016 06:48
Efficient implementation of FISTA
"""
Efficient implementation of FISTA.
"""
# Author: Mathieu Blondel
# License: BSD 3 clause
import numpy as np
@mblondel
mblondel / seminb.py
Created October 28, 2015 12:50
Semi-supervised Naive Bayes
# -*- coding: utf-8 -*-
# Copyright (C) 2010 Mathieu Blondel
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
@mblondel
mblondel / einsum.py
Created May 22, 2015 05:36
Einstein sum notation
import numpy as np
rng = np.random.RandomState(0)
print "Trace"
A = rng.rand(3, 3)
print np.trace(A)
print np.einsum("ii", A)
print