Skip to content

Instantly share code, notes, and snippets.

View YehudaItkin's full-sized avatar

Yehuda (Igor) Itkin YehudaItkin

  • Sogo systems
  • Israel
View GitHub Profile
@YehudaItkin
YehudaItkin / serialize-numpy-array.py
Created April 4, 2016 06:21 — forked from alexland/serialize-numpy-array.py
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)