0
0

More than 1 year has passed since last update.

z-score normalization (python での実装コードあり)

Posted at

z-score normalization とは

下記の式で表現され

$$
\tilde{x} = \dfrac{x-\bar {x}}{\sigma}
$$

$\bar{x}$は平均、$\sigma$は標準偏差を表す。
この標準化方法はそれぞれの値が平均からどれだけ離れているかをばらつき(標準偏差)で補正してあげることで、単位が異なる数値同士を比較できるようになる。

例えば...
体重(kg)と身長(cm)を比較したい時などに z-score normalization を使うことで比較が容易になる。

身長 Z-score(身長) 体重 Z-score(体重)
A 170 0 70 -1.225
B 160 -1.225 90 1.225
C 180 1.225 80 0

z-score はデータを平均 : 0, 標準偏差 : 1 にスケールするので、そこからわかることは

  • A の人は身長が平均ちょうどで、体重が比較的軽い
  • B の人は身長が比較的低く、体重が比較的重い
  • C の人は身長が比較的高く、体重が平均ちょうど

補足 (実装コード)

標準偏差

$$
\sigma = \sqrt{\frac {1}{n} \sum_{i=1}^{n} (X_i - \bar{X})^2}
$$

実装コード

scikit_learnの読み込み & 標準化ライブラリの読み込み
numpy の読み込み


import sklearn
from sklearn.preprocessing import StandardScaler
import numpy as np

データの用意

df_height = np.array([[170], [160], [180]])
df_weight = np.array([[70], [90], [80]])

それぞれを z-score normalization 処理

df_height_norm = scaler.fit_transform(df_height)
df_weight_norm = scaler.fit_transform(df_weight)

確認

print(df_height_norm)
print(df_weight_norm)

出力結果
(上から3つが身長のz-score、4つ目からが体重のz-score)
[[ 0. ]
[-1.22474487]
[ 1.22474487]]
[[-1.22474487]
[ 1.22474487]
[ 0. ]]

0
0
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
0
0