Skip to content

Instantly share code, notes, and snippets.

Erik Bernhardsson erikbern

Block or report user

Report or block erikbern

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View kx_engineer_productivity.py
import numpy
import scipy.optimize
from matplotlib import pyplot
cs = numpy.linspace(0.01, 0.99, 100)
ks = []
for c in cs:
def f(log_k):
k = numpy.exp(log_k)
View utilization_and_cycle_time.py
import numpy
import random
from matplotlib import pyplot
percentiles = [50, 75, 90, 95, 99]
latencies = [[] for p in percentiles]
loads = []
n = 100000
for k in numpy.linspace(0.01, 1.0, 100):
@erikbern
erikbern / american_community_survey_example.py
Last active Dec 30, 2017
Download and parse American Community Survey data using Python
View american_community_survey_example.py
# Uses American Community Survey data to estimate property taxes
# https://www.census.gov/programs-surveys/acs/
# The data is a f-ing PITA to parse, but here's an attempt
import bs4
import csv
import io
import os
import requests
import sys
View plot_word_length_in_letters.py
from num2words import num2words
from matplotlib import pyplot
lang = 'de'
words = [num2words(i, lang=lang) for i in range(1000000)]
fig = pyplot.figure()
ax = fig.add_subplot(111)
ax.semilogx([len(word) for word in words], color='green')
View beta.py
import matplotlib.pyplot, numpy, scipy.stats, seaborn
for i in range(50):
p, = scipy.stats.uniform.rvs(size=1)
a, b = scipy.stats.geom.rvs(p, size=2)
if a == b == 1:
continue
print(a, b)
x = numpy.linspace(0, 1, 1000)
y = scipy.stats.beta.pdf(x, a, b)
View ll_to_3d.py
def ll_to_3d(lat, lon):
lat *= math.pi / 180
lon *= math.pi / 180
x = math.cos(lat) * math.cos(lon)
z = math.cos(lat) * math.sin(lon)
y = math.sin(lat)
return numpy.array([x, y, z])
@erikbern
erikbern / kaplan_meier.py
Created May 22, 2017
Kaplan-Meier snippet
View kaplan_meier.py
n, k = len(te), 0
ts, ys = [], []
p = 1.0
for t, e in te:
if e: # whether the event was "observed" (converted) or not observed (may convert in the future)
p *= (n-1) / n
n -= 1
ts.append(t)
ys.append(100. * (1-p))
pyplot.plot(ts, ys, 'b')
@erikbern
erikbern / get_n_results.py
Created Mar 13, 2017
Get number of search results from Google
View get_n_results.py
def get_n_results_dumb(q):
r = requests.get('http://www.google.com/search',
params={'q': q,
"tbs": "li:1"})
r.raise_for_status()
soup = bs4.BeautifulSoup(r.text)
s = soup.find('div', {'id': 'resultStats'}).text
if not s:
return 0
m = re.search(r'([0-9,]+)', s)
@erikbern
erikbern / group-rotation.js
Last active Oct 20, 2016
Update a Google group to contain only one single member
View group-rotation.js
/*
This code is meant to be used with a rotating responsibility for incoming bugs and stuff.
To make this work, you need to:
1. Set up the key in the Google IAM manager
2. Add domain delegation for the user
3. Download the key and add the following fields: "admin_email" and "domain"
*/
var key = require('./google-people-api-creds.json');
var scope = ['https://www.googleapis.com/auth/admin.directory.group',
View simulate_time_waitingt_train_problems.py
import numpy
from matplotlib import pyplot
import seaborn
p = numpy.logspace(-6, 0, 50)
pyplot.figure(figsize=(10, 5))
for k in [1, 3, 10, 30, 100, 300, 1000]:
Z = (p * k**2 + (1 - p)) / (p * k + (1 - p))
You can’t perform that action at this time.