Dil*_*rix 6 python random numpy
我在某人的代码中看到了这种模式:
import numpy as np
# Create array
xx = np.linspace(0.0, 100.0, num=100)
# Add Noise
xx = np.random.normal(xx)
Run Code Online (Sandbox Code Playgroud)
它似乎为数组的每个值添加了一些噪音,但我找不到任何相关的文档.发生了什么?是什么决定了噪音的属性(即缩放)?给定值是否被视为分布loc中每个采样的平均值(即参数)normal?
我也很想知道为什么文档中似乎没有涵盖这种行为.
我也没有看到它记录下来,但是许多采用ndarray的numpy函数将在元素上运行.无论如何,你可以很容易地验证,当它传递一个数组时,它numpy.random.normal使用该元素的值作为均值来调用数组中的每个元素并返回一个数组:
In [9]: xx = numpy.array([1, 10, 100, 1000])
In [10]: numpy.random.normal(xx)
Out[10]:
array([ 9.45865328e-01, 1.11542264e+01, 9.88601302e+01,
1.00120448e+03])
Run Code Online (Sandbox Code Playgroud)
似乎它使用默认值1.0作为比例.你可以覆盖这个:
In [12]: numpy.random.normal(xx, 10)
Out[12]: array([ 8.92500743, -5.66508088, 97.33440273, 1003.37940455])
In [13]: numpy.random.normal(xx, 100)
Out[13]: array([ -75.13092966, -47.0841671 , 154.12913986, 816.3126146 ])
Run Code Online (Sandbox Code Playgroud)