Created
May 2, 2012 14:47
-
-
Save albarrentine/2577114 to your computer and use it in GitHub Desktop.
numpy.fromiter: matrix from fixed-length arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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