Python 减少数据噪声

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37598986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:37:21  来源:igfitidea点击:

Reducing noise on Data

pythonnoisekalman-filter

提问by PiccolMan

I have 2 lists with data points in them.

我有 2 个列表,其中包含数据点。

x = ["bunch of data points"]
y = ["bunch of data points"]

I've generated a graph using matplotlib in python

我在 python 中使用 matplotlib 生成了一个图形

import matplotlib.pyplot as plt

plt.plot(x, y, linewidth=2, linestyle="-", c="b")
plt.show()
plt.close()

Would I be able to reduce the noise on the data? Would a Kalman filter work here?

我能减少数据上的噪音吗?卡尔曼滤波器可以在这里工作吗?

enter image description here

在此处输入图片说明

回答by Lyken Syu

It depends how you define the "noise" and how it is caused. Since you didn't provide much information about your case, I'll take your question as "how to make the curve smooth". Kalman filter can do this, but it's too complex, I'd prefer simple IIR filter

这取决于您如何定义“噪音”以及它是如何引起的。由于您没有提供有关您的案例的太多信息,因此我将您的问题视为“如何使曲线平滑”。卡尔曼滤波器可以做到这一点,但它太复杂了,我更喜欢简单的 IIR 滤波器

import matplotlib.pyplot as plt

mu, sigma = 0, 500

x = np.arange(1, 100, 0.1)  # x axis
z = np.random.normal(mu, sigma, len(x))  # noise
y = x ** 2 + z # data
plt.plot(x, y, linewidth=2, linestyle="-", c="b")  # it include some noise

enter image description here

在此处输入图片说明

After filter

过滤后

from scipy.signal import lfilter

n = 15  # the larger n is, the smoother curve will be
b = [1.0 / n] * n
a = 1
yy = lfilter(b,a,y)
plt.plot(x, yy, linewidth=2, linestyle="-", c="b")  # smooth by filter

enter image description here

在此处输入图片说明

lfilteris a function from scipy.signal.

lfilter是来自scipy.signal的函数。

By the way, if you do want to use Kalman filter for smoothing, scipy also provides an example. Kalman filter should also work on this case, just not so necessary.

顺便说一句,如果您确实想使用卡尔曼滤波器进行平滑,scipy 还提供了一个示例。卡尔曼滤波器也应该适用于这种情况,只是没有那么必要。

回答by U3.1415926

Depending on how much you like to remove the noise, you can also use the Savitzky-Golay filter from scipy.

根据您想要去除噪声的程度,您还可以使用 Savitzky-Golay 过滤器scipy

The following takes the example from @lyken-syu:

以下以@lyken-syu 为例:

import matplotlib.pyplot as plt
import numpy as np
mu, sigma = 0, 500
x = np.arange(1, 100, 0.1)  # x axis
z = np.random.normal(mu, sigma, len(x))  # noise
y = x ** 2 + z # data
plt.plot(x, y, linewidth=2, linestyle="-", c="b")  # it include some noise

enter image description here

在此处输入图片说明

and applies the Savitzky-Golay filter

并应用 Savitzky-Golay 过滤器

from scipy.signal import savgol_filter
w = savgol_filter(y, 101, 2)
plt.plot(x, w, 'b')  # high frequency noise removed

window_length = 101

窗口长度 = 101

Increasing the window_lengthto 501:

增加window_length至501:

window_length = 501

窗口长度 = 501

Read more about the filter here

此处阅读有关过滤器的更多信息