scale的作用
1、中心化: 减去平均值
2、标准化: 除以根方差
test <- c(2,3,4,6) ## 测试数据 a <- scale(test,center = F, scale = F) ## 既不中心化, 也不标准化 a b <- scale(test, center = T, scale = F) ## 中心化, 但是不标准化 b b_test = test - mean(test) ##中心化, 减去平均值进行验证 b_test c <- scale(test, center = F, scale = T) ## 标准化, 但不中心化 c c_test <- test/sqrt(sum(test^2)/(length(test) - 1)) ##标准化, 除以根方差验证 c_test d <- scale(test, center = T, scale = T) ## 即中心化, 又标准化 d d_temp <- test - mean(test) ## 中心化, 减去平均值 d_test <- d_temp/sqrt(sum(d_temp^2)/(length(d_temp) - 1)) ## 标准化,除以根方差 d_test
运行过程:
> test <- c(2,3,4,6) > a <- scale(test,center = F, scale = F) > a ## a没有变化 [,1] [1,] 2 [2,] 3 [3,] 4 [4,] 6 > b <- scale(test, center = T, scale = F) ## 中心化 > b [,1] [1,] -1.75 [2,] -0.75 [3,] 0.25 [4,] 2.25 attr(,"scaled:center") [1] 3.75 > b_test = test - mean(test) ## 减去平均值验证 > b_test [1] -1.75 -0.75 0.25 2.25 > c <- scale(test, center = F, scale = T) ## 标准化 > c [,1] [1,] 0.4296689 [2,] 0.6445034 [3,] 0.8593378 [4,] 1.2890068 attr(,"scaled:scale") [1] 4.654747 > c_test <- test/sqrt(sum(test^2)/(length(test) - 1)) ## 标准化,除以根方差验证 > c_test [1] 0.4296689 0.6445034 0.8593378 1.2890068 > d <- scale(test, center = T, scale = T) ## 即中心化, 又标准化 > d [,1] [1,] -1.024695 [2,] -0.439155 [3,] 0.146385 [4,] 1.317465 attr(,"scaled:center") [1] 3.75 attr(,"scaled:scale") [1] 1.707825 > d_temp <- test - mean(test) ## 中心化 > d_test <- d_temp/sqrt(sum(d_temp^2)/(length(d_temp) - 1)) ## 除以根方差,标准化 > d_test [1] -1.024695 -0.439155 0.146385 1.317465
参考:https://blog.csdn.net/hac_kill_you/article/details/120498460