数据挖掘-数据标准化 python实现

本文介绍了Python中数据标准化的几种常见方法,包括Min_Max、Z_Score、DecimalScaling、均值标准化、向量标准化、指数转换法(log10、SoftMax、Sigmoid)。通过实例展示了每种方法的实现过程及结果,帮助读者理解如何对数据进行预处理和标准化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据挖掘-数据标准化 python实现

  • 数据标准化
    数据是
    [1, 2, 3, 4, 5, 6, 7, 8, 9]
# -*-coding:utf-8-*-

"""
    Author: Thinkgamer
    Desc:
        代码4-1 Python实现标准化方法
"""
import numpy as np
import math

class DataNorm:
    def __init__(self):
        self.arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        self.x_max = max(self.arr)  # 最大值
        self.x_min = min(self.arr)  # 最小值
        self.x_mean = sum(self.arr) / len(self.arr)  # 平均值
        self.x_std = np.std(self.arr)  # 标准差

    def Min_Max(self):
        arr_ = list()
        for x in self.arr:
            # round(x,4) 对x保留4位小数
            arr_.append(round((x - self.x_min) / (self.x_max - self.x_min), 4))
        print("经过Min_Max标准化后的数据为:\n{}".format(arr_))

    def Z_Score(self):
        arr_ = list()
        for x in self.arr:
            arr_.append(round((x - self.x_mean) / self.x_std, 4))
        print("经过Z_Score标准化后的数据为:\n{}".format(arr_))

    # 有点问题,改为如下这样
    # def DecimalScaling(self):
    #     arr_ = list()
    #     j = self.x_max // 10 if self.x_max % 10 == 0 else self.x_max // 10 + 1
    #     for x in self.arr:
    #         arr_.append(round(x / math.pow(10, j), 4))
    #     print("经过Decimal Scaling标准化后的数据为:\n{}".format(arr_))
    def DecimalScaling(self):
        arr_ = list()
        j = 1
        x_max = max([abs(one) for one in self.arr])
        while x_max / 10 >= 1.0:
            j += 1
            x_max = x_max / 10
        for x in self.arr:
            arr_.append(round(x / math.pow(10, j), 4))
        print("经过Decimal Scaling标准化后的数据为:\n{}".format(arr_))

    def Mean(self):
        arr_ = list()
        for x in self.arr:
            arr_.append(round((x - self.x_mean) / (self.x_max - self.x_min), 4))
        print("经过均值标准化后的数据为:\n{}".format(arr_))

    def Vector(self):
        arr_ = list()
        for x in self.arr:
            arr_.append(round(x / sum(self.arr), 4))
        print("经过向量标准化后的数据为:\n{}".format(arr_))

    def exponential(self):
        arr_1 = list()
        for x in self.arr:
            arr_1.append(round(math.log10(x) / math.log10(self.x_max), 4))
        print("经过指数转换法(log10)标准化后的数据为;\n{}".format(arr_1))

        arr_2 = list()
        sum_e = sum([math.exp(one) for one in self.arr])
        for x in self.arr:
            arr_2.append(round(math.exp(x) / sum_e, 4))
        print("经过指数转换法(SoftMax)标准化后的数据为;\n{}".format(arr_2))

        arr_3 = list()
        for x in self.arr:
            arr_3.append(round(1 / (1 + math.exp(-x)), 4))
        print("经过指数转换法(Sigmoid)标准化后的数据为;\n{}".format(arr_3))

if __name__ == "__main__":
    dn = DataNorm()
    dn.Min_Max()
    dn.Z_Score()
    dn.DecimalScaling()
    dn.Mean()
    dn.Vector()
    dn.exponential()

  • 结果
经过Min_Max标准化后的数据为:
[0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]
经过Z_Score标准化后的数据为:
[-1.5492, -1.1619, -0.7746, -0.3873, 0.0, 0.3873, 0.7746, 1.1619, 1.5492]
经过Decimal Scaling标准化后的数据为:
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
经过均值标准化后的数据为:
[-0.5, -0.375, -0.25, -0.125, 0.0, 0.125, 0.25, 0.375, 0.5]
经过向量标准化后的数据为:
[0.0222, 0.0444, 0.0667, 0.0889, 0.1111, 0.1333, 0.1556, 0.1778, 0.2]
经过指数转换法(log10)标准化后的数据为;
[0.0, 0.3155, 0.5, 0.6309, 0.7325, 0.8155, 0.8856, 0.9464, 1.0]
经过指数转换法(SoftMax)标准化后的数据为;
[0.0002, 0.0006, 0.0016, 0.0043, 0.0116, 0.0315, 0.0856, 0.2326, 0.6322]
经过指数转换法(Sigmoid)标准化后的数据为;
[0.7311, 0.8808, 0.9526, 0.982, 0.9933, 0.9975, 0.9991, 0.9997, 0.9999]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大桃子技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值