当前位置:   article > 正文

numpy 归一化 与 标准化_numpy 标准化

numpy 标准化

代码

import numpy as np


def normalization(x):
    """"
    归一化到区间{0,1]
    返回副本
    """
    _range = np.max(x) - np.min(x)
    return (x - np.min(x)) / _range


def standardization(x):
    """"
     将输入x 正态标准化  (x - mu) / sigma   ~   N(0,1)
     返回副本
    """
    mu = np.mean(x, axis=0)
    sigma = np.std(x, axis=0)
    return (x - mu) / sigma


YUAN = np.random.randint(3, 70, size=5)
a = normalization(YUAN)
b = standardization(YUAN)
print(YUAN, "normalization:", a)
print(YUAN is a)
print(YUAN, "standardization:", b)
print(YUAN is b)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

结果

[33 50 60 12 32] normalization: [0.4375     0.79166667 1.         0.         0.41666667]
False
[33 50 60 12 32] standardization: [-0.26647587  0.76308999  1.36871697 -1.53829253 -0.32703857]
False
  • 1
  • 2
  • 3
  • 4

sklearn.preprocessing.scale 能沿某个轴标准化

import numpy as np
from sklearn import  preprocessing

def standardization(x):
    """"
     将输入x 正态标准化  (x - mu) / sigma   ~   N(0,1)
     返回副本
    """
    mu = np.mean(x, axis=0)
    sigma = np.std(x, axis=0)
    return (x - mu) / sigma

YUAN = np.random.randint(3, 70, size=5)
a = standardization(YUAN)
b = preprocessing.scale(YUAN)

print(YUAN, "normalization:", a)
print(YUAN, "preprocessing.scale:", b)

print(a == b)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

结果

[ 8 14 64 51 61] normalization: [-1.32656065 -1.07468204  1.02430632  0.47856935  0.89836702]
[ 8 14 64 51 61] preprocessing.scale: [-1.32656065 -1.07468204  1.02430632  0.47856935  0.89836702]
[ True  True  True  True  True]
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/416368
推荐阅读