mt.np
Additional utitlities dealing with numpy.
Instead of:
import numpy as np
You do:
from mt import np
It will import the numpy package plus the additional stuff implemented here.
Please see the numpy package for more details.
Submodules
Functions
ndarray_repr()
: Gets a one-line representation string for a numpy array.sigmoid()
: Returns sigmoid(x) = 1/(1+exp(-x)).asigmoid()
: Inverse of sigmoid. Seesigmoid()
.frombytes()
: Converts a bytes instance into a 1D uint8 array.divide_no_nan()
: Computes a safe divide which returns 0 if y (denominator) is zero.to_b85()
: Converts a primitve numpy array into a b85-encoded string.from_b85()
: Converts a b85-encoded string back to a numpy array.sparse_vector()
: Makes a sparse vector as a single-column sparse matrix in COO format of scipy.psd_approx()
: Approximates a real symmetric matrix with a positive semi-definite.sqrtm()
: Computes the matrix square root of a real positive semi-definite matrix.powm()
: Computes the matrix power of a real positive semi-definite matrix.quantise_images()
: Quantises a tensor of images.dequantise_images()
: Dequantises a tensor of images.
- mt.np.ndarray_repr(a: ndarray) str
Gets a one-line representation string for a numpy array.
- Parameters:
a (numpy.ndarray) – input numpy array
- Returns:
a short representation string for the array
- Return type:
str
- mt.np.sigmoid(x) ndarray
Returns sigmoid(x) = 1/(1+exp(-x)).
- mt.np.frombytes(data: bytes) ndarray
Converts a bytes instance into a 1D uint8 array.
- mt.np.divide_no_nan(x: ndarray | float, y: ndarray | float) ndarray | float
Computes a safe divide which returns 0 if y (denominator) is zero.
The returning object is scalar if and only if both x and y are scalar.
- mt.np.to_b85(x: ndarray) str
Converts a primitve numpy array into a b85-encoded string.
- mt.np.from_b85(b85: str) ndarray
Converts a b85-encoded string back to a numpy array.
- mt.np.sparse_vector(values: ndarray, indices: ndarray, dim: int | None = None) scipy.sparse.coo_array
Makes a sparse vector as a single-column sparse matrix in COO format of scipy.
Invoking the function may require importing scipy.
- mt.np.values
A 1D ndarray with shape (N,) containing all nonzero values. The dtype of ‘values’ specifies the dtype of the sparse ndarray.
- Type:
array_like
- mt.np.indices
A 1D ndarray with shape (N,), containing the indices of the nonzero values. Let D be th maximum value of any element of indices, plus 1.
- Type:
array_like
- mt.np.dim
the dimensionality of the sparse vector. Must be greater than or equal to D if provided. Otherwise, it is set to D.
- Type:
int
- mt.np.psd_approx(A)
Approximates a real symmetric matrix with a positive semi-definite.
The approximated matrix is the one closest to the input matrix in Frobenius norm.
- Parameters:
A (numpy.ndarray) – 2D array representing the square input matrix
- Returns:
output square matrix of the same shape that is positive semi-definite
- Return type:
numpy.ndarray
- mt.np.sqrtm(A)
Computes the matrix square root of a real positive semi-definite matrix.
- Parameters:
A (numpy.ndarray) – 2D array representing the input matrix
- Returns:
output square matrix such that its square equals the input matrix
- Return type:
numpy.ndarray
- mt.np.powm(A, exp)
Computes the matrix power of a real positive semi-definite matrix.
- Parameters:
A (numpy.ndarray) – 2D array representing the input matrix
exp (float) – power exponent
- Returns:
output square matrix such that it, raised to the power of exp, equals the input matrix
- Return type:
numpy.ndarray
- mt.np.quantise_images(a: ndarray, clip: bool = True) ndarray
Quantises a tensor of images.
It takes a tensor of images of dtype float32 where every pixel value is in range (0., 256.) and converts them to dtype uint8.
- Parameters:
a (numpy.ndarray) – input tensor of images of dtype float32 and each value is in integer range (0., 256.)
clip (bool) – whether or not to clip overflow or underflow values. That is, values less than 0 are set to 0 and values at 256 or above are set to 255.
- Returns:
output tensor of images of dtype uint8 and each value is in range [0, 256)
- Return type:
numpy.ndarray
- mt.np.dequantise_images(a: ndarray, rng: RandomState | None = None) ndarray
Dequantises a tensor of images.
It takes a tensor of images of dtype uint8, converts the tensor into dtype float32, and adds a uniform noise in range [0,1) to every pixel value.
- Parameters:
a (numpy.ndarray) – input tensor of images of dtype uint8 and each value is in integer range [0, 256)
rng (numpy.random.RandomState, optional) – the random number generator to make uniform noise values
- Returns:
output tensor of images of dtype float32 and each value is in float32 range (0., 256.)
- Return type:
numpy.ndarray
Classes
SparseNdarray
: A sparse ndarray, following TensorFlow’s convention.
- class mt.np.SparseNdarray(values: ndarray, indices: ndarray, dense_shape: tuple)
A sparse ndarray, following TensorFlow’s convention.
- values
A 1D ndarray with shape (N,) containing all nonzero values. The dtype of ‘values’ specifies the dtype of the sparse ndarray.
- Type:
numpy.ndarray
- indices
A 2D ndarray with shape (N, rank), containing the indices of the nonzero values.
- Type:
numpy.ndarray
- dense_shape
An integer tuple with ‘rank’ elements, specifying the shape of the ndarray.
- Type:
tuple
Inheritance
digraph inheritance9994e3ad9a { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "SparseNdarray" [URL="#mt.np.SparseNdarray",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="A sparse ndarray, following TensorFlow's convention."]; }- static from_json(json_obj: str) SparseNdarray
Deserialises from a dictionary
- static from_spcoo(arr) SparseNdarray
Deserialises from a sparse matrix of type
scipy.sparse.coo_array
.
- to_json() str
Serialises to a dictionary.
- to_spcoo() scipy.sparse.coo_array
Serialises to a
scipy.sparse.coo_array
instance.Only works with a matrix or a vector, the latter is treated as a matrix with 1 column.