I normalized data with the minimum and maximum with this R code:
normalize <- function(x) {
return ((x - min(x)) / (max(x) - min(x)))
}
mydata <- as.data.frame(lapply(mydata , normalize))
How can I denormalize the data ?
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 ...)
来源:https://stackoverflow.com/questions/39355942/denormalize-data