Skip to content

Instantly share code, notes, and snippets.

Avatar

Erik Bernhardsson erikbern

View GitHub Profile
@erikbern
erikbern / gist:7cd899651aff9b656e9f
Last active Aug 29, 2015
Stupid graph visualizations
View gist:7cd899651aff9b656e9f
http://blog.revolutionanalytics.com/2015/07/the-network-structure-of-cran.html
http://radar.oreilly.com/2015/06/graphs-in-the-world-modeling-systems-as-networks.html
http://quid.com/insights/the-future-of-artificial-intelligence/?utm_content=bufferb9759&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer
http://www.kpcb.com/blog/a-breakthrough-approach-to-making-data-useful
https://www.cbinsights.com/blog/e-commerce-investments-top-venture-capital-firms/
https://www.recordedfuture.com/two-shady-men-report/
http://www.galvanize.com/blog/2015/08/05/the-first-interactive-visualization-of-product-integrations/#.Vczc1RNVikq
View coin2dice.py
def coin():
while True:
yield random.randint(0, 1)
def dice_optimal():
x, y, z = 0, 1, 1
for c in coin():
x, y, z = x+c*z, y+c*z, 2*z
a, b = x*6//z, y*6//z
View README.md

The cities of Beijing and Buenos Aires are almost antipodes, i.e. they are situated almost opposite of each other on the globe.

An interesting attribute of this is that travelling between the two cities can be done through any point of earth and the trip will still be roughly the same distance. This D3 visualization demonstrates this principle: drag the map around to change perspective, click the map to set a "midpoint" to travel through.

Code for this visualization was stolen from all over the web without any deeper understanding of D3 – in particular this one and this one.

IIRC this is in a theme in Wong Kar-Wai's movie Happy Together.

@erikbern
erikbern / marketing_mc.py
Last active Jan 15, 2021
MCMC for simple marketing data
View marketing_mc.py
import pymc, pymc.graph
import matplotlib.pyplot as plt
import numpy as np
import sys
channels = [
('A', 2292.04, 9),
('B', 1276.85, 2),
('C', 139.59, 3),
('D', 954.98, 5)
@erikbern
erikbern / install-tensorflow.sh
Last active Jan 17, 2021
Installing TensorFlow on EC2
View install-tensorflow.sh
# Note – this is not a bash script (some of the steps require reboot)
# I named it .sh just so Github does correct syntax highlighting.
#
# This is also available as an AMI in us-east-1 (virginia): ami-cf5028a5
#
# The CUDA part is mostly based on this excellent blog post:
# http://tleyden.github.io/blog/2014/10/25/cuda-6-dot-5-on-aws-gpu-instance-running-ubuntu-14-dot-04/
# Install various packages
sudo apt-get update
@erikbern
erikbern / install-tensorflow.sh
Last active Oct 19, 2016
Install TensorFlow on EC2
View install-tensorflow.sh
# Start from ami-763a311e
# Install various packages
sudo apt-get update
sudo apt-get upgrade -y # choose “install package maintainers version”
sudo apt-get install -y build-essential python-pip python-dev git python-numpy swig python-dev default-jdk zip zlib1g-dev
# Install Bazel
git clone https://github.com/bazelbuild/bazel.git
cd bazel
View company_employee_model.py
# Simple models of two competing firms bidding on employees
# Each employee is described in terms of k factors
# Company X knows about the x first factors
# Company Y knows about the y first factors
import numpy
from matplotlib import pyplot
n = 10000
k, x, y = 10,
View ratings_mcmc.py
import pymc3, numpy, sys, seaborn, re
def get_dist(fn):
y = [0, 0, 0, 0, 0]
for line in open(fn):
try:
num = re.split('\D', line)[0]
y[int(num) - 1] += 1
except:
print fn, 'can not parse:', line
View wordpress_to_jekyll_postprocess.py
import os, sys, re
patterns = [
# <img src='http://s.wordpress.com/latex.php?latex=%5Cmathcal%7BO%7D%28n%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='\mathcal{O}(n)' title='\mathcal{O}(n)' class='latex' />
(r'<img.*?title=\'(.*?)\' class=\'latex\' />',
'$$ \\1 $$ ',
0),
# [<img class=" size-full wp-image-1722 aligncenter" src="http://erikbern.com/wp-content/uploads/2016/01/avg.png" alt="avg" width="512" height="512" />](http://erikbern.com/wp-content/uploads/2016/01/avg.png)
(r'\[(<img.*?)\]\(.*?\)',
View scrape_mta.py
from google.transit import gtfs_realtime_pb2
import urllib
for feed_id in [1, 2, 11]:
feed = gtfs_realtime_pb2.FeedMessage()
response = urllib.urlopen('http://datamine.mta.info/mta_esi.php?key=%s&feed_id=%d' % (os.environ['MTA_KEY'], feed_id))
feed.ParseFromString(response.read())
print feed