numpy arange中包含的数字[重复]

时间:2021-09-11 21:25:27

This question already has an answer here:

这个问题在这里已有答案:

I hope to get a simple decimal range by numpy.arange . But, my code outputs unexpected result and size. My snippet shown below.

我希望通过numpy.arange得到一个简单的小数范围。但是,我的代码输出意外的结果和大小。我的片段如下所示。

len(arange(0.60,0.70,0.01))

it returns size 10 ndarray object

它返回大小为10的ndarray对象

array([0.60, ... ,0.69])

And, the max value is changed

并且,最大值被改变

len(arange(0.60,0.80,0.01))

array([0.60, ... , 0.79, 0.80])

I expected it will also return size 20 one.

我预计它也将返回20号。

array([0.60, ... , 0.79])

数组([0.60,...,0.79])

But, the size is 21 and 0.80 is included.
Could you explain Why is this? and What is the difference between two ranges?

但是,大小是21和0.80包括在内。你能解释一下这是为什么吗?两个范围有什么区别?

Python : 3.5.2
numpy : 1.10.1

Python:3.5.2 numpy:1.10.1

1 个解决方案

#1


1  

As is writen in numpy.arange documentation,

正如在numpy.arange文档中所写,

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

“当使用非整数步骤(例如0.1)时,结果通常不一致。最好在这些情况下使用linspace。”

Note that linspace includes both endpoint, so this np.linspace(0.60,0.69,10) will produce

请注意,linspace包含两个端点,因此将生成此np.linspace(0.60,0.69,10)

array([ 0.6 ,  0.61,  0.62,  0.63,  0.64,  0.65,  0.66,  0.67,  0.68,  0.69])

#1


1  

As is writen in numpy.arange documentation,

正如在numpy.arange文档中所写,

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

“当使用非整数步骤(例如0.1)时,结果通常不一致。最好在这些情况下使用linspace。”

Note that linspace includes both endpoint, so this np.linspace(0.60,0.69,10) will produce

请注意,linspace包含两个端点,因此将生成此np.linspace(0.60,0.69,10)

array([ 0.6 ,  0.61,  0.62,  0.63,  0.64,  0.65,  0.66,  0.67,  0.68,  0.69])