Skip to content

Instantly share code, notes, and snippets.

View tansey's full-sized avatar

Wesley Tansey tansey

View GitHub Profile
@tansey
tansey / gist:9753126
Created March 25, 2014 00:54
Numpy Array vs. Numpy Masked Array -- Madness
import numpy as np
import numpy.ma as ma
# Create a 3x3 array in regular numpy
a = np.arange(9).reshape((3,3))
# Get the middle row
b = a[1]
# Change the middle value in the middle row
@tansey
tansey / gist:5b59d73c58587973844d
Created September 24, 2014 15:55
Python-style string formatting in Julia with floating point support
function format(s, args...)
# Python-style string formatting with floating point support
# Note that this is 1-based to be more Julian
result = deepcopy(s)
for (i, x) in enumerate(args)
q = Regex("{$i(:\.([0-9])+f)?}")
next = result
for m in eachmatch(q, result)
val = x
if m.captures[2] != nothing
@tansey
tansey / admm_convergence.py
Created July 31, 2015 22:57
ADMM convergence checker in CVXPY
'''
Implementation of the ADMM convergence rate SDP from Nishihara et al.,
ICML 2015, equation 11.
Code by Wesley Tansey and Sanmi Koyejo
7/31/2015
'''
import cvxpy as cvx
import numpy as np
@tansey
tansey / gist:1424492
Created December 2, 2011 19:23
Markov model definition file for John the Ripper
97=proba1[32]
51=proba2[32*256+35]
44=proba2[32*256+38]
51=proba2[32*256+39]
40=proba2[32*256+48]
35=proba2[32*256+49]
37=proba2[32*256+50]
51=proba2[32*256+51]
44=proba2[32*256+52]
44=proba2[32*256+53]
@tansey
tansey / gist:2410490
Created April 18, 2012 01:50
Lamarckian evolution in SharpNEAT
/// <summary>
/// Saves all phenotypic progress back to the genomes.
/// </summary>
private void PerformLamarkianEvolution(IList<TGenome> genomeList, Func<IAgent, FastCyclicNetwork> networkSelector)
{
for (int i = 0; i < _agents.Length; i++)
{
var agent = _agents[i];
// Get the network for this teacher
@tansey
tansey / gist:2410545
Created April 18, 2012 02:00
Training a NEAT network with backprop in SharpNEAT
double[] inputs = ...
double[] desiredOutputs = ...
// Get the neural network
var network = ((FastCyclicNetwork)blackbox;
// Perform backpropagation through time
network.Train(inputs, desiredOutputs);
class A(object):
def __init__(self, myvar = {}):
self.myvar = myvar
a1 = A()
a1.myvar['foo'] = 1
a2 = A()
a2.myvar['bar'] = 2
class A(object):
def __init__(self, myvar = {}):
self.myvar = myvar
a1 = A()
a1.myvar['foo'] = 1
a2 = A()
a2.myvar['bar'] = 2
@tansey
tansey / gist:6977878
Created October 14, 2013 15:55
2 parameter gibbs sampler with pretty plots
import random
import math
import matplotlib.pyplot as plt
import numpy as np
def acf(x, length=35):
return np.array([1] + [np.corrcoef(x[:-i], x[i:])[0,1] for i in range(1,length)])
# The number of samples to observe
n = 100
@tansey
tansey / plot_with_bands.py
Last active January 3, 2016 17:38
Plot means with confidence (stderr or other metric) bands in a pretty format using matplotlib.
def plot_with_bands(graph_title, means, bands, series, xvals=None, xlabel=None, ylabel=None, subtitle=None, filename='results.pdf'):
colors = ['blue','red','green', 'black', 'yellow', 'orange', 'purple', 'brown'] # max 8 lines
print 'means: {0} bands: {1}'.format(means.shape, bands.shape)
assert(means.shape == bands.shape)
assert(xvals is None or xvals.shape[0] == means.shape[1])
assert(means.shape[1] <= len(colors))
if xvals is None:
xvals = np.arange(means.shape[0])
ax = plt.subplot(111)
plt.ticklabel_format(axis='y', style='plain', useOffset=False)