Skip to content

Instantly share code, notes, and snippets.

View alaiacano's full-sized avatar

Adam Laiacano alaiacano

  • Spotify
  • New Rochelle, NY
View GitHub Profile
@alaiacano
alaiacano / gist:1273903
Created October 9, 2011 16:48
sample plot
labels <- c(
rep("a", 100*rexp(1)),
rep("b", 100*rexp(1)),
rep("c", 100*rexp(1)),
rep("d", 100*rexp(1)))
x <- data.frame(labels = factor(labels), some.value = runif(length(labels)))
qplot(labels, data=x)
@alaiacano
alaiacano / outlier_detector.py
Created October 12, 2011 22:52
outlier detector
from __future__ import division
import numpy as np
from pylab import *
from warnings import warn
class OutlierDetector(object):
def __init__(self, N=10):
"""
Generates some simple statistics on the previous N data points
@alaiacano
alaiacano / plotmatrix2.R
Created November 29, 2011 15:22
plotmatrix with aesthetics
plotmatrix2 <- function (data, mapping = aes())
{
grid <- expand.grid(x = 1:ncol(data), y = 1:ncol(data))
grid <- subset(grid, x != y)
all <- do.call("rbind", lapply(1:nrow(grid), function(i) {
xcol <- grid[i, "x"]
ycol <- grid[i, "y"]
data.frame(xvar = names(data)[ycol], yvar = names(data)[xcol],
x = data[, xcol], y = data[, ycol], data)
}))
@alaiacano
alaiacano / slice_sampler.py
Created December 29, 2011 19:53
A quick and dirty slice sampler.
from numpy.random import uniform
import numpy
import random
def slice_sampler(px, N = 1, x = None):
"""
Provides samples from a user-defined distribution.
slice_sampler(px, N = 1, x = None)
import numpy as np
import random
# dict to hold results
counts = {}
# a hack to generate random key, value pairs.
# 5k keys, 100k values
x=[i%5000 for i in xrange(100000)]
random.shuffle(x)
@alaiacano
alaiacano / plyr_demo.R
Created April 25, 2012 13:54
Passing variables to plyr
my.reduce <- function(df.in, f1, f2){
x <- ddply(df.in, .(f1, f2), summarize,
value=sum(value)
)
x
}
df <- data.frame(
state=rep(c("MA", "NY", "CA"), each=10),
year=rep((2001:2010), 3),
@alaiacano
alaiacano / sample_data.csv
Created May 9, 2012 18:28
sample data for my stackoverflow question.
y x
23 1
26 1
444 0.583333333333333
15 1
34 1
17 1
37 1
139 0.0359712230215827
300 0.183333333333333
"""
Input is one number per line
Output is json of the form:
[
{
'bin' : bin1,
'value' : value1
},
{
@alaiacano
alaiacano / optional_attribution.js
Created October 22, 2012 21:19
optionally display source attribution in a tumblr theme
{block:Posts}
<script language="JavaScript">
var source_url = '{block:ContentSource}{SourceURL}{/block:ContentSource}';
var reblogged_from = '{block:RebloggedFrom}{ReblogParentName}{/block:RebloggedFrom}' + '.tumblr.com';
if (source_url.indexOf(reblogged_from) == -1) {
document.write('Your attribution string!');
}
</script>
{/block:Posts}
@alaiacano
alaiacano / sample.R
Last active December 10, 2015 23:39
values <- c('a', 'b', 'c')
probs <- c(.2, .5, .3)
sample(values, 10, replace=TRUE, prob=probs)
# [1] "a" "b" "a" "a" "c" "a" "c" "b" "c" "b"