Skip to content

Instantly share code, notes, and snippets.

View mblondel's full-sized avatar

Mathieu Blondel mblondel

View GitHub Profile
@mblondel
mblondel / perceptron.py
Last active April 21, 2024 13:42
Kernel Perceptron
# Mathieu Blondel, October 2010
# License: BSD 3 clause
import numpy as np
from numpy import linalg
def linear_kernel(x1, x2):
return np.dot(x1, x2)
def polynomial_kernel(x, y, p=3):
@mblondel
mblondel / statistical_tests.py
Last active April 21, 2024 13:42
t-test and wilcoxon-test examples in Python
# Mathieu Blondel, February 2012
# License: BSD 3 clause
# Port to Python of examples in chapter 5 of
# "Introductory Statistics with R" by Peter Dalgaard
import numpy as np
from scipy.stats import ttest_1samp, wilcoxon, ttest_ind, mannwhitneyu
# daily intake of energy in kJ for 11 women
@mblondel
mblondel / mc_pi.py
Created July 25, 2010 02:39
Compute pi by MCMC
from random import random
"""
Find pi by the Monte-Carlo method.
area of a circle = pi r^2
area of a square = (2r)^2 = 4 r^2
Perform random uniform sampling between -1 and 1.
The proportion of points in the unit circle is:
@mblondel
mblondel / svm.py
Last active April 21, 2024 13:41
Support Vector Machines
# Mathieu Blondel, September 2010
# License: BSD 3 clause
import numpy as np
from numpy import linalg
import cvxopt
import cvxopt.solvers
def linear_kernel(x1, x2):
return np.dot(x1, x2)
@mblondel
mblondel / kmeans.py
Last active April 21, 2024 13:41
Fuzzy K-means and K-medians
# Copyright Mathieu Blondel December 2011
# License: BSD 3 clause
import numpy as np
import pylab as pl
from sklearn.base import BaseEstimator
from sklearn.utils import check_random_state
from sklearn.cluster import MiniBatchKMeans
from sklearn.cluster import KMeans as KMeansGood
@mblondel
mblondel / kernel_sgd.py
Last active April 21, 2024 13:41
Kernel SGD
# Mathieu Blondel, May 2012
# License: BSD 3 clause
import numpy as np
def euclidean_distances(X, Y=None, Y_norm_squared=None, squared=False):
XX = np.sum(X * X, axis=1)[:, np.newaxis]
YY = np.sum(Y ** 2, axis=1)[np.newaxis, :]
distances = np.dot(X, Y.T)
distances *= -2
@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 / projection_simplex.py
Last active March 11, 2024 12:07
Projection onto the simplex
"""
License: BSD
Author: Mathieu Blondel
Implements three algorithms for projecting a vector onto the simplex: sort, pivot and bisection.
For details and references, see the following paper:
Large-scale Multiclass Support Vector Machine Training via Euclidean Projection onto the Simplex
Mathieu Blondel, Akinori Fujino, and Naonori Ueda.
@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 / letor_metrics.py
Last active January 14, 2024 16:27
Learning to rank metrics.
# (C) Mathieu Blondel, November 2013
# License: BSD 3 clause
import numpy as np
def ranking_precision_score(y_true, y_score, k=10):
"""Precision at rank k
Parameters