Skip to content

Instantly share code, notes, and snippets.

View kcarnold's full-sized avatar

Kenneth C. Arnold kcarnold

  • Grand Rapids, MI
View GitHub Profile
@kcarnold
kcarnold / rating.html
Created April 12, 2013 18:42
Star ratings, thanks to http://dabblet.com/gist/1718992 (https://twitter.com/dmfilipenko/status/164800384953614336) for the trick: right-float radio buttons so they display in the right order. I think the dabblet's labeled order was wrong, but I used FontAwesome instead of sprites (you'll need to include its CSS), and I didn't finish porting ove…
<span class="rating">
<input id="star-5" name="radioradio" type="radio" value="5"><label for="star-5"></label>
<input id="star-4" name="radioradio" type="radio" value="4"><label for="star-4"></label>
<input id="star-3" name="radioradio" type="radio" value="3"><label for="star-3"></label>
<input id="star-2" name="radioradio" type="radio" value="2"><label for="star-2"></label>
<input id="star-1" name="radioradio" type="radio" value="1"><label for="star-1"></label>
</span>
@kcarnold
kcarnold / relative_metric.py
Created April 23, 2013 00:47
Liu et al. "Metric Learning from Relative Comparisons by Minimizing Squared Residual". ICDM 2012.
import numpy as np
import scipy.linalg as LA
def comparison_loss(metric, comparisons):
loss = 0.
for weight, xa, xb, xc, xd in comparisons:
vab = xa - xb
vcd = xc - xd
dab = np.dot(vab.T, np.dot(metric, vab))
dcd = np.dot(vcd.T, np.dot(metric, vcd))
@kcarnold
kcarnold / sparse_metric.py
Created April 23, 2013 00:52
Qi, G.-J., Tang, J., Zha, Z.-J., Chua, T.-S., & Zhang, H.-J. (2009). An efficient sparse metric learning in high-dimensional space via l 1 -penalized log-determinant regularization. Proceedings of the 26th Annual International Conference on Machine Learning - ICML ’09 (pp. 1–8). New York, New York, USA: ACM Press. doi:10.1145/1553374.1553482
import numpy as np
from sklearn.covariance import graph_lasso
from sklearn.utils.extmath import pinvh
def compute_K(n, S, D):
K = np.zeros((n,n))
for a, b in S:
K[a,b] = 1
#K[b,a] = 1
for a, b in D:
@kcarnold
kcarnold / responses.py
Created May 14, 2013 21:20
Get result data from Turk, in a way that readily makes a Pandas DataFrame
from collections import OrderedDict
from boto.mturk.connection import MTurkConnection
from dateutil.parser import parse as dateparse
mtc = MTurkConnection(host='mechanicalturk.amazonaws.com')
def responses(hit_group_id):
responses = []
for hit in mtc.get_all_hits():
#!/usr/bin/env python
import numpy as np
import sys
from six import iteritems
from six.moves import zip as izip
from six.moves import xrange
from itertools import chain, repeat, islice
@kcarnold
kcarnold / spidev.pyx
Created July 20, 2013 15:25
SPI driver in Cython. Based on the spidev_test.c sample code included in the Linux kernel.
#
# SPI driver (using spidev device)
#
# Copyright (c) 2013 Kenneth Arnold <[email protected]>
# Copyright (c) 2007 MontaVista Software, Inc.
# Copyright (c) 2007 Anton Vorontsov <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License.
import pandas as pd
import sys
filename, outfile = sys.argv[1:3]
responses = pd.read_csv(filename)
def format_row(row):
return '\n'.join(["<p><b>{}</b><br>{}</p>".format(k, v.replace('\n', '<br>')) for k, v in row.iteritems()])
@kcarnold
kcarnold / reconstruct_lkj_cholesky.py
Created April 22, 2014 00:24
Reconstruct the actual Cholesky factor of a covariance matrix parameterized using the LKJ "onion method"
def reconstruct_lkj_cholesky(ell, R2):
num_items = len(R2) + 1
L = np.zeros((num_items, num_items))
L[0, 0] = 1.
L[1, 0] = 2. * R2[0] - 1.0
L[1, 1] = np.sqrt(1.0 - L[1, 0])
start = 0
for i in range(2, num_items):
ell_row = ell[start:start+i]
@kcarnold
kcarnold / infobox.css
Created September 29, 2014 14:37
Academic Requester Infobox
.mturk-ari {
border: 1px solid black;
padding: 4px;
}
.mturk-ari th {
padding-right: 5px;
text-align: right;
}
@kcarnold
kcarnold / glove_to_npy.py
Last active August 29, 2015 14:10
Convert GloVe (http://www-nlp.stanford.edu/projects/glove/) pre-trained vectors to quick-lookup matrices
import numpy as np
import sys
def read_stored(num_dims, names_filename, data_filename):
import pandas as pd
names = pd.Index(line.strip() for line in open(names_filename))
num_terms = len(names)
data = np.memmap(data_filename, dtype=np.float32, mode='r', shape=(num_terms, num_dims))
return names, data