python矩阵归一化方法_python之sklearn常见数据预处理归一化方式解析

本文详细介绍了Python中使用sklearn库进行数据预处理的归一化方法,包括标准归一化(使数据均值为0,方差为1)、最大最小归一化和最大绝对值归一化。通过示例代码展示了如何实现这些方法,并对可能出现的数值问题进行了讨论。
摘要由CSDN通过智能技术生成

标签:

标准归一化

归一化到均值为0,方差为1

sklearn.preprocessing.scale函数:Standardize a dataset along any axis

先贴出主要的源码,乍一看,很乱,其实细看之下,就是多了一些判断稀疏矩阵之类的条件性代码。

#coding=utf-8

import numpy as np

from scipy import sparse

def _handle_zeros_in_scale(scale, copy=True):

''' Makes sure that whenever scale is zero, we handle it correctly.

This happens in most scalers when we have constant features.'''

# if we are fitting on 1D arrays, scale might be a scalar

if np.isscalar(scale):

if scale == .0:

scale = 1.

return scale

elif isinstance(scale, np.ndarray):

if copy:

# New array to avoid side-effects

scale = scale.copy()

scale[scale == 0.0] = 1.0

return scale

def scale(X, axis=0, with_mean=True, with_std=True, copy=True):

"""Standardize a dataset along any axis

Center to the mean and component wise scale to unit variance.

Read more in the :ref:`User Guide `.

Parameters

----------

X : {array-like, sparse matrix}

The data to center and scale.

axis : int (0 by default)

axis used to compute the means and standard deviations along. If 0,

independently standardize each feature, otherwise (if 1) standardize

each sample.

with_mean : boolean, Tru

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值