简体   繁体   中英

Normalization function using fit_transform idea

I want to normalize my X_train and Y_train data by a function that I wrote on the same principle of MinMax Scaler. And I want that the function return the min and max of train data so that I can use them on validation dataset. That is the code that I wrote:

def normalize(x, min=False, max=False):
  
    if min is False:
       min = np.min(x)
    if max is False:
       max = np.max(x)
    range = max - min

    return [(a - min) / range for a in x], min, max

The problem is that my code return same min and max whatever the data is. 0 for min and 1 for max. I cannot see what cause the problem. Could someone please help me to debug this code.

If I Understand Correctly (IIUC), You run your function multiple on the same data, and after one-time normalization, the min and max of your data are 0 and 1 any times you do normalization.

>>> first_run = normalize([1,2,3])
>>> first_run
([0.0, 0.5, 1.0], 1, 3)

>>> second_run = normalize(first_run[0])
>>> second_run
([0.0, 0.5, 1.0], 0.0, 1.0)

>>> third_run = normalize(second_run[0])
>>> third_run
([0.0, 0.5, 1.0], 0.0, 1.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM