Skip to content

Instantly share code, notes, and snippets.

@albarrentine
Created May 2, 2012 14:47
Show Gist options
  • Save albarrentine/2577114 to your computer and use it in GitHub Desktop.
Save albarrentine/2577114 to your computer and use it in GitHub Desktop.
numpy.fromiter: matrix from fixed-length arrays
# Even more fun
def array_gen(some_strings):
for s in some_strings:
yield numpy.fromstring(s, dtype=numpy.int)
# Let's say I know the length of all the arrays coming out of my generator
# and I want to build a matrix
K = 10
M = 5
stringified = numpy.arange(K).tostring()
str_gen = (stringified for i in xrange(M)) # This is just a generator object, hasn't done anything yet
m = numpy.fromiter(array_gen(str_gen), dtype=[('cool', numpy.int, K)])
"""
The array would look like this:
>>> m
array([([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],),
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],),
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],),
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],),
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],)],
dtype=[('cool', ''<i8', 10)])
>>> m.flags.owndata
True
....But you can also access the single matrix through the 'cool' index without making copies (uses views)
>>> m['cool']
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
>>> m.flags.owndata
False
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment