Skip to content

Instantly share code, notes, and snippets.

View tristanwietsma's full-sized avatar

Tristan Wietsma tristanwietsma

  • Chicago, Illinois
View GitHub Profile
@tristanwietsma
tristanwietsma / py2.py
Last active August 29, 2015 13:57
parallel python examples
import random
from joblib import Parallel, delayed
import time
def Example(c):
some_random_delay = int(2*random.random()*c)
time.sleep(some_random_delay) # let's pretend we're doing work
print some_random_delay
return some_random_delay
@tristanwietsma
tristanwietsma / hmm.py
Created March 12, 2014 02:10
Hidden Markov model example
from __future__ import division
import numpy
from sklearn import hmm
"""
X is a numpy array with shape (#samples, #sensors).
X = [[sensor1(0), sensor2(0)], [sensor1(1), sensor2(1)], ..., [sensor1(T), sensor2(T)]]
nc = number of latent states (3 would imply three strata)
@tristanwietsma
tristanwietsma / example.py
Created March 6, 2014 21:08
Futures in Python 3.3
# python 3.3: example of futures
import time
import concurrent.futures
def add(x, y):
time.sleep(2)
return x + y
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as queue:
@tristanwietsma
tristanwietsma / getset.go
Created February 9, 2014 20:55
Go web server with GET, POST
package main
import (
"fmt"
"github.com/gorilla/mux"
"github.com/tristanwietsma/metastore"
"log"
"net/http"
)
@tristanwietsma
tristanwietsma / Dockerfile
Created February 6, 2014 19:29
Oscats dockerfile
# OSCATS
FROM ubuntu
MAINTAINER Tristan Wietsma [email protected]
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y build-essential
RUN apt-get install -y autoconf automake
@tristanwietsma
tristanwietsma / qr-server.py
Created February 2, 2014 21:53
QR code server
import flask
import qrcode
import qrcode.image.svg
import qrcode.image.pil
app = flask.Flask(__name__)
class TextStream:
def __init__(self):
@tristanwietsma
tristanwietsma / glmnet.py
Last active October 9, 2023 18:55
Access glmnet through RPy2
import numpy as np
import rpy2.robjects as ro
import rpy2.robjects.numpy2ri as n2r
n2r.activate()
r = ro.r
r.library('glmnet')
# input files (for this example) need to have header and NO index column
X = np.loadtxt('./x.csv', dtype=float, delimiter=',', skiprows=1)
@tristanwietsma
tristanwietsma / rpy2-example.py
Created January 13, 2014 23:08
Bayesian regression in Python via RPy2
from __future__ import division
import numpy
import rpy2.robjects as ro
import rpy2.rlike.container as rlc
import rpy2.robjects.numpy2ri as numpy2ri
X = numpy.random.rand(100, 3)
_y = 0.25*X[:,0] + 0.7*X[:,1] - 0.45*X[:,2]
@tristanwietsma
tristanwietsma / pandas_merge.py
Created December 20, 2013 00:21
Outer join on two DataFrames in Pandas
import numpy
import pandas
df1 = pandas.DataFrame(numpy.random.randn(5,4), columns=list('ABCD'))
df2 = pandas.DataFrame(numpy.random.randn(5,4), columns=list('DEFG'))
print pandas.merge(df1, df2, on='D', how='outer')
# A B C D E F G
# 0 1.054809 -0.078626 -0.052766 0.463954 NaN NaN NaN
@tristanwietsma
tristanwietsma / Dockerfile
Created December 9, 2013 05:17
data-science dockerfile
# Python Data Science Environment
#
# VERSION 1.0
# use ubuntu base image from dotCloud
FROM ubuntu
MAINTAINER Tristan Wietsma [email protected]
# update and upgrade repos
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list