BetterDocs
Home
Docs

Creation | np.fromiter()

Method:

np.fromiter(iter, dtype, count=-1, *, like=None)

Creates an array from an iterable.

Returns:

np.ndarray

Parameters:

iter: filepath-

Input iterable.

import numpy as np

arr_from_iter = np.fromiter(iter=range(5), dtype=np.int8)
print(arr_from_iter) # Output: [0 1 2 3 4]

dtype: data-type-

It will convert the elements to the specified data-type.

import numpy as np

arr_from_iter = np.fromiter(range(5), dtype=np.float32)
print(arr_from_iter) # Output: [0. 1. 2. 3. 4.]

Values: +

count: int, Optional-

Number of items to read.

import numpy as np

arr_from_iter = np.fromiter(range(5), dtype=np.int8, count=2)
print(arr_from_iter) # Output: [0 1]

like: None, Optional-

The like parameter in np.fromiter() was introduced to make array creation more flexible, particularly for compatibility with libraries that extend NumPy, like Dask, CuPy, or other libraries that create arrays compatible with np.ndarray but optimized for different backends (e.g., parallel processing or GPU-based arrays).


Logo

BetterDocs

Support

EmailDiscordForms

Documentations

Python

Company

AboutDocs

Policies

Terms of ServicePrivacy Policy