技术文章和资源

技术文章(时间排序)

热门类别

Python PHP MySQL JDBC Linux

什么是机器学习中的标准化

machine learningpythonnumpy

数据集是任何 ML 模型的核心。数据集中的数据必须经过缩放并且处于特定范围内,才能提供准确的结果,这一点至关重要。

机器学习中的标准化是一种特征缩放,用于使数据集统一,从而产生具有相同尺度和范围的独立变量和特征。标准化将标准差转换为 1,将平均值转换为 0。在标准化中,从每个数据点中减去平均值,然后将得到的结果除以标准差,从而得到标准化和重新缩放的数据。

该技术用于机器学习模型,例如主成分分析、支持向量机和 k 均值聚类,因为它们依赖于欧几里得距离。

数学表示如下

Z = (X - m ) / s 

where

X   a data point

 the mean

 the standard deviation

 the standardized value

Algorithm

Step 1 − Import the libraries required. Some of the commonly imported libraries to standardize an ML model are numpy, pandas or scikit-learn .

Step 2  Import the StandardScaler() function from the preprocessor.

Step 3  Upload the data set that you want to standardize.

Step 4  Divide the data into training data and testing data : X_test, y_test, X_train and y_train .

Step 5  Fit the data into the StandardScaler() function to standardize .

示例

在此示例中,我们将通过采用随机数据值来检查标准化。让我们将以下一组值视为数据点 

3 5 7 8 9 4
平均值 m= 36/6 = 6
标准差 s = 2.36
Z1= - 1.27
Z2= - 0.42
Z3= - 0.42
Z4= 0.84
Z5= 1.27
Z6= -0.84
现在,平均值为 (Z1 + Z2 + Z3 + Z4 + Z5)/5
= (- 1.27 - 0.42 + 0.42 + 0.84 + 1.27 - 0.84)/5
= 0

并且标准差为 1

因此,标准化后,值在同一范围内,平均值为 0,标准差为 1。

示例

from sklearn.preprocessing import StandardScaler
import numpy as np

# 创建样本数据矩阵
X = np.array([[85,72,80], [64, 35, 26], [67, 48, 29], [100, 11, 102], [130, 14, 151]])

# 创建 StandardScaler 实例
standard_scaler = StandardScaler()

# 将缩放器拟合到数据
standard_scaler.fit(X)

# 使用缩放器转换数据
X_new= standard_scaler.transform(X)

# 打印转换后的数据
print(" new data:", X_new)

输出

new data: [[-0.17359522  1.59410679  0.0511375 ]
 [-1.04157134 -0.04428074 -1.09945622]
 [-0.91757475  0.53136893 -1.03553435]
 [ 0.44638772 -1.10701861  0.5198979 ]
 [ 1.68635359 -0.97417637  1.56395517]]

在此程序中,变量 X 以数字数组的形式包含特征。它适合 StandardScaler() 函数,并显示标准化数组。

结论

标准化是通过操纵数据获得无错误结果的好方法。数据集具有各种变量,其值可能超出范围。使用标准化和规范化可以解决此问题,这两者都属于特征缩放。特征缩放的目的是确保在使用机器学习模型预测输出时所有特征都具有同等重要性。


相关文章