欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python3入门机器学习经典算法与应用——knn算法数据归一化 Feature Scaling

程序员文章站 2022-03-28 16:13:35
数据归一化 Feature Scaling最值归一化 normalization把所有数据映射到0-1之间适⽤用于分布有明显边界的情况;受outlier影响较⼤大均值方差归一化 standardization均值方差归一化:把所有数据归⼀一到均值为0⽅方差为1的分布中对测试数据集如何归一化测试数据是模拟真实环境• 真实环境很有可能⽆无法得到所有测试数据的均值和⽅方差• 对数据的归⼀一化也是算法的⼀一部分(x_test - mean_train) / std_train模型...

数据归一化 Feature Scaling

最值归一化 normalization

把所有数据映射到0-1之间
Python3入门机器学习经典算法与应用——knn算法数据归一化 Feature Scaling

适⽤用于分布有明显边界的情况;受outlier影响较⼤大

均值方差归一化 standardization

均值方差归一化:把所有数据归⼀一到均值为0⽅方差为1的分布中
Python3入门机器学习经典算法与应用——knn算法数据归一化 Feature Scaling

对测试数据集如何归一化

测试数据是模拟真实环境
• 真实环境很有可能⽆无法得到所有测试
数据的均值和⽅方差
• 对数据的归⼀一化也是算法的⼀一部分

(x_test - mean_train) / std_train

Python3入门机器学习经典算法与应用——knn算法数据归一化 Feature Scaling
模型

Python3入门机器学习经典算法与应用——knn算法数据归一化 Feature Scaling

手写StandardScaler

import numpy as np


class StandardScaler:

    def __init__(self):
        self.mean_ = None
        self.scale_ = None

    def fit(self, X):
        """根据训练数据集X获得数据的均值和方差"""
        assert X.ndim == 2, "The dimension of X must be 2"

        self.mean_ = np.array([np.mean(X[:,i]) for i in range(X.shape[1])])
        self.scale_ = np.array([np.std(X[:,i]) for i in range(X.shape[1])])

        return self

    def transform(self, X):
        """将X根据这个StandardScaler进行均值方差归一化处理"""
        assert X.ndim == 2, "The dimension of X must be 2"
        assert self.mean_ is not None and self.scale_ is not None, \
               "must fit before transform!"
        assert X.shape[1] == len(self.mean_), \
               "the feature number of X must be equal to mean_ and std_"

        resX = np.empty(shape=X.shape, dtype=float)
        for col in range(X.shape[1]):
            resX[:,col] = (X[:,col] - self.mean_[col]) / self.scale_[col]
        return resX

本文地址:https://blog.csdn.net/e891377/article/details/109646918