Skip to content

Instantly share code, notes, and snippets.

View alexland's full-sized avatar

doug ybarbo alexland

View GitHub Profile
@alexland
alexland / serialize-numpy-array.py
Last active November 28, 2023 07:12
serialize, persist, retrieve, and de-serialize a NumPy array as a binary string (any dimension, any dtype); exemplary use case: a web app calculates some result--eg, from a Machine Learning algorithm, using NumPy and the result is a NumPy array; it is efficient to just return that result to rather than persist the array then retrieve it via query
import time
import numpy as NP
from redis import StrictRedis as redis
# a 2D array to serialize
A = 10 * NP.random.randn(10000).reshape(1000, 10)
# flatten the 2D NumPy array and save it as a binary string
array_dtype = str(A.dtype)
>>> from scipy import misc as MSC
>>> import os
>>> df = os.path.expanduser("~/any-image.png")
>>> # now read the image in as a multi-dimensional array (matrix)
>>> # 'imread' is a wrapper over a PIL method
>>> mg1 = MSC.imread(df)
>>> mg1.shape
@alexland
alexland / NumPy-persist-in-HDF5.ipynb
Last active August 29, 2015 14:02
shows how to persist a NumPy array using HDF5 and how to use that (HDF5) on-disk array for out-of-core matrix computation; this is the JSON (w/ the code embedded) for an ipython notebook; to view it, go to http://nbviewer.ipython.orgin; in the textbox, type in the id for this gist
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@alexland
alexland / convert-datetime-to-unix.py
Last active March 8, 2018 01:55
various date/time conversions in python
from datetime import datetime as DT
#------------------ epoch time -----------------#
>>> import time
>>> time.time()
1411060580.205373
@alexland
alexland / compress.py
Last active August 29, 2015 14:03
compressed file IO in python
df = '~/path/to/some/text/file.csv'
df1 = 'path/to/some/text/file.gz'
import zlib
import gzip
with open(df, 'r', encoding='utf-8') as fh:
tx = fh.read()
# compress a text file
@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')
@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 / 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 / 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 / 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)