Skip to content

Instantly share code, notes, and snippets.

@jobliz
jobliz / RedisPythonPubSub1.py
Created May 4, 2012 17:58
A short script exploring Redis pubsub functions in Python
import redis
import threading
class Listener(threading.Thread):
def __init__(self, r, channels):
threading.Thread.__init__(self)
self.redis = r
self.pubsub = self.redis.pubsub()
self.pubsub.subscribe(channels)
@jobliz
jobliz / TwitterSearchActor1.py
Created May 10, 2012 03:30
A crude implementation of the actor model to thread the twitter public search usage with python and redis publish/subscribe.
import time
import redis
import urllib
import threading
import simplejson
class Generator(threading.Thread):
def __init__(self, generator, output_channel, _redis=None):
threading.Thread.__init__(self)
self.generator = generator
@jobliz
jobliz / iris_petal.py
Created June 10, 2012 01:53
Iris dataset (petal size) scatterplot done in matplotlib
import matplotlib
import matplotlib.pyplot as plt
# Iris petal length/width scatterplot (greatest class correlation)
# Dataset: http://archive.ics.uci.edu/ml/datasets/Iris
# Output: https://imgur.com/9TWhn
def data():
lists = [line.strip().split(",") for line in open('flowerdata.txt', 'r').readlines()]
return [map(float, l[:4]) for l in lists], [l[-1] for l in lists]
import csv
import pylab as p
data = [r for r in csv.reader(open('piratebay.csv', 'rb'))]
def categoryratio(c):
return sum([ int(r[3]) for r in data if r[1][0] == str(c) ])
# Adapted from # http://scienceoss.com/bar-plot-with-custom-axis-labels/
fig = p.figure()
heights = ["high", "medium", "short"]
dist = [0.2, 0.6, 0.2]
generator = position_generator(dist)
n = 10000
count = 0
high = 0
med = 0
low = 0
def profit(supply, demand, sale_price=0.40, production_cost=0.25, welfare_price=0.10, lost=0.15):
operative_cost = production_cost * supply
if supply >= demand:
excedent = supply - demand
sale_income = demand * sale_price
welfare_gain = excedent * welfare_price
result = sale_income + welfare_gain - operative_cost
else:
not_baked_loss = lost * (demand - supply)
sale_income = supply * sale_price
demand_dist = (0.30, 0.45, 0.25) # [0]high, [1]medium, [2]low
positions = ( 36, 48, 60, 72, 84, 96)
high_demand = (0.05, 0.10, 0.25, 0.30, 0.20, 0.10)
med_demand = (0.10, 0.20, 0.30, 0.25, 0.10, 0.05)
low_demand = (0.15, 0.25, 0.35, 0.15, 0.05, 0.05)
demand_type = position_generator(demand_dist)
# be careful with order!
demand_amount = [
import sys
import itertools
import numpy as np
from collections import Counter
from matplotlib import pyplot as plt
def dice_prob(n):
sides = [(1,2,3,4,5,6) for x in xrange(n)]
combined = itertools.product(*sides)
sums = [sum(i) for i in combined]
import sys
import math
import numpy as np
from matplotlib import pyplot as plt
# https://en.wikipedia.org/wiki/Binomial_distribution
# k successes, n trials, p probability of success
def binomial_pmf(k, n, p):
nk = math.factorial(n) / (math.factorial(k) * math.factorial(n-k))
return nk * p**k * (1.0-p)**(n-k)
@jobliz
jobliz / deriv.py
Created March 15, 2013 02:55
The definition of derivative "just works" when coded in a programming language.
# from http://funcall.blogspot.sg/2009/03/not-lisp-again.html
def deriv(f, dx=.0001):
return lambda x: (f(x + dx) - f(x)) / dx
cube = lambda x: x**3
d = deriv(cube)
for n in xrange(5):
print n, d(n)