helpers.swap_elements

helpers.swap_elements(arr, idx, axis=1, seed=None)

Swap two random elements in a matrix

Parameters

Name Type Description Default
arr numpy.numpy.ArrayLike A numpy ndarray required
idx int A positive integer, which stands for the (idx) column or row of (arr) required
axis int If axis is 1, two random elements will be exchanged within column (idx). If axis is 0, two random elements will be exchanged within row (idx). Defaults to axis = 1. 1
seed Optional[Union[int, np.random.Generator]]) If seedis an integer or None, a new numpy.random.Generator is created using np.random.default_rng(seed). If seed is already a `Generator instance, then the provided instance is used. Defaults to None. None

Returns

Type Description
numpy.numpy.ndarray A new design matrix after the swap of elements

Examples: Choose the first columns of random_lhd and swap two randomly selected elements

import pyLHD
random_lhd = pyLHD.LatinHypercube(size = (5,3))
random_lhd
array([[0.03643722, 0.23643722, 0.63643722],
       [0.43643722, 0.03643722, 0.03643722],
       [0.23643722, 0.83643722, 0.23643722],
       [0.63643722, 0.63643722, 0.43643722],
       [0.83643722, 0.43643722, 0.83643722]])

Choose column 1 of random_lhd and swap two randomly selected elements

pyLHD.swap_elements(random_lhd,idx=1)
array([[0.03643722, 0.23643722, 0.63643722],
       [0.43643722, 0.03643722, 0.03643722],
       [0.23643722, 0.63643722, 0.23643722],
       [0.63643722, 0.83643722, 0.43643722],
       [0.83643722, 0.43643722, 0.83643722]])

Choose the first row of random_lhd and swap two randomly selected elements

pyLHD.swap_elements(random_lhd,idx=1,axis = 0)
array([[0.03643722, 0.23643722, 0.63643722],
       [0.03643722, 0.43643722, 0.03643722],
       [0.23643722, 0.63643722, 0.23643722],
       [0.63643722, 0.83643722, 0.43643722],
       [0.83643722, 0.43643722, 0.83643722]])