numpy linspace和numpy logspace之间的输出差异 [英] Difference in output between numpy linspace and numpy logspace
问题描述
Numpy linspace在指定的间隔内返回均匀间隔的数字. Numpy日志空间返回数字在对数刻度上均匀分布.
我不明白为什么numpy日志空间经常返回我设置的边界超出范围"的值.取介于0.02
和2.0
之间的数字:
import numpy as np
print np.linspace(0.02, 2.0, num=20)
print np.logspace(0.02, 2.0, num=20)
第一个输出为:
[ 0.02 0.12421053 0.22842105 0.33263158 0.43684211 0.54105263
0.64526316 0.74947368 0.85368421 0.95789474 1.06210526 1.16631579
1.27052632 1.37473684 1.47894737 1.58315789 1.68736842 1.79157895
1.89578947 2. ]
这看起来是正确的.但是,np.logspace()
的输出是错误的:
[ 1.04712855 1.33109952 1.69208062 2.15095626 2.73427446
3.47578281 4.41838095 5.61660244 7.13976982 9.07600522
11.53732863 14.66613875 18.64345144 23.69937223 30.12640904
38.29639507 48.68200101 61.88408121 78.6664358 100. ]
为什么将1.047
输出到100.0
?
2017更新:numpy 1.12包含一个函数,该函数完全执行原始问题的要求,即返回在日志空间中均匀采样的两个值之间的范围.>
该函数为 numpy.geomspace
>>> np.geomspace(0.02, 2.0, 20)
array([ 0.02 , 0.0254855 , 0.03247553, 0.04138276, 0.05273302,
0.06719637, 0.08562665, 0.1091119 , 0.13903856, 0.17717336,
0.22576758, 0.28768998, 0.36659614, 0.46714429, 0.59527029,
0.75853804, 0.96658605, 1.23169642, 1.56951994, 2. ])
Numpy linspace returns evenly spaced numbers over a specified interval. Numpy logspace return numbers spaced evenly on a log scale.
I don't understand why numpy logspace often returns values "out of range" from the bounds I set. Take numbers between 0.02
and 2.0
:
import numpy as np
print np.linspace(0.02, 2.0, num=20)
print np.logspace(0.02, 2.0, num=20)
The output for the first is:
[ 0.02 0.12421053 0.22842105 0.33263158 0.43684211 0.54105263
0.64526316 0.74947368 0.85368421 0.95789474 1.06210526 1.16631579
1.27052632 1.37473684 1.47894737 1.58315789 1.68736842 1.79157895
1.89578947 2. ]
That looks correct. However, the output for np.logspace()
is wrong:
[ 1.04712855 1.33109952 1.69208062 2.15095626 2.73427446
3.47578281 4.41838095 5.61660244 7.13976982 9.07600522
11.53732863 14.66613875 18.64345144 23.69937223 30.12640904
38.29639507 48.68200101 61.88408121 78.6664358 100. ]
Why does it output 1.047
to 100.0
?
2017 update: The numpy 1.12 includes a function that does exactly what the original question asked, i.e. returns a range between two values evenly sampled in log space.
The function is numpy.geomspace
>>> np.geomspace(0.02, 2.0, 20)
array([ 0.02 , 0.0254855 , 0.03247553, 0.04138276, 0.05273302,
0.06719637, 0.08562665, 0.1091119 , 0.13903856, 0.17717336,
0.22576758, 0.28768998, 0.36659614, 0.46714429, 0.59527029,
0.75853804, 0.96658605, 1.23169642, 1.56951994, 2. ])
这篇关于numpy linspace和numpy logspace之间的输出差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!