Skip to content

Instantly share code, notes, and snippets.

View GaelVaroquaux's full-sized avatar

Gael Varoquaux GaelVaroquaux

View GitHub Profile
@GaelVaroquaux
GaelVaroquaux / SparsePCA.py
Created January 28, 2011 10:12
A sparse PCA implementation based on the LARS algorithm
import time
import sys
import numpy as np
from numpy.lib.stride_tricks import as_strided
from math import sqrt
from scipy import linalg
from scikits.learn.linear_model import Lasso, lars_path
from joblib import Parallel, delayed
@GaelVaroquaux
GaelVaroquaux / concat_img.py
Created March 10, 2011 16:23
Preprocess some resting state fMRI data with NiPype
"""A helper node to concatenate images in nipype"""
import os
from nipype.interfaces.base import TraitedSpec, File, CommandLineInputSpec, CommandLine
from nipype.utils.misc import isdefined
class ConcatImgInputSpec(CommandLineInputSpec):
in_file1 = File(exists=True, argstr="-append %s",
position=1, mandatory=True)
@GaelVaroquaux
GaelVaroquaux / compute_bins.py
Created June 6, 2011 10:46
Computing bins to have a somewhat equal partitioning of the data
import numpy as np
def _compute_bins(x, n_bins=10):
""" Find optimal bins from a univariate distribution
Parameters
===========
x: 1D array-like
Samples
n_bins: integer
@GaelVaroquaux
GaelVaroquaux / spectral_clustering.py
Created September 25, 2011 18:33
With a random shapeless affinity matrix, spectral clustering does not work: the spectrum of the laplacian is flat.
#!/usr/bin/env python
# gvaroquaux (adapted from tdh.net gmail com)
# 31.August.2011
# try clustering module in scikits.learn
import numpy as np
from scipy import linalg
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.cluster import SpectralClustering
@GaelVaroquaux
GaelVaroquaux / 00README.rst
Last active September 15, 2023 03:58
Copy-less bindings of C-generated arrays with Cython

Cython example of exposing C-computed arrays in Python without data copies

The goal of this example is to show how an existing C codebase for numerical computing (here c_code.c) can be wrapped in Cython to be exposed in Python.

The meat of the example is that the data is allocated in C, but exposed in Python without a copy using the PyArray_SimpleNewFromData numpy

@GaelVaroquaux
GaelVaroquaux / lighting_talk.rst
Created December 5, 2011 10:29
Bootstrapping a SciPy-related community project

Bootstrapping a SciPy-related community project

This document is a lightning talk: it only gives pointers, you need to Google and read references

Goal

@GaelVaroquaux
GaelVaroquaux / bench.py
Created January 2, 2012 16:15
Benching I/O speed with numpy, joblib, NiBabel and pytables
"""
Benching I/O with joblib and other libraries. Comment and
un-comment what you are interested in.
Warning: this is slow, and the benchs are easily offset by other disk
activity.
"""
import os
import time
import shutil
@GaelVaroquaux
GaelVaroquaux / bench.py
Created August 6, 2012 13:29
Benching neurimaging I/O
"""
Benching I/O with joblib and other libraries. Comment and
un-comment what you are interested in.
Warning: this is slow, and the benchs are easily offset by other disk
activity.
"""
import os
import time
import shutil
@GaelVaroquaux
GaelVaroquaux / bench_elastic_net.py
Created September 24, 2012 13:08
Benchmark of elastic net on a very sparse system
# Licence : BSD
# Author: Gael Varoquaux
from time import time
import numpy as np
import pylab as pl
from scipy import linalg, ndimage
from sklearn import linear_model
@GaelVaroquaux
GaelVaroquaux / sklearn_EN_example.py
Created September 29, 2012 16:16 — forked from arokem/sklearn_EN_example.py
Demonstrate that ElasticNet doesn't fit all the way, even for rather strict convergence criteria
import numpy as np
import sklearn.linear_model as lm
X = np.array([[ -2.18252949e-01, -8.21949578e-02, -4.64055457e-02,
-1.78405908e-01, -1.93863740e-01, 5.30667625e-02,
1.83851107e-01, 1.23426449e-01, 1.97396315e-01,
-2.12615837e-01, 7.06452283e-02, -1.94509405e-01,
-9.77929516e-02, 2.07135018e-01, -3.40368338e-02,
2.02970673e-01, -2.28669466e-01, 4.17398420e-02,
1.80163132e-01, 3.24254938e-02, -2.41198452e-03,