How Do I Build A Numpy Array From A Generator?
Last Updated :
28 Apr, 2025
NumPy is a powerful library in Python for numerical operations, and it provides an efficient array object, numpy.ndarray
, for working with large datasets. Often, data is generated using generators, and it becomes necessary to convert this data into NumPy arrays for further analysis. In this article, we'll explore some different methods to build a NumPy array from a generator.
How Do I Build A Numpy Array From A Generator?
Below, are the ways to Build A Numpy Array From A Generator in Python.
Numpy Array From A Generator Using numpy.fromiter()
In this example, the below code defines a generator function generator()
that yields numbers from 1 to 10. It then uses NumPy's fromiter
function to create an array from the generator, storing it in the variable arr
. Finally, it prints the array and its data type.
Python3
#importing numpy library
import numpy as np
#generator function
def generator():
n = 10
for i in range(1,n+1):
yield i
print(type(generator()))
if __name__ == "__main__":
#calling the function and storing in our arrya
arr = np.fromiter(generator(), dtype=int,count=-1)
#Displaying the array
print("Array : {}".format(arr))
print(type(arr))
Output :
<class 'generator'>
Array : [ 1 2 3 4 5 6 7 8 9 10]
<class 'numpy.ndarray'>
Numpy Array From A Generator Using numpy.array()
In this example , below code defines a generator function generator()
that yields numbers from 1 to 10. It then converts the generator output to a list and creates a NumPy array from that list using np.array()
, storing it in the variable arr
. Finally, it prints the array and its data type.
Python3
# importing numpy library
import numpy as np
# generator function
def generator():
n = 10
for i in range(1, n+1):
yield i
print(type(generator()))
if __name__ == "__main__":
# calling the function and storing in our arrya
arr = np.array(list(generator()), dtype=int)
# Displaying the array
print("Array : {}".format(arr))
print(type(arr))
Output :
<class 'generator'>
Array : [ 1 2 3 4 5 6 7 8 9 10]
<class 'numpy.ndarray'>
Numpy Array From A Generator Using numpy.concatenate()
In this example, below code defines a generator function generator()
that yields numbers from 1 to 10. It then converts the generator output to a list (gen_list
) and uses NumPy's np.concatenate()
to concatenate the list and create an array (arr
). Finally, it prints the array and its data type.
Python3
# importing numpy library
import numpy as np
# generator function
def generator():
n = 10
for i in range(1, n + 1):
yield i
print(type(generator()))
if __name__ == "__main__":
# calling the function and converting the
#generator to a list
gen_list = list(generator())
arr = np.concatenate([gen_list], dtype=int)
# Displaying the array
print("Array: {}".format(arr))
print("Type of Array:", type(arr))
Output :
<class 'generator'>
Array: [ 1 2 3 4 5 6 7 8 9 10]
Type of Array: <class 'numpy.ndarray'>
Conclusion
In conclusion, building a NumPy array from a generator provides a memory-efficient way to handle large datasets by generating data on-the-fly. Utilizing the numpy.fromiter
function or directly converting a generator expression to an array with numpy.array
allows for seamless integration of generator-based data into NumPy workflows. This approach not only conserves resources but also enhances the overall efficiency and performance of numerical operations in Python.
Similar Reads
How to convert NumPy array to list ? This article will guide you through the process of convert a NumPy array to a list in Python, employing various methods and providing detailed examples for better understanding. Convert NumPy Array to List There are various ways to convert NumPy Array to List here we are discussing some generally us
4 min read
How to Convert NumPy Matrix to Array In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array. Convert Python NumPy Matrix to an ArrayBelow are the ways by which we can
3 min read
Create A Pandas Dataframe From Generator A Pandas DataFrame is a 2D data structure like a table with rows and columns. The values in the data frame are mutable and can be modified. Data frames are mostly used in data analysis and data manipulation. DataFrames store data in table form like databases and Excel sheets. We can easily perform a
3 min read
How to Map a Function Over NumPy Array? Mapping a function over a NumPy array means applying a specific operation to each element individually. This lets you transform all elements of the array efficiently without writing explicit loops. For example, if you want to add 2 to every element in an array [1, 2, 3, 4, 5], the result will be [3,
2 min read
How to create an empty and a full NumPy array? Creating arrays is a basic operation in NumPy. Empty array: This array isnât initialized with any specific values. Itâs like a blank page, ready to be filled with data later. However, it will contain random leftover values in memory until you update it.Full array: This is an array where all the elem
2 min read
How to convert an array of indices to one-hot encoded NumPy array A very popular technique used in machine learning to transform categorical data into binary values of 0 and 1 is called the one-hot encoding technique. There are various circumstances when you need to use a one-hot encoded NumPy array rather than an array of indices, thus we can convert it using the
3 min read