denormalize data

谁说胖子不能爱 提交于 2019-12-01 14:03:20

Essentially, you just have to reverse the arithmetic: x1 = (x0-min)/(max-min) implies that x0 = x1*(max-min) + min. However, if you're overwriting your data, you'd better have stored the min and max values before you normalized, otherwise (as pointed out by @MrFlick in the comments) you're doomed.

Set up data:

dd <- data.frame(x=1:5,y=6:10)

Normalize:

normalize <- function(x) {
    return ((x - min(x)) / (max(x) - min(x)))
}
ddnorm <- as.data.frame(lapply(dd,normalize))
##      x    y
## 1 0.00 0.00
## 2 0.25 0.25
## 3 0.50 0.50
## 4 0.75 0.75
## 5 1.00 1.00

Denormalize:

minvec <- sapply(dd,min)
maxvec <- sapply(dd,max)
denormalize <- function(x,minval,maxval) {
    x*(maxval-minval) + minval
}
as.data.frame(Map(denormalize,ddnorm,minvec,maxvec))
##   x  y
## 1 1  6
## 2 2  7
## 3 3  8
## 4 4  9
## 5 5 10

A cleverer normalize function would attach the scaling variables to the result as attributes (see the ?scale function ...)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!