zoukankan      html  css  js  c++  java
  • preprocessing

    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
  • 相关阅读:
    Linux(debian)安装Redis教程
    Linux下Nginx学习笔记
    Linux常用命令
    MySQL代码片段
    MySql学习笔记
    漂亮的Web颜色
    Electron开发问题总结
    Git 教程
    Python基础
    【原创】servlet+spring+mybatis配置总结
  • 原文地址:https://www.cnblogs.com/heguoxiu/p/10135577.html
Copyright © 2011-2022 走看看