[Python] How to normalise a two-dimensional array

Discussão em 'Python' iniciado por Stack, Fevereiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    I am given a two-dimensional array. Each element represents the x,y coordinates of a trapezium. I do not want negative values so I need to adjust the minimum x value to zero and I want to adjust the minimum y value to zero. I can do this (see below), but in a very long winded way. Is there a more elegant way to normalise the arrays?

    import numpy as np

    # This line is given
    pa = np.array([[ 213.00002 , 213.00002 ],[ -213.00002 , 213.00002 ],[ 213.00002 , -213.00002 ],[ -213.00002 , -213.00002 ]])
    # This line is given

    #1 get values
    pa_x_values = np.array([(pa[0][0]),(pa[1][0]),(pa[2][0]),(pa[3][0])])
    pa_y_values = np.array([(pa[0][1]),(pa[1][1]),(pa[2][1]),(pa[3][1])])



    #2 get minimum value
    pa_min_x = min(pa_x_values)
    pa_min_y = min(pa_y_values)



    #3 calculate difference from zero
    p1_dx = 0 - pa_min_x
    p1_dy = 0 - pa_min_y



    #4 make new array
    p1 = np.array([[0,0],[0,0],[0,0],[0,0]],dtype=np.float32)


    #5 normalise
    for i in range(4):
    p1[0] = (pa[0]) + p1_dx
    p1[1] = (pa[1]) + p1_dy



    # RESULT
    # p1
    # [[426.00003, 426.00003], [ 0, 426.00003], [426.00003, 0 ], [ 0, 0 ]]

    Continue reading...

Compartilhe esta Página