I am trying to convert a generator to a numpy array. I apply a map function on a list of data and the result is a generator. I tried doing list(map()) and then creating the numpy vector but it takes a long time. I saw somewhere that I can directly use np.fromiter to create a numpy vector from my generator. However, I run into this error:
ValueError: setting an array element with a sequence.
I've found out that the error rises because my generator generates a list of lists. like: [[1,2,3], [4,5,6]] and I should use a proper structural dtype for the fromiter() function. I couldn't find a proper explanation of the way to do this. Can you help me?
Here's a full example:
import numpy as np
def foo(bar):
return [bar] * 3 # so for 4 it returns [4,4,4], ..
a = [1,2,3,4,5,6,7]
b = map(foo,a)
c = np.fromiter(b, int) # this doesn't work.