numpy.fromiter() in Python
The fromiter() function of Python numpy class creates a ndarray by using an iterable object. It returns a one-dimensional ndarray object.
Syntax
1 2 3 |
numpy.fromiter(iterable, dtype, count=-1) |
Parameter
The numpy.frombuffer() method consists of three parameters, which are as follows:
- Iterable: This parameter represents an iterable object.
- dtype: It represents the data type of the resultant array items.
- count: This functiondepicts the number of items to read from the buffer in the specified array.
Return
This function returns an array created by using the iterable object.
Example 1
1 2 3 4 5 6 7 8 9 10 |
# Python Programming giving an example for # numpy.fromiter() function importnumpy as numpy my_list = [0,2,4,6] it = iter(my_list) n = numpy.fromiter(it, dtype = float) print(n) print(type(n)) |
Output
1 2 3 4 |
[ 0. 2. 4. 6.] <class 'numpy.ndarray'> |