[Python MinMaxScaler] Normalize to 0 ~ 1

Normalization using sklearn.preprocessing.MinMaxScaler

What is MinMaxScaler normalization?

Conversion from 0 to 1 by the following formula image.png

Code example

Usage example when the minimum and maximum values ​​such as latitude and longitude are determined in advance

Sample data

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]])

Definition of minimum and maximum values

#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.

Normalization

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   ]])

Denormalized (revert to before normalization)

#Denormalize sample data
mmscaler.inverse_transform(scaled_sample)

=> array([[ 35.6712, 139.7665],
          [ 35.6812, 139.7671],
          [ 35.658 , 139.7016]])

Reference

Recommended Posts

[Python MinMaxScaler] Normalize to 0 ~ 1
Updated to Python 2.7.9
"Backport" to python 2
How to install Python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Rewrite Python2 code to Python3 (2to3)
How to install python
Introduction to Python language
Introduction to OpenCV (python)-(2)
Introducing Python 2.7 to CentOS 6.6
Connect python to mysql
Connect to BigQuery with Python
[2020.8 latest] How to install Python
[python] Convert date to string
Post from Python to Slack
How to install Python [Windows]
Post to vim → Python → Slack
To flush stdout in Python
Convert numpy int64 to python int
python3: How to use bottle (2)
[Python] Convert list to Pandas [Pandas]
Cheating from PHP to Python
A road to intermediate Python
Try to understand Python self
Python notes to forget soon
[Python] How to use list 1
Connect to Wikipedia with Python
How to update Python Tkinter to 8.6
Post to slack with Python 3
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Post to Twitter using Python
Start to Selenium using python
Introduction to serial communication [Python]
Update python on Mac to 3.7-> 3.8
3 Reasons Beginners to Start Python
Convert Scratch project to Python
Python: How to use pydub
[Python] How to use checkio
Switch from python2.7 to python3.6 (centos7)
How to run Notepad ++ Python
Speech to speech in python [text to speech]
[Python] Another way to import
Connect to sqlite from python
[Introduction to Python] <list> [edit: 2020/02/22]
How to develop in Python
~ Tips for beginners to Python ③ ~
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
Introduction to Python For, While
python3: How to use bottle (3)
python3: How to use bottle
Convert python 3.x code to python 2.x
Until Toot to Mastodon (Python)
Post to Slack in Python
How to use Python bytes
Call Matlab from Python to optimize
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments