Skip to content

Instantly share code, notes, and snippets.

View fabianp's full-sized avatar
🏠
Working from home

Fabian Pedregosa fabianp

🏠
Working from home
View GitHub Profile
@fabianp
fabianp / bench_triangular_solve.py
Created October 29, 2010 21:05
Benchmark solve_triangular from scipy.linalg
import numpy as np
from scipy import linalg
from datetime import datetime
import gc
mu_sec = 1e-6 # number of seconds in one microseconds
solve_time =[]
solve_triangular_time =[]
@fabianp
fabianp / gist:820401
Created February 10, 2011 11:52
Some experiments with PLS
"""
Just some experiments with pls
"""
import numpy as np
m, n = 3, 3
X = np.random.randn(m, n)
Y = np.random.randn(m, n)
@fabianp
fabianp / gist:905457
Created April 6, 2011 10:37
Print error distribution for norm
import pylab as pl
import numpy as np
import scipy.linalg
nrm2, = scipy.linalg.get_blas_funcs(('nrm2',), np.empty(0, dtype=np.float32))
x_min, x_max = -4., 0
y_min, y_max = 0, 4.
h = 134567e-7
@fabianp
fabianp / gist:915461
Created April 12, 2011 13:05
Equality constrained least squares
import numpy as np
def lse(A, b, B, d):
"""
Equality-contrained least squares.
The following algorithm minimizes ||Ax - b|| subject to the
constrain Bx = d.
@fabianp
fabianp / covariance.c
Created April 18, 2011 07:16
numerically stable covariance algorithm
int covariance(int n, int m, double data[], int strides[2], char mode, double matrix[])
/* This algorithm is described in:
* B.P. Welford:
* "Note on a method for calculating corrected sums of squares and products."
* Technometrics 4(3): 419-420 (1962).
* Also see:
* Peter M. Neely:
* "Comparison of several algorithms for computation of means, standard
* deviations and correlation coefficients."
* Communications of the ACM 9(7): 496-499 (1966).
@fabianp
fabianp / gist:927229
Created April 19, 2011 12:08
GridSearch fails when arguments are not hashable
import numpy as np
from scikits.learn import svm, grid_search, datasets
iris = datasets.load_iris()
parameters = {'C': [np.array(1)]}
svr = svm.SVC()
clf = grid_search.GridSearchCV(svr, parameters)
clf.fit(iris.data, iris.target)
print clf.grid_points_scores_
@fabianp
fabianp / gist:934363
Created April 21, 2011 12:16
locally linear embedding - swiss roll example
# -*- coding: utf-8 -*-
"""
===================================
Swiss Roll reduction with LLE
===================================
An illustration of Swiss Roll reduction
with locally linear embedding
"""
@fabianp
fabianp / plot_lle_digits.py
Created May 4, 2011 05:56
Handwritten digits and Locally Linear Embedding
"""
===============================================
Handwritten digits and Locally Linear Embedding
===============================================
An illustration of locally linear embedding on the digits dataset.
"""
# Author: Fabian Pedregosa -- <fabian.pedregosa@inria.fr>
# License: BSD, (C) INRIA 2011
@fabianp
fabianp / gist:1046959
Created June 25, 2011 22:15
Hilbert matrix in Python
"""
Hilbert matrix using numpy. Contains:
- hilb(n, m) : returns the Hilbert matrix of size (n, m)
- invhilb(n) : returns the inverse of the Hilbert matrix of size (n, n)
"""
import numpy as np
from math import factorial
@fabianp
fabianp / gist:1073728
Created July 9, 2011 16:43
Classical Gram-Schmidt algorithm
import numpy as np
from scipy import linalg
def cgs(A):
"""Classical Gram-Schmidt (CGS) algorithm"""
m, n = A.shape
R = np.zeros((n, n))
Q = np.empty((m, n))
R[0, 0] = linalg.norm(A[:, 0])
Q[:, 0] = A[:, 0] / R[0, 0]