Skip to content

Instantly share code, notes, and snippets.

View alexland's full-sized avatar

doug ybarbo alexland

View GitHub Profile
@alexland
alexland / typed_mv.py
Created January 30, 2015 03:33
typed memoryviews in NumPy using the python buffer protocol (PEP 3118)
import numpy as NP
d = NP.random.randint(1, 10, 40).reshape(8, 5)
mv = memoryview(d)
def mem_addr(arr):
'''
returns: location of a NumPy array in memory, or
@alexland
alexland / fizzbuzz_numpy.py
Last active May 23, 2022 23:56
fizz buzz test in three short lines with NumPy
'''
fizzbuzz refers to a quick test to filter applicants for programmer jobs who don't actually know how to code.
(see eg, http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/)
"pass in a sequence of integers from 1 to 100;
for integers that are multiples of three: print “fizz”
for integers that are multiples of five print “buzz”
for integers that are multiples of five AND three print "fizzbuzz"
for remaining integers, print the integer value
'''
@alexland
alexland / offset_merge_arrays.py
Last active August 29, 2015 14:11
in-place merge two halves of an array w/ specified offset
>>> data = NP.arange(20)
>>> data
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
# use floor division to account for an odd number of items in the array,
# ie, 20//2 and 21//2 return the *same* result
>>> mp = data.shape[0] // 2
'''
@alexland
alexland / max_diff.py
Last active August 29, 2015 14:09
calculating max diff for a 1D sequence
def fnx(arr):
'''
pass in: NumPy 1d array
returns: 3-tuple (idx_min_val, idx_max_val, max_diff)
'''
arr = arr.reshape(-1, 1)
arr_diff = arr.T - arr
idx = NP.unravel_index(arr_diff.argmax(), arr_diff.shape)
return (idx[0], idx[1], arr_diff[idx])
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>dynamic data in d3: data join & update-append-exit pattern</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1"><!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
@alexland
alexland / tableOfKnowledge.md
Last active August 29, 2015 14:08
simple formulas, constants, etc., i don't always remember

rule of thumb for the number of dimensions above which a K-d tree is not ideal container:

rough dimension limit = log base 2 of the number of data points (n)

import numpy as NP

fnx = lambda n: NP.log2(n)

@alexland
alexland / exception_finder.py
Created October 7, 2014 05:44
your code throws an exception, but you can't write a handler because you don't know the module in which the exception class is defined
try:
...
except Exception as xe:
print("caught exception defined in module {}".format(xe.__class__.__module__)
@alexland
alexland / code-timer-class.py
Created September 28, 2014 09:05
lightweight class for timing python code
#!/usr/local/bin/python2.7
# encoding: utf-8
import time
from timeit import default_timer
class Timer(object):
def __init__(self, verbose=True):
self.verbose = verbose
@alexland
alexland / scipy_sparse_array_builder.py
Last active August 29, 2015 14:06
populating a scipy sparse DOK matrix
def str2num(t):
return ( int(t[0]), float(t[1]) )
def parse_line(line):
'''
returns:
(i) score (scalar);
(ii) adjacency dict (one row in sparse 2D array)
pass in:
@alexland
alexland / ts-rollup.R
Last active August 29, 2015 14:03
roll-up (aka 'group by') functions for time series data in R
require(zoo)
require(xts)
# TSR is an R time series comprised of a datetime index and a single column of data
# created like so:
# mock a datetime index, by creating a data vector comprised of the previous 500 days
nd = Sys.Date()
st = 500 - nd
idx = seq(from=st, to = nd, by='days')