helpers.replace_values

helpers.replace_values(arr, mapping)

Replace values in a numpy array based on a provided mapping dictionary

Parameters

Name Type Description Default
arr numpy.numpy.ArrayLike A numpy array with values to be replaced. required
mapping dict A dictionary where keys correspond to values in arr and values are the replacement values. required

Returns

Type Description
numpy.numpy.ndarray A numpy array with replaced values.

Raises

Type Description
ValueError If mapping does not contain the same unique values as in arr, or if the keys do not match.

Examples:

import pyLHD
random_ls = pyLHD.LatinSquare(size = (4,4), seed = 1)
random_ls
array([[2, 3, 1, 2],
       [4, 2, 4, 4],
       [1, 4, 3, 3],
       [3, 1, 2, 1]])

Consider the mapping \(1 \rightarrow 2, 2 \rightarrow 11, 3 \rightarrow 12, 4 \rightarrow 13\)

mapping = {1:10, 2:11, 3:12, 4:13}
pyLHD.replace_values(random_ls, mapping = mapping)
array([[11, 12, 10, 11],
       [13, 11, 13, 13],
       [10, 13, 12, 12],
       [12, 10, 11, 10]])