Skip to content

Instantly share code, notes, and snippets.

View erikbern's full-sized avatar

Erik Bernhardsson erikbern

View GitHub Profile
@erikbern
erikbern / gist:7cd899651aff9b656e9f
Last active August 29, 2015 14:24
Stupid graph visualizations
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
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
@erikbern
erikbern / README.md
Last active September 8, 2015 03:29
Antipodes

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 January 15, 2021 00:50
MCMC for simple marketing data
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 June 26, 2023 00:40
Installing TensorFlow on EC2
# 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 October 19, 2016 14:00
Install TensorFlow on EC2
# 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
# 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,
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
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.*?)\]\(.*?\)',
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