normalize_weights#

skrough.weights.normalize_weights(weights: ndarray) ndarray[source]#

Normalize weights.

Normalize input weights using 1-norm (manhattan) norm. The function is intended to be used for normalization of weights of elements under consideration (e.g., attributes, objects/instances), thus preparing discrete probability distribution used later in various draw tasks. Some of the draw methods cannot handle 0-valued probabilities and therefore the normalize_weights function uses a special procedure when 0-valued elements are found in the input weights vector. In such a case numpy.nextafter() is used internally to increase all values towards numpy.inf before and after (to overcome edge cases with close to zero values) normalization.

The function does not check for negative values in the input weights. Therefore, using the function with such inputs may produce unexpected results, especially when the output of the function is later used as a discrete probability distribution.

Parameters:

weights – Values to be normalized.

Returns:

Normalized weights.

Examples

>>> normalize_weights(np.asarray([1, 1, 2]))
array([0.25, 0.25, 0.5])
>>> normalize_weights(np.asarray([1, 3]))
array([0.25, 0.75])
>>> normalize_weights(np.asarray([0, 0]))
array([0.5, 0.5])
>>> normalize_weights(np.asarray([0, 1]))
array([2.22044605e-16, 1.00000000e+00])
>>> normalize_weights(np.asarray([-1, 1]))
array([-0.5, 0.5])