2024-09-01
Python
00

代码

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]
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:Dong

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!