Skip to content

Instantly share code, notes, and snippets.

View vegarsti's full-sized avatar

Vegard Stikbakke vegarsti

View GitHub Profile
@vegarsti
vegarsti / tree.md
Created April 25, 2017 12:12 — forked from hrldcpr/tree.md
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

Requires node.js and npm.

First, do npm install -g ijavascript. Then run ijs once.

  • Virtualenv & stuff (incl. autoenv): Read!
  • Homebrew
  • LaTeX w/ Sublime
# defaultdict use case
# Finding a Pareto front, where (x1, x2) is a list of tuples satisfying
# e.g. (x1, x2) : |df[(df[var1 > x1]) & df[var2 > x2)]| < 0.1|df|,
# where |df| is the size of df, i.e., df.shape[0] (in Pandas)
pareto = defaultdict(lambda: np.Inf)
for x1, x2 in xs:
pareto[x1] = min(pareto[x1], x2)
@vegarsti
vegarsti / jupyter-notify.md
Created September 13, 2017 08:22
Jupyter notebook: Get notified when cell is done
  • To count occurrences when aggregating (e.g. in pivot_table), use len as aggfunc!

  • To drop a row: Cheatsheet

    • df[df.name != 'Tina']
    • df.drop(df.index[2])
  • c['a'].shift(periods=1)

pandoc file.md -o file.pdf

  • current_date - interval '1 days'*extract(isodow from now()) sunday before this week

Given array a, count occurrences and stack.

e.g. a = np.array([0, 0, 1, 1, 2, 1, 0, 2, 3, 0, 1])

then counts = np.dstack(np.unique(a, return_counts=True)) gives

array([[[0, 4],
        [1, 4],
 [2, 2],
  • tee from itertools is cool! link
from itertools import tee
i, j = tee(data)
next(j)
for first, second in zip(i, j):