mojolearn.Array#

class mojolearn.Array(shape, dtype='<f4')[source]#

Bases: object

The array type mojolearn returns: typed, shaped, contiguous memory.

mojolearn does not require NumPy, so estimators return Array objects. An Array is a C- or F-contiguous block that either owns its memory or, through from_buffer(), borrows it from another buffer it keeps alive. Its memory address never changes during its lifetime.

With NumPy installed, numpy.asarray(a) is a zero-copy view (through __array_interface__). On Python 3.12 and later memoryview(a) is zero-copy too.

Array is a container, not a NumPy replacement: there are no arithmetic operators, no broadcasting and no fancy indexing. Indexing with ints and slices is supported (a full integer index returns a Python scalar, anything else a C-order copy), as are tolist(), reshape(), astype(), == and the reductions min(), max(), sum() and argmax(), so you can inspect results without NumPy. '<f2' (float16) values are stored, not computed with.

Parameters:
  • shape (int or tuple of int) – Shape of the new, zero-filled array.

  • dtype (str, default “<f4”) – One of "<f4", "<f8", "<f2", "<i4", "<i8", "<i1", "<u4", "<u2", "<u1".

Variables:
  • shape (tuple of int)

  • dtype (str)

  • itemsize (ndim, size, nbytes,)

  • strides (tuple of int)

  • order ({"C", "F"})

  • flags (dict) – C_CONTIGUOUS, F_CONTIGUOUS and WRITEABLE.

Examples

>>> from mojolearn import Array
>>> a = Array.from_list([[1.0, 2.0], [3.0, 4.0]], "<f4")
>>> a.shape, a.sum()
((2, 2), 10.0)
>>> import numpy as np
>>> np.asarray(a).dtype
dtype('float32')
__init__(shape, dtype='<f4')[source]#

Methods

argmax()

Flat (C-order) index of the first maximum.

astype(dtype)

A converted copy (always a copy, even for the same dtype).

copy()

A new Array with the same dtype, shape, order and values.

from_buffer(obj)

A zero-copy view of any contiguous buffer-protocol object.

from_list(nested[, dtype])

A new C-order Array from a (nested) list of Python numbers.

max()

Largest element, in storage order; ValueError for an empty Array.

min()

Smallest element, in storage order; ValueError for an empty Array.

ravel()

A 1-D view, same as reshape(-1).

reshape(shape)

A C-order view with a new shape (one dimension may be -1).

sum()

Sum of all elements: an exact int for integer dtypes, a float summed in storage order for float dtypes.

tobytes()

The raw bytes, in storage order (row-major for a C-order Array, column-major for an F-order one).

tolist()

The values as nested Python lists (a scalar for a 0-d Array).

Attributes

shape

dtype

ndim

size

nbytes

itemsize

strides

order

flags

{"C_CONTIGUOUS", "F_CONTIGUOUS", "WRITEABLE"} as booleans.

classmethod from_buffer(obj)[source]#

A zero-copy view of any contiguous buffer-protocol object.

The result keeps obj alive but does not own it; writes through the result reach obj.

Parameters:

obj (buffer-protocol object) – For example a NumPy array, array.array, bytearray or memoryview.

Returns:

Array

Raises:

ValueError, TypeError – For a non-contiguous buffer or an unsupported format (no copy is made).

classmethod from_list(nested, dtype='<f8')[source]#

A new C-order Array from a (nested) list of Python numbers.

The shape is inferred and must be rectangular. Floats converted to "<f4" round to nearest even. Floats are not converted to integer dtypes (an error is raised instead of truncating), and integers too large for the dtype raise an error rather than wrap.

Parameters:
  • nested (list) – Nested lists of numbers.

  • dtype (str, default “<f8”) – Element type.

Returns:

Array

property flags#

{"C_CONTIGUOUS", "F_CONTIGUOUS", "WRITEABLE"} as booleans.

tobytes()[source]#

The raw bytes, in storage order (row-major for a C-order Array, column-major for an F-order one).

tolist()[source]#

The values as nested Python lists (a scalar for a 0-d Array).

copy()[source]#

A new Array with the same dtype, shape, order and values.

astype(dtype)[source]#

A converted copy (always a copy, even for the same dtype).

Float to "<f4" rounds to nearest even; float to "<f8" and integers up to 2**53 to float are exact. Float to integer truncates toward zero, like NumPy. The memory order is kept.

Parameters:

dtype (str) – Target element type.

Returns:

Array

reshape(shape)[source]#

A C-order view with a new shape (one dimension may be -1).

An F-order Array is copied to C order first, as NumPy does.

Parameters:

shape (int or tuple of int)

Returns:

Array

ravel()[source]#

A 1-D view, same as reshape(-1).

min()[source]#

Smallest element, in storage order; ValueError for an empty Array.

max()[source]#

Largest element, in storage order; ValueError for an empty Array.

sum()[source]#

Sum of all elements: an exact int for integer dtypes, a float summed in storage order for float dtypes.

argmax()[source]#

Flat (C-order) index of the first maximum.