本文主要是介绍python Z-score标准化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
python Z-score标准化
- Zscore标准化
- sklearn库实现Z-score标准化
- 手动实现Z-score标准化
Zscore标准化
Z-score标准化(也称为标准差标准化)是一种常见的数据标准化方法,它将数据集中的每个特征的值转换为一个新的尺度,使得转化后的数据具有均值为0,标准差为1的分布。Z-score标准化公式如下:
其中:
- X 是原始数据值
- μ 是数据的均值
- σ 是数据的标准差
sklearn库实现Z-score标准化
import numpy as np
from sklearn.preprocessing import StandardScaler# 生成示例数据
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])# 创建StandardScaler对象
scaler = StandardScaler()# 对数据进行标准化
data_standardized = scaler.fit_transform(data)print("原始数据:")
print(data)
print("\n标准化后的数据:")
print(data_standardized)
print("\n均值:", scaler.mean_)
print("标准差:", scaler.scale_)
手动实现Z-score标准化
import numpy as np# 生成示例数据
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])# 计算均值和标准差
mean = np.mean(data, axis=0)
std = np.std(data, axis=0)# 进行Z-score标准化
data_standardized = (data - mean) / stdprint("原始数据:")
print(data)
print("\n标准化后的数据:")
print(data_standardized)
这篇关于python Z-score标准化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!