LoginSignup
2
3

More than 3 years have passed since last update.

LSTMのための正規化例(sklearn.preprocessing.MinMaxScaler)

Posted at

概要

LSTMのためのデータ正規化の例。
sklearn.preprocessing.MinMaxScaler
環境
- Google Colaboratory
- Keras
参考
- https://machinelearningmastery.com/how-to-scale-data-for-long-short-term-memory-networks-in-python/

MinMaxScalerインポート、データ定義


from pandas import Series
from sklearn.preprocessing import MinMaxScaler

## define contrived series
data = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0]
series = Series(data)
print(series)

# 0     10.0
# 1     20.0
# 2     30.0
# 3     40.0
# 4     50.0
# 5     60.0
# 6     70.0
# 7     80.0
# 8     90.0
# 9    100.0
# dtype: float64

正規化の定義 : fit


## prepare data for normalization
values = series.values
values = values.reshape((len(values), 1))

## train the normalization
scaler = MinMaxScaler(feature_range=(0, 1))
scaler = scaler.fit(values)
print('Min: %f, Max: %f' % (scaler.data_min_, scaler.data_max_))

# Min: 10.000000, Max: 100.000000

データセットの正規化 : transform

データを0から1の間に変換する

## normalize the dataset and print
normalized = scaler.transform(values)
print(normalized)

# [[0.        ]
#  [0.11111111]
#  [0.22222222]
#  [0.33333333]
#  [0.44444444]
#  [0.55555556]
#  [0.66666667]
#  [0.77777778]
#  [0.88888889]
#  [1.        ]]

正規化したデータセットをもとに戻す:inverse_transform

## inverse transform and print
inversed = scaler.inverse_transform(normalized)
print(inversed)
# [[ 10.]
#  [ 20.]
#  [ 30.]
#  [ 40.]
#  [ 50.]
#  [ 60.]
#  [ 70.]
#  [ 80.]
#  [ 90.]
#  [100.]]

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3