mojolearn.Array#
- class mojolearn.Array(shape, dtype='<f4')[source]#
Bases:
objectThe array type mojolearn returns: typed, shaped, contiguous memory.
mojolearn does not require NumPy, so estimators return
Arrayobjects. AnArrayis 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 latermemoryview(a)is zero-copy too.Arrayis 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 reductionsmin(),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:
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')
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
intfor 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
shapedtypendimsizenbytesitemsizestridesorder{"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
objalive but does not own it; writes through the result reachobj.- Parameters:
obj (buffer-protocol object) – For example a NumPy array,
array.array,bytearrayormemoryview.- 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).
- 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