如何计算/标准化零均值和单位方差

时间:2023-01-05 04:17:36

What is "Zero mean and unit variance" and how to calculate/normalize it for single column file in R? I also want to divide the normalized values into two classes:

什么是“零均值和单位方差”以及如何计算/标准化R中的单列文件?我还想将规范化的值分为两类:

  1. normalized value at least 0.5 standard deviation (SD) above the mean
  2. 归一化值至少高于平均值0.5标准差(SD)
  3. normalized value at least 0.5 standard deviation (SD) below the mean
  4. 标准化值低于平均值至少0.5标准偏差(SD)

Thanks

谢谢

1 个解决方案

#1


5  

The quote "Zero mean and unit variance" means that the normalized variable has a mean of 0 and a standard deviation (and variance) of 1. One way to normalize variables in R is to use the scale function. Here is an example:

引用“零均值和单位方差”意味着归一化变量的均值为0,标准差(和方差)为1.在R中标准化变量的一种方法是使用比例函数。这是一个例子:

# create vector
set.seed(1234)
temp <- rnorm(20, 3, 7)

# take a look
> mean(temp)
[1] 1.245352
> sd(temp)
[1] 7.096653

# scale vector
tempScaled <- c(scale(temp))

# take a look
> mean(tempScaled)
[1] 1.112391e-17
> sd(tempScaled)
[1] 1

# find values below 0.5 standard deviation in scaled vector
tempScaled[tempScaled < -0.5]
# find values above 0.5 standard deviation in scaled vector
tempScaled[tempScaled > 0.5]

You could also scale the variable by hand pretty easily:

您还可以非常轻松地手动缩放变量:

tempScaled2 <- (temp - mean(temp)) / sd(temp) 

> all.equal(tempScaled, tempScaled2)
[1] TRUE

#1


5  

The quote "Zero mean and unit variance" means that the normalized variable has a mean of 0 and a standard deviation (and variance) of 1. One way to normalize variables in R is to use the scale function. Here is an example:

引用“零均值和单位方差”意味着归一化变量的均值为0,标准差(和方差)为1.在R中标准化变量的一种方法是使用比例函数。这是一个例子:

# create vector
set.seed(1234)
temp <- rnorm(20, 3, 7)

# take a look
> mean(temp)
[1] 1.245352
> sd(temp)
[1] 7.096653

# scale vector
tempScaled <- c(scale(temp))

# take a look
> mean(tempScaled)
[1] 1.112391e-17
> sd(tempScaled)
[1] 1

# find values below 0.5 standard deviation in scaled vector
tempScaled[tempScaled < -0.5]
# find values above 0.5 standard deviation in scaled vector
tempScaled[tempScaled > 0.5]

You could also scale the variable by hand pretty easily:

您还可以非常轻松地手动缩放变量:

tempScaled2 <- (temp - mean(temp)) / sd(temp) 

> all.equal(tempScaled, tempScaled2)
[1] TRUE