This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_rdict(d): | |
""" | |
Return a dictionary with each value mapping to a set of keys. | |
""" | |
rd = {} | |
for (key,value) in d.items(): | |
v = rd.setdefault(value, set([])) | |
v.add(key) | |
return rd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_rdict(d): | |
""" | |
Return a dictionary with each value mapping to a set of keys. | |
""" | |
rd = {} | |
for (key,value) in d.items(): | |
if rd.has_key(value): | |
rd[value].add(key) | |
else: | |
rd[value] = set([key]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flat_dict(d): | |
""" | |
Return a dict where sets are replaced by a single element of each | |
set. Compose with create_rdict to reverse a 1-1 dict. | |
""" | |
nd = {} | |
for (key, value) in d.items(): | |
nd[key] = value.pop() | |
return nd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random as pr | |
def flip(p): | |
""" Flip a biased coin. """ | |
if pr.random() < p: | |
return True | |
else: | |
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# J. Stober | |
# May 13, 2011 | |
import numpy as np | |
import numpy.random as nr | |
class EGreedy(object): | |
def __init__(self, k = 10, epsilon = 0.1): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
Author: Jeremy M. Stober | |
Program: GMM.PY | |
Date: Thursday, September 4 2008 | |
Description: Fit a Gaussian Mixture Model with EM. | |
""" | |
import os, sys, getopt, pdb, string | |
from numpy import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A: | |
pass | |
if __name__ == '__main__': | |
import pickle | |
pickle.dump(A(), open("A.pck","w")) | |
a = pickle.load(open("A.pck")) | |
print a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
""" | |
Author: Jeremy M. Stober | |
Program: NORMAL.PY | |
Date: Friday, July 7, 2011 | |
Description: Manipulating normal distributions. | |
""" | |
import numpy as np | |
import numpy.linalg as la |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# Unlike other implementations, this can handle very high dimensional data (like images). | |
def compute_pca(data): | |
m = np.mean(data, axis=0) | |
datac = np.array([obs - m for obs in data]) | |
T = np.dot(datac, datac.T) | |
[u,s,v] = np.linalg.svd(T) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
Author: Jeremy M. Stober | |
Program: TILES.PY | |
Date: Monday, March 31 2008 | |
Description: A simple CMAC implementation. | |
""" | |
import os, sys, getopt, pdb | |
from numpy import * |
OlderNewer