赞
踩
代码
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)
结果
[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
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)
结果
[ 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]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。