Home > Enterprise >  Min-max normalization based on a part of row
Min-max normalization based on a part of row

Time:04-22

I have a numpy array and I want to normalize the array based on max and min of a constant window size of the row and then using x_normal = (x-min)/(max-min).

For example, in the following array, I choose the max and min of each 4 column in each row. For the first row, 4 columns (3, 5, 3, 3) min and max is 3, 5 and then (3-3/5-3,5-3/5-3, 3-3/5-3, 3-3/5-3) and then for (9,2,2,5) the min and max is 2, 9, then (9-2/9-2, 2-2/9-2,2-2/9-2, 5-2/9-2) and so on.

import numpy as np
x = np.array([
       [3, 5, 3, 3, 9, 2, 2, 5, 1, 0, 7, 2],
       [4, 4, 8, 4, 3, 1, 4, 8, 7, 6, 1, 4]
       ])

output:

x = np.array([
       [0, 1, 0, 0, 1, 0, 0, 3/7, 1/6, 0, 1, 2/6],
       [0, 0, 1, 0, 2/7, 0, 3/7, 1, 1, 5/6, 0, 3/6]
       ])

CodePudding user response:

import numpy as np
import numpy.typing as npt


def normalize(array: npt.NDArray) -> npt.NDArray:
    minimum = np.expand_dims(np.min(array, axis=1), axis=1)
    maximum = np.expand_dims(np.max(array, axis=1), axis=1)
    return (array - minimum) / (maximum - minimum)

x = np.array([
    [3, 5, 3, 3, 9, 2, 2, 5, 1, 0, 7, 2],
    [4, 4, 8, 4, 3, 1, 4, 8, 7, 6, 1, 4]
]).astype(float)

windows_size = 4
for i in range(0, x.shape[1], windows_size):
    x[:, i: i   windows_size] = normalize(x[:, i: i   windows_size])

The approach:

  • Select sliding windows size
  • Take the subarray with specified windows size
  • Calculate min/max per column
  • Perform subtract and division per column
  • Related