在 Python 中定义白噪声过程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32237769/
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
Defining a white noise process in Python
提问by dbliss
I need to draw samples from a white noise process in order to implement a particular integral numerically.
我需要从白噪声过程中抽取样本,以便在数值上实现特定的积分。
How do I generate this with Python (i.e., numpy, scipy, etc.)?
如何使用 Python(即 numpy、scipy 等)生成它?
采纳答案by Sam
You can achieve this through the numpy.random.normal
function, which draws a given number of samples from a Gaussian distribution.
您可以通过numpy.random.normal
函数实现这一点,该函数从高斯分布中抽取给定数量的样本。
import numpy
import matplotlib.pyplot as plt
mean = 0
std = 1
num_samples = 1000
samples = numpy.random.normal(mean, std, size=num_samples)
plt.plot(samples)
plt.show()
回答by user8866568
Short answer is numpy.random.random()
. Numpy site description
简短的回答是numpy.random.random()
。Numpy 站点描述
But since I find more and more answers to similar questions written as numpy.random.normal
, I suspect a little description is needed. If I do understand Wikipedia (and a few lessons at the University) correctly, Gauss and White Noise are two separate things. White noise has Uniform distribution, not Normal (Gaussian).
但由于我发现越来越多的类似问题的答案写成numpy.random.normal
,我怀疑需要一些描述。如果我确实正确理解了维基百科(以及大学的一些课程),那么高斯和白噪声是两个独立的事物。白噪声具有均匀分布,而不是正态(高斯)。
import numpy.random as nprnd
import matplotlib.pyplot as plt
num_samples = 10000
num_bins = 200
samples = numpy.random.random(size=num_samples)
plt.hist(samples, num_bins)
plt.show()
This is my first answer, so if you correct mistakes possibly made by me here, I'll gladly update it. Thanks =)
这是我的第一个答案,所以如果您纠正我在这里可能犯的错误,我会很乐意更新。谢谢=)
回答by Ryan Yarahmadian
Create random samples with normal distribution (Gaussian) with numpy.random.normal
:
创建具有正态分布(高斯)的随机样本numpy.random.normal
:
import numpy as np
import seaborn as sns
mu, sigma = 0, 1 # mean and standard deviation
s = np.random.normal(mu, sigma, size=1000) # 1000 samples with normal distribution
# seaborn histogram with Kernel Density Estimation
sns.distplot(s, bins=40, hist_kws={'edgecolor':'black'})