• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

R语言数据标准化

武飞扬头像
qq_27390023
帮助1

在数据分析之前,我们通常需要先将数据标准化(normalization),利用标准化后的数据进行数据分析。 数据的标准化(normalization)是将数据按比例缩放,使之落入一个小的特定区间, 其中最典型的就是数据的归一化处理,即将数据统一映射到[0,1]区间上。  数据归一化后可以提升模型的收敛速度和精度。

  1.  
    ### 1. 离差标准化
  2.  
    # 数据映射到[0,1]
  3.  
    mat <- matrix(data = sample(0:10000,1000,replace=TRUE),
  4.  
    nrow = 100, ncol = 10, byrow = TRUE,
  5.  
    dimnames = list(paste0("gene", 1:100),
  6.  
    paste0("sample", 1:10)))
  7.  
     
  8.  
     
  9.  
    normalize <- function(x){
  10.  
    return ((x-min(x))/(max(x)-min(x)))
  11.  
    }
  12.  
     
  13.  
    apply(mat,2,normalize) # 对列操作
  14.  
     
  15.  
     
  16.  
    # 数据映射到[-1,1]
  17.  
    normalize2 <- function(x){
  18.  
    return ((x-mean(x))/(max(x)-min(x)))
  19.  
    }
  20.  
     
  21.  
    class(mat) # [1] "matrix" "array"
  22.  
    mat_norm <- apply(mat,2,normalize2)
  23.  
    class(mat_norm) # [1] "matrix" "array"
  24.  
     
  25.  
    ### 2. log函数转换
  26.  
    # 所有的数据都要大于等于1
  27.  
    # 除以log10(max)后在[0,1]区间上
  28.  
     
  29.  
    # log(x, base)
  30.  
    log(10) #default 自然对数
  31.  
    log(10,10)
  32.  
    log(10,2)
  33.  
     
  34.  
    lognorm <- function(x){
  35.  
    return (log(x,10)/log(max(x),10))
  36.  
    }
  37.  
     
  38.  
    mat_lognorm <- apply(mat,2,lognorm)
  39.  
    # 二代测序数据基因表达数据,往往 1 再取log
  40.  
     
  41.  
    ### 3.z-score标准化,标准差标准化
  42.  
    # 经过该方法处理的数据的均值是0,标准差是1
  43.  
    # z = (x-μ)/σ
  44.  
     
  45.  
    scale(mat) #
  46.  
    class(scale(mat)) # [1] "matrix" "array"
  47.  
     
  48.  
    # x <- 1:10
  49.  
    x <- matrix(1:10, ncol = 2)
  50.  
    scale(x) # z-score normalization
  51.  
    #scale(x,center = TRUE, scale = TRUE)
  52.  
     
  53.  
    ### 4. 各种包中的标准化函数
  54.  
    # R语言很多bioconductor包都有自己的数据标准化预处理方法
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfikigc
系列文章
更多 icon
同类精品
更多 icon
继续加载