정규화는 min max scaling이라고도 말하며, 여러개의 변수들의 범위가 달라서 직접비교하기 힘들 때 범위를 0에서 1까지 변환해주는 전처리과정을 말한다.

표준화는 z-score라고도 말하며, 평균을 기준으로 얼마나 떨어져 있는지를 나타내는 값이다. z-score는 자료를 다소 평평하게 만드는 특성을 가진다.

#표준화
head(scale(iris[,1]))
##            [,1]
## [1,] -0.8976739
## [2,] -1.1392005
## [3,] -1.3807271
## [4,] -1.5014904
## [5,] -1.0184372
## [6,] -0.5353840
head(iris[,1]-mean(iris[,1]))/sd(iris[,1])
## [1] -0.8976739 -1.1392005 -1.3807271 -1.5014904 -1.0184372 -0.5353840
#정규화
head(iris[,1]-min(iris[,1]))/(max(iris[,1])-min(iris[,1]))
## [1] 0.22222222 0.16666667 0.11111111 0.08333333 0.19444444 0.30555556

자료에 적용

#표준화
head(cbind(iris,scale=scale(iris[,-5])))
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
##   scale.Sepal.Length scale.Sepal.Width scale.Petal.Length
## 1         -0.8976739        1.01560199          -1.335752
## 2         -1.1392005       -0.13153881          -1.335752
## 3         -1.3807271        0.32731751          -1.392399
## 4         -1.5014904        0.09788935          -1.279104
## 5         -1.0184372        1.24503015          -1.335752
## 6         -0.5353840        1.93331463          -1.165809
##   scale.Petal.Width
## 1         -1.311052
## 2         -1.311052
## 3         -1.311052
## 4         -1.311052
## 5         -1.311052
## 6         -1.048667
head(cbind(iris,apply(iris[,-5],2,function(x){(x-mean(x,na.rm=T))/sd(x,na.rm=T)})))
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length
## 1          5.1         3.5          1.4         0.2  setosa   -0.8976739
## 2          4.9         3.0          1.4         0.2  setosa   -1.1392005
## 3          4.7         3.2          1.3         0.2  setosa   -1.3807271
## 4          4.6         3.1          1.5         0.2  setosa   -1.5014904
## 5          5.0         3.6          1.4         0.2  setosa   -1.0184372
## 6          5.4         3.9          1.7         0.4  setosa   -0.5353840
##   Sepal.Width Petal.Length Petal.Width
## 1  1.01560199    -1.335752   -1.311052
## 2 -0.13153881    -1.335752   -1.311052
## 3  0.32731751    -1.392399   -1.311052
## 4  0.09788935    -1.279104   -1.311052
## 5  1.24503015    -1.335752   -1.311052
## 6  1.93331463    -1.165809   -1.048667
#정규화
head(cbind(iris,apply(iris[,-5],2,function(x){(x-min(x,na.rm=T))/(max(x,na.rm=T)-min(x,na.rm=T))})))
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length
## 1          5.1         3.5          1.4         0.2  setosa   0.22222222
## 2          4.9         3.0          1.4         0.2  setosa   0.16666667
## 3          4.7         3.2          1.3         0.2  setosa   0.11111111
## 4          4.6         3.1          1.5         0.2  setosa   0.08333333
## 5          5.0         3.6          1.4         0.2  setosa   0.19444444
## 6          5.4         3.9          1.7         0.4  setosa   0.30555556
##   Sepal.Width Petal.Length Petal.Width
## 1   0.6250000   0.06779661  0.04166667
## 2   0.4166667   0.06779661  0.04166667
## 3   0.5000000   0.05084746  0.04166667
## 4   0.4583333   0.08474576  0.04166667
## 5   0.6666667   0.06779661  0.04166667
## 6   0.7916667   0.11864407  0.12500000