dpnp.fromiter

dpnp.fromiter(iter, dtype, count=-1, *, like=None, device=None, usm_type='device', sycl_queue=None)[source]

Create a new 1-dimensional array from an iterable object.

For full documentation refer to numpy.fromiter.

Parameters:
iteriterable object

An iterable object providing data for the array.

dtype{None, str, dtype object}

The data-type of the returned array.

countint, optional

The number of items to read from iterable. The default is -1, which means all data is read.

Default: -1.

device{None, string, SyclDevice, SyclQueue, Device}, optional

An array API concept of device where the output array is created. device can be None, a oneAPI filter selector string, an instance of dpctl.SyclDevice corresponding to a non-partitioned SYCL device, an instance of dpctl.SyclQueue, or a dpnp.tensor.Device object returned by dpnp.ndarray.device.

Default: None.

usm_type{None, "device", "shared", "host"}, optional

The type of SYCL USM allocation for the output array.

Default: "device".

sycl_queue{None, SyclQueue}, optional

A SYCL queue to use for output array allocation and copying. The sycl_queue can be passed as None (the default), which means to get the SYCL queue from device keyword if present or to use a default queue.

Default: None.

Returns:
outdpnp.ndarray

The output array.

Limitations

Parameter like is supported only with default value None. Otherwise, the function raises NotImplementedError exception.

See also

dpnp.frombuffer

Construct array from the buffer data.

dpnp.fromfile

Construct array from data in a text or binary file.

dpnp.fromstring

Construct array from the text data in a string.

Notes

This uses numpy.fromiter and coerces the result to a DPNP array.

Examples

>>> import dpnp as np
>>> iterable = (a * a for a in range(5))
>>> np.fromiter(iterable, float)
array([  0.,   1.,   4.,   9.,  16.])

Creating an array on a different device or with a specified usm_type

>>> x = np.fromiter(iterable, np.int32) # default case
>>> x.device, x.usm_type
(Device(level_zero:gpu:0), 'device')
>>> y = np.fromiter(iterable, np.int32, device='cpu')
>>> y.device, y.usm_type
(Device(opencl:cpu:0), 'device')
>>> z = np.fromiter(iterable, np.int32, usm_type="host")
>>> z.device, z.usm_type
(Device(level_zero:gpu:0), 'host')