在R中缩放数值矩阵,值为0到1

时间:2013-03-18 02:25:46

标签: r scale

以下是我所拥有的数字矩阵的摘录

 [1,]   30 -33.129487   3894754.1 -39.701738 -38.356477 -34.220534
 [2,]   29 -44.289487  -8217525.9 -44.801738 -47.946477 -41.020534
 [3,]   28 -48.439487  -4572815.9 -49.181738 -48.086477 -46.110534
 [4,]   27 -48.359487  -2454575.9 -42.031738 -43.706477 -43.900534
 [5,]   26 -38.919487  -2157535.9 -47.881738 -43.576477 -46.330534
 [6,]   25 -45.069487  -5122485.9 -47.831738 -47.156477 -42.860534
 [7,]   24 -46.207487  -2336325.9 -53.131738 -50.576477 -50.410534
 [8,]   23 -51.127487  -2637685.9 -43.121738 -47.336477 -47.040534
 [9,]   22 -45.645487   3700424.1 -56.151738 -47.396477 -50.720534
[10,]   21 -56.739487   1572594.1 -49.831738 -54.386577 -52.470534
[11,]   20 -46.319487    642214.1 -39.631738 -44.406577 -41.490534

我现在要做的是将每列的值缩放为0到1之间的值。

我尝试使用我的矩阵上的scale()函数(默认参数)完成此操作,我得到了这个

[1,] -0.88123100  0.53812440 -1.05963281 -1.031191482 -0.92872324
 [2,] -1.17808251 -1.13538649 -1.19575096 -1.289013031 -1.11327085
 [3,] -1.28847084 -0.63180980 -1.31265244 -1.292776849 -1.25141017
 [4,] -1.28634287 -0.33914007 -1.12182012 -1.175023107 -1.19143220
 [5,] -1.03524267 -0.29809911 -1.27795565 -1.171528133 -1.25738083
 [6,] -1.19883019 -0.70775576 -1.27662116 -1.267774342 -1.16320727
 [7,] -1.22910054 -0.32280189 -1.41807728 -1.359719044 -1.36810940
 [8,] -1.35997055 -0.36443973 -1.15091204 -1.272613537 -1.27664977
 [9,] -1.21415156  0.51127451 -1.49868058 -1.274226602 -1.37652260
[10,] -1.50924749  0.21727976 -1.33000083 -1.462151358 -1.42401647
[11,] -1.23207969  0.08873245 -1.05776452 -1.193844887 -1.12602635

这已经接近我想要的,但0:1的值甚至更好。我阅读了scale()的帮助手册,但我真的不明白我会怎么做。

5 个答案:

答案 0 :(得分:20)

如果你还在使用scale

maxs <- apply(a, 2, max)
mins <- apply(a, 2, min)
scale(a, center = mins, scale = maxs - mins)

答案 1 :(得分:19)

尝试以下操作,这看起来很简单:

## Data to make a minimal reproducible example
m <- matrix(rnorm(9), ncol=3)

## Rescale each column to range between 0 and 1
apply(m, MARGIN = 2, FUN = function(X) (X - min(X))/diff(range(X)))
#           [,1]      [,2]      [,3]
# [1,] 0.0000000 0.0000000 0.5220198
# [2,] 0.6239273 1.0000000 0.0000000
# [3,] 1.0000000 0.9253893 1.0000000

答案 2 :(得分:2)

安装cluster-Sim软件包并运行以下命令:

normX = data.Normalization(x,type="n4");

答案 3 :(得分:0)

不是最漂亮,但这只是完成了工作,因为我需要在数据框中执行此操作。

column_zero_one_range_scale  <- function(
input_df,
columns_to_scale #columns in input_df to scale, must be numeric
){

input_df_replace <- input_df

columncount <- length(columns_to_scale)
for(i in 1:columncount){

columnnum <- columns_to_scale[i]

if(class(input_df[,columnnum]) !='numeric' & class(input_df[,columnnum])!='integer')
  {print(paste('Column name ',colnames(input_df)[columnnum],' not an integer or numeric, will skip',sep='')) }


if(class(input_df[,columnnum]) %in% c('numeric','integer'))
{
  vec <- input_df[,columnnum]
  rangevec <- max(vec,na.rm=T)-min(vec,na.rm=T)
  vec1 <- vec - min(vec,na.rm=T)
  vec2 <- vec1/rangevec
}
input_df_replace[,columnnum] <- vec2
colnames(input_df_replace)[columnnum] <- paste(colnames(input_df)[columnnum],'_scaled')

}

return(input_df_replace)


}

答案 4 :(得分:0)

class JobListCubit extends Cubit<JobListState> with HydratedMixin<JobListState> { JobListState get initialState { return initialState ?? JobListInitial(); } final JobListRepository jobListRepository; final UserAuthCubit userAuthCubit; JobListCubit({this.jobListRepository, this.userAuthCubit}) : super(JobListInitial()); ... 软件包具有一个称为scales的功能:

rescale
相关问题