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 / README.md
Last active August 29, 2015 14:16
Adjustable metric

Compare colors by hue, saturation, or lightness. The sliders adjust the weights. It seems like the main effect is whether the weight is 0 or not.

The embedding is simply links to the 5 nearest neighbors, according to that metric.

@kcarnold
kcarnold / README.md
Created March 12, 2015 19:19
Distance Scaling

This is a simple implementation of MDS. Dragging an item constrains it; double-clicking removes the constraint. The cost is shown in the top left.

The data is eurodist from R.

@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
@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 / 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]
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 / 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.
#!/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 / 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():
@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: