将数据从标准化值转换回来

时间:2017-01-19 06:51:54

标签: r dataframe normalize

我已使用scale来规范化数据。示例数据如下所示

structure(list(pp.pmhouravg = c(106.8181818182, 114.0833333333, 
100.8333333333, 105, 102.4166666667, 117.8333333333), cc.cmhouravg = c(91.7272727273, 
86.4166666667, 82.75, 84, 59.5833333333, 41.3333333333), ss.sdhouravg = c(49.2727272727, 
46.8333333333, 47.5, 48.3333333333, 41, 45.5833333333), nn.ndhouravg = c(41.2727272727, 
45.25, 34.0833333333, 27.75, 33.0833333333, 35.3333333333)), .Names = c("pp.pmhouravg", 
"cc.cmhouravg", "ss.sdhouravg", "nn.ndhouravg"), row.names = c(NA, 
6L), class = "data.frame")

并将其标准化我使用

scale(df, center = T, scale = T)

我得到了以下标准化数据:

pp.pmhouravg cc.cmhouravg ss.sdhouravg nn.ndhouravg
1   -0.1504657    0.8893812    0.9702219    0.8259116
2    0.9290599    0.6183329    0.1404438    1.4645030
3   -1.0397516    0.4311897    0.3672155   -0.3284185
4   -0.4206285    0.4949885    0.6506801   -1.3452993
5   -0.8044848   -0.7512149   -1.8438082   -0.4889786
6    1.4862706   -1.6826775   -0.2847531   -0.1277183
attr(,"scaled:center")
pp.pmhouravg cc.cmhouravg ss.sdhouravg nn.ndhouravg 
   107.83081     74.30177     46.42045     36.12879 
attr(,"scaled:scale")
pp.pmhouravg cc.cmhouravg ss.sdhouravg nn.ndhouravg 
    6.729949    19.592842     2.939815     6.228196 

如何在规范化后将数据转换回来。

2 个答案:

答案 0 :(得分:3)

x成为您的原始数据(可能是数据框或矩阵),sx是缩放的数据(必须是矩阵,因为scale返回矩阵),你可以这样做:

b <- attr(sx, "scaled:scale")
a <- attr(sx, "scaled:center")
rx <- sx * rep(b, each = nrow(sx)) + rep(a, each = nrow(sx))

&#34; de-scaled&#34;数据rx当然也是一个矩阵,因为sx是一个矩阵。您只需执行以下操作即可将其设为数据框:

data.frame(rx)

答案 1 :(得分:2)

从概念上来说,我们也可以自己尝试z-score-normalization(这与scale()具有完全相同的结果)并获得原始矩阵:

# save the mean and sd
mu <- colMeans(df)    
sd <- sapply(df, sd)

scaled <- t((t(as.matrix(df)) - mu) / sd) # z-score-normalize
all(scaled == scale(df, center = T, scale = T)) # check the scaled matrix is same as obtained from scale()
#[1] TRUE

orig <- t(t(scaled)*sd + mu) # get the original matrix back
all.equal(as.matrix(df), orig)
#[1] TRUE