多种归一化方法


在数据挖掘中,当不同的特征列在一起的时候,由于特征本身表达方式的原因而导致在绝对数值上的小数据被大数据“吃掉”的情况,这个时候我们需要做的就是对抽取出来的features vector进行归一化处理,以保证每个特征被分类器平等对待。

(0,1)标准化

x n o r m a l i z a t i o n = x − M i n M a x − M i n x_{normalization} = \frac{x-Min}{Max-Min} xnormalization=MaxMinxMin
取值范围:[0,1]

x = np.random.rand(10)
print(x)
x = (x - np.min(x)) / (np.max(x) - np.min(x));
print(x)
# 归一化前:[0.50121645 0.50608683 0.21882079 0.00466352 0.41783624 0.56337611
#          0.87656187 0.67758484 0.84233305 0.92616129]
#归一化后:[0.53885419 0.54413948 0.23240128 0.         0.44837083 0.60630922
#          0.94617522 0.73024737 0.90903045 1.        ]
#
#对列进行归一化
from sklearn import preprocessing
from sklearn.preprocessing import MinMaxScaler
X_train = np.array([[ 1., -1., 2.], [ 2., 0., 0.], [ 0., 1., -1.]])
#X_test = np.array([[ -3., -1., 4.]]) 对测试集进行归一化
min_max_scaler = preprocessing.MinMaxScaler()
X_train_minmax = min_max_scaler.fit_transform(X_train)
print(X_train_minmax)
# 结果:array([[0.5   , 0.    , 1.        ],
#             [1.    , 0.5   , 0.33333333],
#             [0.    , 1.    , 0.        ]])

Z-score标准化

这种方法给予原始数据的均值(mean)和标准差(standard deviation)进行数据的标准化。经过处理的数据符合标准正态分布,即均值为0,标准差为1:
x n o r m a l i z a t i o n = x − u σ x_{normalization} = \frac{x-u}{\sigma } xnormalization=σxu
标准化后的数据可正可负,一般绝对值不会太大。

x = np.random.rand(10)
print(x)
x = (x - np.average(x)) / np.std(x);
print(x)
from sklearn import preprocessing
import numpy as np
X = np.array([[ 1., -1.,  2.],
              [ 2.,  0.,  0.],
              [ 0.,  1., -1.]])
X_scaled = preprocessing.scale(X)
# 结果:
X_scaled = array([[ 0.        , -1.22474487,  1.33630621],
                  [ 1.22474487,  0.        , -0.26726124],
                  [-1.22474487,  1.22474487, -1.06904497]])
#归一化后的均值
X_scaled.mean(axis=0)
#array([0., 0., 0.])
#归一化后的方差
X_scaled.std(axis=0)
#array([ 1.,  1.,  1.])

Sigmoid函数

Sigmoid函数是一个具有S形曲线的函数,是良好的阈值函数,在(0, 0.5)处中心对称,在(0, 0.5)附近有比较大的斜率,而当数据趋向于正无穷和负无穷的时候,映射出来的值就会无限趋向于1和0:

x = np.random.rand(10)
print(x)
x = 1.0 / (1 + np.exp(-x))
print(x)

pyspark归一化

##############normalizer
dataFrame = spark.createDataFrame([(0, Vectors.dense([1.0, 0.5, -1.0]),),
                                   (1, Vectors.dense([2.0, 1.0, 1.0]),),
                                   (2, Vectors.dense([4.0, 10.0, 2.0]),)],
                                   ["id", "features"])
dataFrame.show()

from pyspark.ml.feature import Normalizer
from pyspark.ml.linalg import Vectors
normalizer = Normalizer(inputCol="features", outputCol="normFeatures", p=1.0)
l1NormData = normalizer.transform(dataFrame)
print("Normalized using L^1 norm")
l1NormData.show()

normalizer = Normalizer(inputCol="features", outputCol="normFeatures")
l1NormData = normalizer.transform(dataFrame,{normalizer.p:float(2)})
print("Normalized using L^2 norm")
l1NormData.show(truncate=False)
########StandardScaler
from pyspark.ml.feature import StandardScaler
scaler = StandardScaler(inputCol="features", outputCol="scaledFeatures",    withStd=True, withMean=True)
scalerModel = scaler.fit(dataFrame)
scaledData = scalerModel.transform(dataFrame)
scaledData.show()
########MinMaxScaler
from pyspark.ml.feature import MinMaxScaler
from pyspark.ml.linalg import Vectors
dataFrame = spark.createDataFrame([(0, Vectors.dense([1.0, 0.1, -1.0]),),    
                                   (1, Vectors.dense([2.0, 1.1, 1.0]),),    
                                   (2, Vectors.dense([3.0, 10.1, 3.0]),)],
                                   ["id", "features"])
scaler = MinMaxScaler(inputCol="features", outputCol="scaledFeatures")
scalerModel = scaler.fit(dataFrame)
scaledData = scalerModel.transform(dataFrame)
print("Features scaled to range: [%f, %f]" % (scaler.getMin(), scaler.getMax()))
scaledData.select("features", "scaledFeatures").show()

参考:https://www.aboutyun.com/thread-24358-1-1.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值