什么是机器学习中的标准化
数据集是任何机器学习模型的核心。数据集中的数据被缩放并处于特定范围内非常重要,以提供准确的结果。
机器学习中的标准化是一种特征缩放的类型,用于使数据集具有统一性,从而使独立变量和特征具有相同的尺度和范围。标准化将标准差变为1,均值变为0。在标准化中,从每个数据点中减去均值,然后将结果除以标准差,从而得到标准化和重新缩放的数据。
这种技术在机器学习模型中被广泛应用,如主成分分析、支持向量机和k-means聚类,因为它们依赖于欧氏距离。
数学表示如下:
Z = (X - m ) / s
其中
X −一个数据点
m −均值
s −标准差
Z −标准化值
步骤
步骤1 −导入所需的库。标准化ML模型常常导入的库包括numpy,pandas或scikit-learn。
步骤2 −从预处理器中导入StandardScaler()函数。
步骤3 −上传要进行标准化的数据集。
步骤4 −将数据分为训练数据和测试数据:X_test,y_test,X_train和y_train。
步骤5 −将数据拟合到StandardScaler()函数中进行标准化。
示例
在这个例子中,我们将通过取随机数据值来进行标准化。让我们将以下一组值视为数据点−
3 5 7 8 9 4
The mean m= 36/6 = 6
The standard deviation s = 2.36
Z1= - 1.27
Z2= - 0.42
Z3= - 0.42
Z4= 0.84
Z5= 1.27
Z6= -0.84
Now, the mean is (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
# Create a sample data matrix
X = np.array([[85,72,80], [64, 35, 26], [67, 48, 29], [100, 11, 102], [130, 14, 151]])
# create an instance of StandardScaler
standard_scaler = StandardScaler()
# Fit the scaler to the data
standard_scaler.fit(X)
# Transform the data using the scaler
X_new= standard_scaler.transform(X)
# Print the transformed data
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()函数中并显示标准化的数组。
结论
标准化是通过操作数据来获得无误差结果的一种有效方法。数据集中的各种变量的值可能超出范围。通过标准化和归一化来解决这个问题,这两种方法都属于特征缩放。特征缩放的目的是确保在使用机器学习模型预测输出时,所有特征都被赋予相等的重要性。