Skip to content

Instantly share code, notes, and snippets.

View mblondel's full-sized avatar

Mathieu Blondel mblondel

View GitHub Profile
@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 / 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 / sparse_multiclass_numba.py
Last active January 27, 2020 14:58
Sparse Multiclass Classification in Numba!
"""
(C) August 2013, Mathieu Blondel
# License: BSD 3 clause
This is a Numba-based reimplementation of the block coordinate descent solver
(without line search) described in the paper:
Block Coordinate Descent Algorithms for Large-scale Sparse Multiclass
Classification. Mathieu Blondel, Kazuhiro Seki, and Kuniaki Uehara.
Machine Learning, May 2013.
@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 / nmf_cd.py
Last active June 12, 2019 20:00
NMF by coordinate descent
"""
NMF by coordinate descent, designed for sparse data (without missing values)
"""
# Author: Mathieu Blondel <mathieu@mblondel.org>
# License: BSD 3 clause
import numpy as np
import scipy.sparse as sp
import numba
@mblondel
mblondel / matrix_sketch.py
Last active February 13, 2019 09:26
Frequent directions algorithm for matrix sketching.
# (C) Mathieu Blondel, November 2013
# License: BSD 3 clause
import numpy as np
from scipy.linalg import svd
def frequent_directions(A, ell, verbose=False):
"""
Return the sketch of matrix A.
@mblondel
mblondel / gaussian_process.py
Created August 14, 2013 12:52
Gaussian Process Regression
"""Gaussian processes"""
# Author: Mathieu Blondel <mathieu@mblondel.org>
# License: BSD 3 clause
import numpy as np
from scipy.linalg import cholesky, solve_triangular
from sklearn.base import BaseEstimator, RegressorMixin
from sklearn.metrics.pairwise import pairwise_kernels
@mblondel
mblondel / logsum.py
Created April 13, 2010 06:39
Logarithm of a sum without underflow
import numpy as np
def _logsum(logx, logy):
"""
Return log(x+y), avoiding arithmetic underflow/overflow.
logx: log(x)
logy: log(y)
Rationale:
@mblondel
mblondel / out_of_scope.py
Last active January 18, 2018 14:13
Out of scope
def test():
print(i)
i = 1
test()
@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