ndarray.utils

Utility functions for NDArray and BaseSparseNDArray.

Functions

zeros(shape[, ctx, dtype, stype])

Return a new array of given shape and type, filled with zeros.

empty(shape[, ctx, dtype, stype])

Returns a new array of given shape and type, without initializing entries.

array(source_array[, ctx, dtype])

Creates an array from any object exposing the array interface.

load(fname)

Loads an array from file.

load_frombuffer(buf)

Loads an array dictionary or list from a buffer

save(fname, data)

Saves a list of arrays or a dict of str->array to file.

zeros(shape, ctx=None, dtype=None, stype=None, **kwargs)[source]

Return a new array of given shape and type, filled with zeros.

Parameters
  • shape (int or tuple of int) – The shape of the empty array

  • ctx (Context, optional) – An optional device context (default is the current default context)

  • dtype (str or numpy.dtype, optional) – An optional value type (default is float32)

  • stype (string, optional) – The storage type of the empty array, such as ‘row_sparse’, ‘csr’, etc.

Returns

A created array

Return type

NDArray, CSRNDArray or RowSparseNDArray

Examples

>>> mx.nd.zeros((1,2), mx.cpu(), stype='csr')
<CSRNDArray 1x2 @cpu(0)>
>>> mx.nd.zeros((1,2), mx.cpu(), 'float16', stype='row_sparse').asnumpy()
array([[ 0.,  0.]], dtype=float16)
empty(shape, ctx=None, dtype=None, stype=None)[source]

Returns a new array of given shape and type, without initializing entries.

Parameters
  • shape (int or tuple of int) – The shape of the empty array.

  • ctx (Context, optional) – An optional device context (default is the current default context).

  • dtype (str or numpy.dtype, optional) – An optional value type (default is float32).

  • stype (str, optional) – An optional storage type (default is default).

Returns

A created array.

Return type

NDArray, CSRNDArray or RowSparseNDArray

Examples

>>> mx.nd.empty(1)
<NDArray 1 @cpu(0)>
>>> mx.nd.empty((1,2), mx.gpu(0))
<NDArray 1x2 @gpu(0)>
>>> mx.nd.empty((1,2), mx.gpu(0), 'float16')
<NDArray 1x2 @gpu(0)>
>>> mx.nd.empty((1,2), stype='csr')
<CSRNDArray 1x2 @cpu(0)>
array(source_array, ctx=None, dtype=None)[source]

Creates an array from any object exposing the array interface.

Parameters
  • source_array (array_like) – An object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence.

  • ctx (Context, optional) – Device context (default is the current default context).

  • dtype (str or numpy.dtype, optional) – The data type of the output array. The default dtype is source_array.dtype if source_array is an NDArray, float32 otherwise.

Returns

An array with the same contents as the source_array.

Return type

NDArray, RowSparseNDArray or CSRNDArray

Examples

>>> import numpy as np
>>> mx.nd.array([1, 2, 3])
<NDArray 3 @cpu(0)>
>>> mx.nd.array([[1, 2], [3, 4]])
<NDArray 2x2 @cpu(0)>
>>> mx.nd.array(np.zeros((3, 2)))
<NDArray 3x2 @cpu(0)>
>>> mx.nd.array(np.zeros((3, 2)), mx.gpu(0))
<NDArray 3x2 @gpu(0)>
>>> mx.nd.array(mx.nd.zeros((3, 2), stype='row_sparse'))
<RowSparseNDArray 3x2 @cpu(0)>
load(fname)[source]

Loads an array from file.

See more details in save.

Parameters

fname (str) – The filename.

Returns

Loaded data.

Return type

list of NDArray, RowSparseNDArray or CSRNDArray, or dict of str to NDArray, RowSparseNDArray or CSRNDArray

load_frombuffer(buf)[source]

Loads an array dictionary or list from a buffer

See more details in save.

Parameters

buf (str) – Buffer containing contents of a file as a string or bytes.

Returns

Loaded data.

Return type

list of NDArray, RowSparseNDArray or CSRNDArray, or dict of str to NDArray, RowSparseNDArray or CSRNDArray

save(fname, data)[source]

Saves a list of arrays or a dict of str->array to file.

Parameters

Examples

>>> x = mx.nd.zeros((2,3))
>>> y = mx.nd.ones((1,4))
>>> mx.nd.save('my_list', [x,y])
>>> mx.nd.save('my_dict', {'x':x, 'y':y})
>>> mx.nd.load('my_list')
[<NDArray 2x3 @cpu(0)>, <NDArray 1x4 @cpu(0)>]
>>> mx.nd.load('my_dict')
{'y': <NDArray 1x4 @cpu(0)>, 'x': <NDArray 2x3 @cpu(0)>}