使用StandardScaler()函数标准化Python数据

时间:2020-02-23 14:43:45  来源:igfitidea点击:

在本文中,我们将重点介绍Python中最重要的预处理技术之一-使用StandardScaler()函数进行标准化。

需要标准化

在进入标准化之前,让我们首先了解缩放的概念。

特征缩放是使用数据集对算法建模的重要步骤。
通常用于建模目的的数据是通过各种方式得出的,例如:

  • 问卷调查
  • 调查
  • 研究
  • 采集等

因此,获得的数据总共包含各种维度和尺度的特征。
数据特征的不同比例会不利地影响数据集的建模。

就误分类误差和准确率而言,这导致预测结果有偏差。
因此,有必要在建模之前缩放数据。

这是标准化成为现实的时候。

标准化是一种缩放技术,其中它通过将数据的统计分布转换为以下格式来使数据无缩放:

  • 均值– 0(零)
  • 标准偏差– 1

标准化

这样,整个数据集的均值和单位方差均为零。

现在,让我们尝试在接下来的部分中实现标准化的概念。

Python sklearn StandardScaler()函数

Python sklearn库为我们提供了StandardScaler()函数,以将数据值标准化为标准格式。

语法:

object = StandardScaler()
object.fit_transform(data)

根据上述语法,我们首先创建了StandardScaler()函数的对象。
此外,我们将fit_transform()与分配的对象一起使用来转换数据并使之标准化。

注意:标准化仅适用于正态分布之后的数据值。

使用StandardScaler()函数标准化数据

看下面的例子!

from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
 
dataset = load_iris()
object= StandardScaler()
 
# Splitting the independent and dependent variables
i_data = dataset.data
response = dataset.target
 
# standardization 
scale = object.fit_transform(i_data) 
print(scale)

说明:

  • 导入所需的必要库。
    我们已经导入了sklearn库以使用StandardScaler函数。

  • 加载数据集。
    其中我们使用了sklearn.datasets库中的IRIS数据集。
    您可以在此处找到数据集。

  • 将对象设置为StandardScaler()函数。

  • 分离独立变量和目标变量,如上所示。

  • 使用fit_transform()函数将函数应用于数据集。