使用Python或R

时间:2019-02-26 11:22:51

标签: python r normalization

我有一个包含37个变量和50,000行的数据框。同时具有分类和数字特征。我想对数据框中的某些列执行归一化功能。

这是伪造的数据集:

diagnosis   gender  area    age weight    score   compactness   class
 447          1     95.88   50  117.66      674.8      80         0
 167          0     109.3   65  118.8       886.3      35.6       2
 444          0     117.5   80  160.85      990        64.2       2
 100          0     88.05   35  94.98       582.7      35.23      1
 227          1     97.45   40  15.51       684.5      70         1

我只想对面积,重量,分数,紧凑度进行归一化处理。我该怎么办?顺便说一句,我发现了一种here的标准偏差方法,但这意味着对整个数据集进行规范化,其代码为:

# identify outliers with standard deviation
from numpy.random import seed
from numpy.random import randn
from numpy import mean
from numpy import std

# calculate summary statistics
data_mean, data_std = mean(data), std(data)
# identify outliers
cut_off = data_std * 3
lower, upper = data_mean - cut_off, data_mean + cut_off
# identify outliers
outliers = [x for x in data if x < lower or x > upper]
print('Identified outliers: %d' % len(outliers))
# remove outliers
outliers_removed = [x for x in data if x >= lower and x <= upper]
print('Non-outlier observations: %d' % len(outliers_removed))

我的问题是如何仅对数据帧中的某些列进行标准化?感谢您的提前帮助!

1 个答案:

答案 0 :(得分:0)

我实际上有一个用于自动归一化的书面功能。如下:

n <-function(x){
  d=dim(x)
  c=colMeans(x)
  xm=sapply(1:d[2],function(i){
    x[,i]=x[,i]-c[i]
  })
  # xm is the x with removed means
  v=var(xm) # variance matrix
  xn=sapply(1:d[2],function(i){
    xm[,i]=xm[,i]/sqrt(v[i,i])
  })
  xn
}

然后只需将此功能应用于所需的列即可。

tochange=c("age","weight","score")
df[,tochange]=n(df[,tochange])
> df
     diagnosis gender   area        age     weight      score
[1,]       447      1  95.88 -0.2161373  0.3000106 -0.5282662
[2,]       167      0 109.30  0.5943775  0.3212536  0.7290858
[3,]       444      0 117.50  1.4048924  1.1048216  1.3455747
[4,]       100      0  88.05 -1.0266521 -0.1226130 -1.0757939
[5,]       227      1  97.45 -0.7564805 -1.6034728 -0.4706004
     compactness class
[1,]       80.00     0
[2,]       35.60     2
[3,]       64.20     2
[4,]       35.23     1
[5,]       70.00     1