Skip to content

Instantly share code, notes, and snippets.

@diogojc
diogojc / combinations.py
Created Apr 2, 2012
Generates every possible combination from a set of discrete variables
View combinations.py
#!/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 Jan 1, 2012
Density estimation using multivariate gaussians
View multivariateGaussian.py
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 Dec 28, 2011
Regression based collaborative filtering
View cf.py
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 Dec 25, 2011
Ridge Regression
View ridge.py
#!/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 Nov 25, 2011
parameterizing and plotting Power Laws in python (Zipf example)
View powerlaw.py
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 Nov 3, 2011
python implementation of pagerank
View pagerank.py
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.