关于sklearn中StandardScaler的使用方式

文章目录

  • 背景
  • 标准化过程
  • 例子
  • [fit_transform和fit + transform的区别和使用场景](#fit_transform和fit + transform的区别和使用场景)

背景

在机器学习中经常会使用StandardScaler 进行数据归一化,注意一旦调整好StandardScaler以后就保存下来,后面如果进行测试单个时,可以进行加载并对其进行标准化

StandardScaler 是一种常用的数据标准化 方法,用于将数据转换为均值为 0 ,标准差为 1 的标准正态分布。

标准化过程

  1. 计算原始数据的均值 mean 和标准差 std。

  2. 对原始数据进行标准化处理,即对每个数据点减去均值 ,再除以标准差

  3. 在 StandardScaler 中,均值和标准差的计算分别基于训练数据的样本 ,可以使用 fit 方法计算均值和标准差。然后,使用 transform 方法将数据标准化为标准正态分布。

PS: StandardScaler 在计算过程中用的是 "总体标准差",而不是"样本标准差"

总体标准差:
σ = ∑ i = 1 n ( x i − x ˉ ) 2 n \sigma = \sqrt{\frac{\sum_{i = 1}^{n}(x_{i} - \textcolor{myblue}{\bar{x}})^{2}}{n}} σ=n∑i=1n(xi−xˉ)2

x x x 为某个样本, x ˉ \textcolor{myblue}{\bar{x}} xˉ 为总体样本的均值, n n n 为总体样本的数量。

样本标准差:

S = ∑ i = 1 n ( x i − x ˉ ) 2 n − 1 S = \sqrt{\frac{\sum_{i = 1}^{n}(x_{i} - \textcolor{myblue}{\bar{x}})^{2}}{n - 1}} S=n−1∑i=1n(xi−xˉ)2

x x x 为某个抽样样本, x ˉ \textcolor{myblue}{\bar{x}} xˉ 为抽样样本的均值, n n n 为抽样样本的数量。

例子

python 复制代码
from sklearn.preprocessing import StandardScaler
import numpy as np
import pickle
import os

# 创建一个 3 x 2 的矩阵
X = np.array([[1, 2], [3, 4], [5, 6]])

# 创建 StandardScaler 对象
scaler = StandardScaler()

# 计算均值和标准差
scaler.fit(X)

# 将数据标准化为标准正态分布
X_scaled = scaler.transform(X)

# 打印结果
print("原始数据:\n", X)
print("均值:", scaler.mean_)
print("标准差:", scaler.scale_)
print("标准化后的数据:\n", X_scaled)

scaler_filename = 'standard_scaler.pkl'
with open(scaler_filename, 'wb') as file:
    pickle.dump(scaler, file)
print(f"\nStandardScaler 对象已保存到文件: {scaler_filename}")

输出结果:

python 复制代码
原始数据:
 [[1 2]
  [3 4]
  [5 6]]
均值: [3. 4.]
标准差: [1.63299316 1.63299316]
标准化后的数据:
 [[-1.22474487 -1.22474487]
  [ 0.          0.        ]
  [ 1.22474487  1.22474487]]
StandardScaler 对象已保存到文件: standard_scaler.pkl

在上面的代码中,我们首先创建了一个 3 x 2 的矩阵 X,然后创建了一个 StandardScaler 对象 scaler。通过调用 fit 方法计算了均值和标准差,并通过调用 transform 方法将原始数据标准化为标准正态分布。最后,我们打印了原始数据、均值、标准差和标准化后的数据。

使用 Python 内置的 pickle 模块保存 StandardScaler 对象

fit_transform和fit + transform的区别和使用场景

要注意fit_transform 和 fit + transform 的区别在于

  • fit_transform 是一个组合方法,它将拟合数据和转换数据这两个步骤合并到一个方法中。它首先拟合数据,然后应用相同的拟合参数转换数据。在使用 fit_transform 方法时,你只需在训练集上调用一次,就可以同时进行拟合和转换,这样能够节省时间和代码。
  • fit 和 transform 是单独的方法。fit 方法用于从数据中学习模型参数,例如,对于 StandardScaler,它计算并保存数据集的均值和标准差 。transform 方法用于使用已学习的模型参数来转换数据。通常,你会在训练集上调用 fit 方法来拟合模型 ,并在训练集和测试集上分别调用 transform 方法来转换数据。

在使用机器学习算法时,通常将数据集分成训练集和测试集。通常情况下,我们只使用训练集拟合模型并保存模型,然后使用已保存的模型在测试集上转换数据。因此,在训练集 上使用 fit_transform 方法,而在测试集 上使用 transform 方法。

python 复制代码
from sklearn.preprocessing import StandardScaler
import numpy as np

# 创建一个 3 x 2 的矩阵
X = np.array([[1, 2], [3, 4], [5, 6]])

# 使用 fit_transform 方法标准化数据
scaler = StandardScaler()
X_scaled1 = scaler.fit_transform(X)

# 使用 fit 和 transform 方法标准化数据
scaler = StandardScaler()
scaler.fit(X)
X_scaled2 = scaler.transform(X)

# 打印结果
print("使用 fit_transform 方法标准化的结果:\n", X_scaled1)
print("使用 fit 和 transform 方法标准化的结果:\n", X_scaled2)

输出结果:

python 复制代码
使用 fit_transform 方法标准化的结果:
 [[-1.22474487 -1.22474487]
  [ 0.          0.        ]
  [ 1.22474487  1.22474487]]
使用 fit 和 transform 方法标准化的结果:
 [[-1.22474487 -1.22474487]
  [ 0.          0.        ]
  [ 1.22474487  1.22474487]]

参考: https://www.cnblogs.com/chentiao/p/17203643.html

相关推荐
Dmz丶16 分钟前
前端转 AI 100 天 Day 44:前端集成实战——用 React 给 Agent 做个正式界面
python
数据技术说20 分钟前
在 KES AI 数据库上搭一条 NL2SQL 链路:语义、召回、权限各放哪
人工智能·sql·代码规范
jay神25 分钟前
基于 YOLOv8 的手势识别系统(Web 版)
前端·人工智能·python·深度学习·yolo·计算机视觉·计算机毕业设计
奶油话梅糖33 分钟前
用 iPhone 扫一间房,AI 帮你在浏览器里 1:1 重建它:Wes Bos 的 scan-to-threejs 工作流
人工智能·ios·iphone
DeepVisionary34 分钟前
阿里云上线旗舰多模态新模型,不公布跑分且限时14天退役
python·自动化
东莞市云毅网络有限公司35 分钟前
FAQ 覆盖度打分:TF-IDF 与向量嵌入双路召回的 Python 实现
python·数据清洗·rag·企业知识库·文档解析
爱笑的k111 小时前
nano-vllm 源码剖析:千行 Python 手写离线推理引擎的调度、显存与并行时序
开发语言·python·vllm
锋行天下1 小时前
LangGraph HITL 实战:从 interrupt 到 useStream 的最小闭环
人工智能
kyriewen1 小时前
AI 能把页面写出来,为什么改个需求就开始崩?
前端·javascript·人工智能
用户8356290780512 小时前
使用 Python 控制 Excel 工作表的分页位置
后端·python