normalize_weights#
- skrough.weights.normalize_weights(weights: ndarray) ndarray[source]#
Normalize weights.
Normalize input
weightsusing 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 thenormalize_weightsfunction uses a special procedure when 0-valued elements are found in the inputweightsvector. In such a casenumpy.nextafter()is used internally to increase all values towardsnumpy.infbefore 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])