请注意,我正在询问 numpy.linspace 的端点。这些函数保证返回一个包含端点的数组。但是端点是否保证是 float 与提供给函数的参数相同?
询问原因 - 纯粹的好奇心。除非有人能想出一个充分的理由来依赖这种行为? 谢谢。
最佳答案
这是 definition of np.linspace 的摘录:
def linspace(start, stop, num=50, endpoint=True, retstep=False):
...
if endpoint:
if num == 1:
return array([float(start)])
step = (stop-start)/float((num-1))
y = _nx.arange(0, num) * step + start #<-- the first point is `start`
y[-1] = stop # <-- the last point is `stop`
...
return y
所以,是的,当 endpoints
为 True 时,返回的端点将完全等于 start
和 stop
。
请注意,即使 endpoints=True
(默认情况下),如果 num
是,np.linspace
也可能不会返回端点小于2:
In [8]: np.linspace(0, 1, num=0)
Out[8]: array([], dtype=float64)
In [9]: np.linspace(0, 1, num=1)
Out[9]: array([ 0.])
关于python - 对于浮点 x,x==numpy.linspace(x,y,n)[0] 总是 True 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19821848/