zoukankan      html  css  js  c++  java
  • R语言中scale函数的用法

    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

  • 相关阅读:
    UVALive 6044(双连通分量的应用)
    hdu 3760(2次bfs求最短路)
    zoj 3370(二分+二分图染色)
    sgu 326(经典网络流构图)
    hdu 4291(矩阵+暴力求循环节)
    uva 11381(神奇的构图、最小费用最大流)
    hdu 4685(匹配+强连通分量)
    hdu 4496(并查集)
    hdu 4722(记忆化搜索)
    Linux安装Nginx使用负载均衡
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15703506.html
Copyright © 2011-2022 走看看