artlib.common.utils

General utilities used throughout ARTLib.

Attributes

IndexableOrKeyable

Functions

normalize(→ Tuple[numpy.ndarray, numpy.ndarray, ...)

Normalize data column-wise between 0 and 1.

de_normalize(→ numpy.ndarray)

Restore column-wise normalized data to original scale.

complement_code(→ numpy.ndarray)

Complement code the data.

de_complement_code(→ numpy.ndarray)

Find the centroid of complement coded data.

l1norm(→ float)

Get the L1 norm of a vector using Numba.

l2norm2(→ float)

Get the squared L2 norm of a vector.

fuzzy_and(→ numpy.ndarray)

Get the fuzzy AND operation between two vectors.

fracsort(→ numpy.typing.NDArray[numpy.intp])

Get argsort indices for elementwise fractions num[i] / den[i] without

fracargmax(→ numpy.intp)

Get the index that maximizes the elementwise fractions num[i] / den[i]

binarize_features_thermometer(→ numpy.ndarray)

Binarizes each feature in the data using thermometer encoding.

Module Contents

artlib.common.utils.IndexableOrKeyable
artlib.common.utils.normalize(data: numpy.ndarray, d_max: numpy.ndarray | None = None, d_min: numpy.ndarray | None = None) Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]

Normalize data column-wise between 0 and 1.

Parameters:
  • data (np.ndarray) – 2D array of dataset (rows = samples, columns = features).

  • d_max (np.ndarray, optional) – Maximum values for each column.

  • d_min (np.ndarray, optional) – Minimum values for each column.

Returns:

  • np.ndarray – Normalized data.

  • np.ndarray – Maximum values for each column.

  • np.ndarray – Minimum values for each column.

artlib.common.utils.de_normalize(data: numpy.ndarray, d_max: numpy.ndarray, d_min: numpy.ndarray) numpy.ndarray

Restore column-wise normalized data to original scale.

Parameters:
  • data (np.ndarray) – Normalized data.

  • d_max (np.ndarray) – Maximum values for each column.

  • d_min (np.ndarray) – Minimum values for each column.

Returns:

De-normalized data.

Return type:

np.ndarray

artlib.common.utils.complement_code(data: numpy.ndarray) numpy.ndarray

Complement code the data.

Parameters:

data (np.ndarray) – Dataset.

Returns:

complement coded data.

Return type:

np.ndarray

artlib.common.utils.de_complement_code(data: numpy.ndarray) numpy.ndarray

Find the centroid of complement coded data.

Parameters:

data (np.ndarray) – Dataset.

Returns:

De-complement coded data.

Return type:

np.ndarray

artlib.common.utils.l1norm(x: numpy.ndarray) float

Get the L1 norm of a vector using Numba.

Parameters:

x (np.ndarray) – Input vector.

Returns:

L1 norm.

Return type:

float

artlib.common.utils.l2norm2(data: numpy.ndarray) float

Get the squared L2 norm of a vector.

Parameters:

data (np.ndarray) – Input vector.

Returns:

Squared L2 norm.

Return type:

float

artlib.common.utils.fuzzy_and(x: numpy.ndarray, y: numpy.ndarray) numpy.ndarray

Get the fuzzy AND operation between two vectors.

Parameters:
  • x (np.ndarray) – First input vector.

  • y (np.ndarray) – Second input vector.

Returns:

Fuzzy AND result.

Return type:

np.ndarray

artlib.common.utils.fracsort(num: numpy.typing.ArrayLike, den: numpy.typing.ArrayLike) numpy.typing.NDArray[numpy.intp]

Get argsort indices for elementwise fractions num[i] / den[i] without division.

This function returns an index array that sorts the rational values exactly using cross-multiplication in a compiled C++ backend (no division is performed). Ties are broken by the lowest original index.

Parameters:
  • num (ArrayLike) – 1D array-like of nonnegative numerators. Must be convertible to a contiguous NumPy array with dtype np.uint32 or np.uint64.

  • den (ArrayLike) – 1D array-like of denominators with den[i] >= 1. Must be convertible to a contiguous NumPy array with dtype np.uint32 or np.uint64 and have the same shape and dtype as num.

Returns:

Indices that sort num[i] / den[i] in ascending order, with ties broken by the lowest index.

Return type:

NDArray[np.intp]

artlib.common.utils.fracargmax(num: numpy.typing.ArrayLike, den: numpy.typing.ArrayLike) numpy.intp

Get the index that maximizes the elementwise fractions num[i] / den[i] without division.

This function returns the index of the maximum rational value exactly using cross-multiplication in a compiled C++ backend (no division is performed). Ties are broken first by the larger denominator, then by the lowest original index.

Parameters:
  • num (ArrayLike) – 1D array-like of nonnegative numerators. Must be convertible to a contiguous NumPy array with dtype np.uint32 or np.uint64.

  • den (ArrayLike) – 1D array-like of denominators with den[i] >= 1. Must be convertible to a contiguous NumPy array with dtype np.uint32 or np.uint64 and have the same shape and dtype as num.

Returns:

Index i that maximizes num[i] / den[i] (descending). Ties are broken by larger denominator first, then the lowest index.

Return type:

np.intp

artlib.common.utils.binarize_features_thermometer(data: numpy.ndarray, n_bits: int) numpy.ndarray

Binarizes each feature in the data using thermometer encoding.

Parameters:
  • data (np.ndarray) – Input array of shape (n, m), where n is the number of samples and m is the number of features.

  • n_bits (int) – Number of bits to use for thermometer encoding.

Returns:

A thermometer-coded representation of the input data with

shape (n, m * n_bits).

Return type:

np.ndarray