将 np.arange 中的 range 增加 1,而不是将 range 增加 2 [英] Increasing range in np.arange by 1 increases range by 2 instead

查看:81
本文介绍了将 np.arange 中的 range 增加 1,而不是将 range 增加 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是一个错误还是我做错了什么.我有以下代码:

I'm not sure if this is a bug or if I'm doing something wrong. I've got the following code:

r_div = 200
r_max = 1.4
numMax=.84

lowerBin = int((numMax - .2)/(r_max/r_div))
upperBin = int((numMax + .2)/(r_max/r_div))

k =np.arange((r_max/r_div)*lowerBin,(r_max/r_div)*(upperBin+1),r_max/r_div)

当我运行 np.shape(k) 时,我得到 (59).现在,如果我在最后一行将上限更改一个:

When I run np.shape(k), I get (59). Now, if I change the upper limit by one in the last line:

k =np.arange((r_max/r_div)*lowerBin,(r_max/r_div)*(upperBin),r_max/r_div)

再次运行 np.shape(k),它给了我 57.当我只将 arange 的上限更改 1 时,我不确定为什么它会更改 2.

and run np.shape(k) again, it gives me 57. I'm not really sure why it's changing by 2 when I'm only changing the upperbound on arange by 1.

推荐答案

来自 arange 文档:

arange([start,] stop[, step,], dtype=None)

....

When using a non-integer step, such as 0.1, the results will often not
be consistent.  It is better to use `numpy.linspace` for these cases.

这篇关于将 np.arange 中的 range 增加 1,而不是将 range 增加 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆