Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
import os
import numpy as np
from scipy.io import savemat
from collections import defaultdict
def get_yields(rootdir, state, year):
yields = {}
yfile = 'yields-%s-%s.txt' % (state, year)
with open(os.path.join(rootdir, yfile), 'r') as f:
@garydoranjr
garydoranjr / nemenyi.py
Created February 22, 2013 20:51
Implements the formula to compare models with the Nemenyi test.
"""
Implements the formula to compare models with the Nemenyi test. "The performance
of two classifiers is significantly different if the corresponding average ranks
differ by at least the critical difference" from:
Demsar, J. "Statistical comparisons of classifiers over multiple data sets."
The Journal of Machine Learning Research 7 (2006): 1-30.
Critical values taken from:
@garydoranjr
garydoranjr / dataset_load.m
Created October 11, 2012 13:02
MATLAB function to load datasets
function [examples classifications] = dataset_load(dataset_name)
% dataset_load(dataset_name)
% Returns the example feature vectors (as rows) and corresponding
% classifications. Automatically strips the first column (example ids).
% Load dataset into a structure array
mat_struct = load(dataset_name);
% Syntax to get the field of a structure array by name
% http://www.mathworks.com/help/matlab/ref/getfield.html
@garydoranjr
garydoranjr / pad.py
Created October 10, 2012 02:58
Pads your txt msgs
#!/usr/bin/python
PAD = '.......................................................................................................................................................987654321'
if __name__ == '__main__':
from sys import argv
msg = argv[1]
print '%s%s' % (msg, PAD[(len(msg) - len(PAD)):])
@garydoranjr
garydoranjr / witness.py
Created March 20, 2012 16:55
Plot witness f for Gaussian and Laplace PDFs
#!/usr/bin/env python
import numpy as np
import pylab as pl
from scipy.spatial.distance import cdist
SAMPLES = 1.5e5
SIGMA = 0.2
def main():
X = np.random.laplace(size=SAMPLES)
@garydoranjr
garydoranjr / quadprog.py
Created February 21, 2012 20:35
MATLAB style quadprog from CVXOPT qp
from cvxopt import matrix as cvxmat, sparse, spmatrix
from cvxopt.solvers import qp, options
import numpy as np
def quadprog(H, f, Aeq, beq, lb, ub):
"""
minimize:
(1/2)*x'*H*x + f'*x
subject to:
Aeq*x = beq
@garydoranjr
garydoranjr / isprime.s
Created March 22, 2011 21:15
Assembly program that checks if a given number (from stdin) is prime
.data
prime: .string "Prime.\n"
p_len = . - prime
not_prime: .string "Not Prime.\n"
np_len = . - not_prime
buffer: .string " "
.text
.globl main
main:
call _getinput
@garydoranjr
garydoranjr / parens.py
Created February 11, 2011 22:03
Prints almost matched parens
#!/usr/bin/python
from random import random
BE_EVIL = True
MAX_DEPTH = 20
PROB_t = 0.05
PROB_p = 0.4
def production(depth):
depth += 1
@garydoranjr
garydoranjr / max.py
Created November 18, 2010 06:09
Prints a silly status message
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import string
import sys
def recurse(levels, path):
if levels == 0:
left = string.uppercase[path]
right = left + "′"