Home > OS >  Normalize the espicific rows of an array
Normalize the espicific rows of an array

Time:11-18

I have an array with size ( 61000) I want to normalize it based on this rule: Normalize the rows 0, 6, 12, 18, 24, ... (6i for i in range(1000)) based on the formulation which I provide. Dont change the values of the other rows. Here is an example:

def normalize(array):
    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   0.00001)

Calling with the following input doesn't work:

A = array([[15, 14,  3],
   [11,  9,  9],
   [16,  6,  1],
   [14,  6,  9],
   [ 1, 12,  2],
   [ 5,  1,  2],
   [13, 11,  2],
   [11,  4,  1],
   [11,  7, 10],
   [10, 11, 16],
   [ 2, 13,  4],
   [12, 14, 14]])

normalize(A)

I expect the following output:

array([[0.99999917, 0.9166659 , 0.    ],
   [11,  9,  9],
   [16,  6,  1],
   [14,  6,  9],
   [ 1, 12,  2],
   [ 5,  1,  2],
   [0.99999909, 0.81818107, 0.        ]],
   [11,  4,  1],
   [11,  7, 10],
   [10, 11, 16],
   [ 2, 13,  4],
   [12, 14, 14]])

CodePudding user response:

You have to set up a second function having the step argument:

def normalize_with_step(array, step):
    
    b = normalize(array[::step])
    a, b = list(array), list(b)
    
    for i in range(0, len(a), step):
        a[i] = b[int(i/step)]
        
    a = np.array(a)
    return a

Let's try it with a step = 6:

a = np.random.randint(17, size = (12, 3))
a = normalize_with_step(a, 6)
a

Output

array([[ 0.83333264,  0.99999917,  0.        ],
       [ 9.        , 14.        ,  6.        ],
       [14.        , 15.        , 12.        ],
       [12.        ,  7.        , 10.        ],
       [ 8.        , 13.        ,  9.        ],
       [12.        ,  0.        ,  3.        ],
       [ 0.53333298,  0.99999933,  0.        ],
       [15.        , 14.        , 12.        ],
       [14.        ,  6.        , 16.        ],
       [ 9.        , 14.        ,  3.        ],
       [ 8.        ,  9.        ,  0.        ],
       [10.        , 13.        ,  0.        ]])
  • Related