对数据进行非规范化 [英] denormalize data
问题描述
我使用以下R代码对数据的最小值和最大值进行了归一化:
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 ?
推荐答案
本质上,您只需要颠倒该算法即可:x1 = (x0-min)/(max-min)
表示x0 = x1*(max-min) + min
.但是,如果您要覆盖数据,则最好在进行规范化之前存储最小值和最大值,否则(如@MrFlick在评论中指出的那样)您注定要失败.
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.
设置数据:
dd <- data.frame(x=1:5,y=6:10)
归一化:
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
去规格化:
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
更聪明的normalize
函数会将缩放变量作为属性附加到结果(请参见?scale
函数...)
A cleverer normalize
function would attach the scaling variables to the result as attributes (see the ?scale
function ...)
这篇关于对数据进行非规范化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!