start
和
stop
num
类似于谁可能会忘记
In [1]: import numpy as np
In [2]: np.linspace(0, 10, 9)
Out[2]: array([ 0. , 1.25, 2.5 , 3.75, 5. , 6.25, 7.5 , 8.75, 10. ])
,它会产生一个给定的阵列
np.arange
,start
和stop
::
step
但是,是否有一个函数可以在省略
In [4]: np.arange(0, 10, 1.25)
Out[4]: array([ 0. , 1.25, 2.5 , 3.75, 5. , 6.25, 7.5 , 8.75])
时指定一个元素的start
,
step
和
num
? 应该有
(从我的角度来看)最短,最优雅的方式是:
stop
returns
删除的答案指出
[ 0. 1.25 2.5 3.75 5. 6.25 7.5 8.75 10. ]
采用一个参数。
,其他答案中给出的两个示例可以写为:linspace
endpoint
中定义的功能,以获取有关如何生成范围和/或网格的其他想法。 例如,
In [955]: np.linspace(0, 0+(0.1*3),3,endpoint=False)
Out[955]: array([ 0. , 0.1, 0.2])
In [956]: np.linspace(0, 0+(5*3),3,endpoint=False)
Out[956]: array([ 0., 5., 10.])
In [957]: np.linspace(0, 0+(1.25*9),9,endpoint=False)
Out[957]: array([ 0. , 1.25, 2.5 , 3.75, 5. , 6.25, 7.5 , 8.75, 10. ])
的行为像
numpy.lib.index_tricks
。
np.ogrid[0:10:9j]
其他解决方案中的某些解决方案对我不起作用,因此,由于我已经很舒服地使用
linspace
,所以我决定将一个替换为def altspace(start, step, count, endpoint=False, **kwargs):
stop = start+(step*count)
return np.linspace(start, stop, count, endpoint=endpoint, **kwargs)
的函数替换为np.linspace
参数。
linspace
示例输出:num
Edit:我误解了这个问题,原始问题想要一个省略参数的函数。我仍然将其留在这里,因为我认为这对一些偶然发现这个问题的人可能很有用,因为这是我发现的唯一一个类似于我最初的问题的问题,即找到
step
,
def linspace(start, stop, step=1.):
"""
Like np.linspace but uses step instead of num
This is inclusive to stop, so if start=1, stop=3, step=0.5
Output is: array([1., 1.5, 2., 2.5, 3.])
"""
return np.linspace(start, stop, int((stop - start) / step + 1))
和
linspace(9.5, 11.5, step=.5)
array([ 9.5, 10. , 10.5, 11. , 11.5])
的函数,而不是
stop
start
Maybe?
there是一个应该始终与浮子一起使用的人。
stop
如果您想将其与浮子以外的其他物品一起使用,则可以使
step
(或Kwarg)成为ARG(或Kwarg):
num
@wjandrea很好的答案。谢谢!