Skip to content

Instantly share code, notes, and snippets.

@diogojc
diogojc / combinations.py
Created April 2, 2012 17:37
Generates every possible combination from a set of discrete variables
#!/usr/bin/python
def combinations(S):
"""
Generates every possible combination from a set of discrete variables
Arguments
---------
S: Cardinality of variable space. When S = [2, 3, 4] variables 1, 2 and
@diogojc
diogojc / multivariateGaussian.py
Created January 1, 2012 22:51
Density estimation using multivariate gaussians
import numpy as np
import matplotlib.pyplot as plt
def params(X):
"""
Calculates the mean vector and covariance matrix for the given data.
Arguments
---------
@diogojc
diogojc / cf.py
Created December 28, 2011 23:10
Regression based collaborative filtering
import numpy as np
from scipy.optimize import fmin_cg
def cost(p, Y, R, alpha):
"""
Calculates collaborative filtering cost function.
Arguments
@diogojc
diogojc / ridge.py
Created December 25, 2011 21:11
Ridge Regression
#!/usr/bin/python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
class RidgeRegressor(object):
"""
Linear Least Squares Regression with Tikhonov regularization.
@diogojc
diogojc / powerlaw.py
Created November 25, 2011 19:55
parameterizing and plotting Power Laws in python (Zipf example)
import numpy as np
def powerLaw(y, x):
"""
'When the frequency of an event varies as power of some attribute of that
event the frequency is said to follow a power law.' (wikipedia)
This is represented by the following equation, where c and alpha are
constants:
y = c . x ^ alpha
@diogojc
diogojc / pagerank.py
Created November 3, 2011 23:11
python implementation of pagerank
import numpy as np
from scipy.sparse import csc_matrix
def pageRank(G, s = .85, maxerr = .001):
"""
Computes the pagerank for each of the n states.
Used in webpage ranking and text summarization using unweighted
or weighted transitions respectively.