学无先后,达者为师

网站首页 编程语言 正文

利用python3如何给数据添加高斯噪声_python

作者:WaiSaa   更新时间: 2022-05-19 编程语言

Background

高斯噪声,顾名思义是指服从高斯分布(正态分布)的一类噪声。有的时候我们需要向标准数据中加入合适的高斯噪声让数据更加符合实际。

python中的random库中集成了高斯正态分布,可以直接使用。

我们可以通过调整高斯噪声均值和方差,获取不同效果的处理数据。

原始数据

高斯噪声sigma = 0.05

高斯噪声sigma = 0.1

高斯噪声sigma = 0.15

源码

import random

import numpy as np
from matplotlib import pyplot as plt

def gauss_noisy(x, y):
    """
    对输入数据加入高斯噪声
    :param x: x轴数据
    :param y: y轴数据
    :return:
    """
    mu = 0
    sigma = 0.05
    for i in range(len(x)):
        x[i] += random.gauss(mu, sigma)
        y[i] += random.gauss(mu, sigma)

if __name__ == '__main__':
    # 在0-5的区间上生成50个点作为测试数据
    xl = np.linspace(0, 5, 50, endpoint=True)
    yl = np.sin(xl)

    # 加入高斯噪声
    gauss_noisy(xl, yl)

    # 画出这些点
    plt.plot(xl, yl, linestyle='', marker='.')
    plt.show()

总结

原文链接:https://blog.csdn.net/qq_42761569/article/details/120952481

栏目分类
最近更新