Normalization using sklearn.preprocessing.MinMaxScaler
Conversion from 0 to 1 by the following formula
Usage example when the minimum and maximum values such as latitude and longitude are determined in advance
import numpy as np
from sklearn.preprocessing import MinMaxScaler
#Latitude / longitude sample data set in Tokyo
sample = np.array([[35.6712, 139.7665], [35.6812, 139.7671], [35.6580, 139.7016]]) # shape=(3, 2)
=> array([[ 35.6712, 139.7665],
[ 35.6812, 139.7671],
[ 35.658 , 139.7016]])
#Define minimum and maximum values
# [latitude,longitude]
min_li = [-90, -180]
max_li = [90, 180]
min_max_li = np.array([min_li, max_li]) # shape=(2, 2)
#Define minimum and maximum values to use in normalization
mmscaler = MinMaxScaler(feature_range=(0, 1), copy=True)
mmscaler.fit(min_max_li.astype('float'))
# mmscaler.fit(sample)If, the minimum and maximum values are automatically defined for each column of sample data.
The data to be normalized must have the same number of columns as the data used to define the minimum and maximum values. This time there are two columns, latitude and longitude
#Normalize for sample data
scaled_sample = mmscaler.transform(sample)
=> array([[0.69817333, 0.88824028],
[0.69822889, 0.88824194],
[0.6981 , 0.88806 ]])
#Denormalize sample data
mmscaler.inverse_transform(scaled_sample)
=> array([[ 35.6712, 139.7665],
[ 35.6812, 139.7671],
[ 35.658 , 139.7016]])
Reference
Recommended Posts