Skip to content

Instantly share code, notes, and snippets.

View agramfort's full-sized avatar

Alexandre Gramfort agramfort

View GitHub Profile
@aweinstein
aweinstein / lasso.py
Created October 3, 2012 18:20
scikitlearn lasso path fat vs thin X matrix
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import Lasso, lars_path
np.random.seed(42)
def gen_data(n, m, k):
X = np.random.randn(n, m)
w = np.zeros((m, 1))
i = np.arange(0, m)
@paolo-losi
paolo-losi / openopt_svm.py
Created September 28, 2012 13:24
OpenOpt SVM
import numpy as np
import FuncDesigner as fd
from openopt import NLP
from sklearn.metrics.pairwise import rbf_kernel, polynomial_kernel
def theta(x):
return 0.5 * (fd.sign(x) + 1.)
@daien
daien / multinomial_logistic_regression.py
Created March 6, 2012 22:01
Multinomial Logistic Regression draft
"""
Multinomial Logistic Regression (MLR)
=====================================
Multiclass-classification with the MLR classifier
Authors: Adrien Gaidon & Jakob Verbeek
"""
@mshivers
mshivers / nnlr
Created February 3, 2012 15:46
L2 Regularized Non-negative logistic regression
import scipy as sp
from scipy import optimize as opt
def nnlr(X, y, C):
"""
Non-negative Logistic Regression with L2 regularizer
"""
def lr_cost(X, y, theta, C):
m = len(y)
return (1./m) * (sp.dot(-y, sp.log(sigmoid(sp.dot(X, theta)))) \
@satra
satra / fs_gifti_xfm.py
Created December 11, 2011 10:04
freesurfer gifti transformation to native space
#DOES NOT WORK: GIFTI READ/WRITE is broken
import nibabel as nb
import nibabel.gifti as gifti
# load combined file
# created with: mris_convert --combinesurfs lh.pial rh.pial ./pial.gii
surf = gifti.read('pial.gii')
#load orig affine transform
ao = nb.load('orig.nii').get_affine()
@mblondel
mblondel / online_variance.py
Created August 2, 2011 11:03
Sample variance in a single pass
def online_mean_variance(iterable):
mN = 0
mM = 0.0
mS = 0.0
for x in iterable:
mN += 1
nextM = mM + (x - mM) / mN
mS += (x - mM) * (x - nextM)
@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 / 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)