Python – numpy.arange()

Being a linear sequence generator, the numpy.arange() function is used to generate a sequence of numbers in linear space with a uniform step size.

作为线性序列生成器, numpy.arange()函数用于在线性空间中以均匀步长生成数字序列。

This is similar to another function, numpy.linspace(), which also generates a linear sequence with a uniform step size.

这类似于另一个函数numpy.linspace() ,该函数还生成具有均匀步长的线性序列。

Let’s understand how we can use this function to generate different sequences.

让我们了解如何使用此函数生成不同的序列。



句法 (Syntax)

Format:

格式:


array = numpy.arange(start, stop, step, dtype=None)

Here,

这里,

  • start -> The starting point (included) of the range, which is set to 0 by default.

    start >范围的起点( 包括在内 ),默认情况下设置为0
  • stop -> The ending point (excluded) of the range

    stop ->范围的终点( 不包括
  • step -> The step size of the sequence, which is set to 1 by default. This can be any real number except Zero.

    step >序列的步长,默认设置为1。 外,它可以是任何实数。
  • dtype -> The type of the output array. If dtype is not given (or provided as None), the datatype will be inferred from the type of other input arguments.

    dtype >输出数组的类型。 如果未给出dtype (或作为None提供),则将从其他输入参数的类型推断出数据类型。

Let us take a simple example to understand this:

让我们举一个简单的例子来理解这一点:


import numpy as np
 
a = np.arange(0.02, 2, 0.1, None)
 
print('Linear Sequence from 0.02 to 2:', a)
print('Length:', len(a))

This will generate a linear sequence from 0.2 (included) until 2 (excluded) with a step size of 0.1, so there will be (2 – 0.2)/0.1 – 1 = 20 elements in the sequence, which is the length of the resulting numpy array.

这将生成一个从0.2 (包括)到2 (不包括)的线性序列,步长为0.1 ,因此序列中将有(2 – 0.2)/0.1 – 1 = 20个元素,这是结果的长度numpy数组。

Output

输出量


Linear Sequence from 0.02 to 2: [0.02 0.12 0.22 0.32 0.42 0.52 0.62 0.72 0.82 0.92 1.02 1.12 1.22 1.32
 1.42 1.52 1.62 1.72 1.82 1.92]
Length: 20

Here is another line of code which generates the numbers from 0 to 9 using arange(), using the default step size of 1:

这是另一行代码,它使用arange()使用默认步长1生成从0到9的数字:


>>> np.arange(0, 10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

If the step size is provided as 0, this is not a valid sequence, since a step of 0 implies you are dividing the range by 0, which will raise a ZeroDivisionError Exception.

如果步长为0,则此序列无效,因为步长为0意味着您将范围除以0,这将引发ZeroDivisionError异常。


import numpy as np

# Invalid Step Size!
a = np.arange(0, 10, 0)

Output

输出量


ZeroDivisionError: division by zero

NOTE: This function is a bit different from numpy.linspace(), which, by default, includes both the starting and the endpoints for the sequence calculation. It also does not take the step size as an argument, but rather takes only the number of elements in the sequence.

注意 :此函数与numpy.linspace()不同, numpy.linspace()默认情况下包括序列计算的起始和numpy.linspace() 。 它还不以步长为参数,而是仅采用序列中元素的数量。



一个简单的例子 (A simple example)

Let’s now put all of this together into a simple example to demonstrate the linearity of the sequences generated by numpy.arange().

现在,将所有这些放到一个简单的示例中,以演示numpy.arange()生成的序列的线性。

The following code plots 2 linear sequences between [0, 20] and [0, 10] using numpy.arange() to show that there is uniformity generated by the sequence, so the resulting arrays are linear.

以下代码使用numpy.arange()[0, 10] numpy.arange() [0, 20][0, 10] numpy.arange() [0, 20]之间绘制2个线性序列[0, 20]以显示该序列生成的均匀性,因此所得数组为线性。


import numpy as np
import matplotlib.pyplot as plt

y = np.zeros(5)

# Construct two linear sequences
# First one has a step size of 4 units
x1 = np.arange(0, 20, 4)

# Second one has a step size of 2 units
x2 = np.arange(0, 10, 2)

# Plot (x1, [0, 0, ..])
plt.plot(x1, y, 'o')

# Plot (x2, [0.5, 0.5, ..])
plt.plot(x2, y + 0.5, 'o')

# Set limit for y on the plot
plt.ylim([-0.5, 1])

plt.show()

Output

输出量

Python – numpy.arange()_第1张图片
Numpy Arange 脾气暴躁的

As you can see, the orange dots represent a linear sequence from 0 to 10 having a step size of 2 units, but since 10 is not included, the sequence is [0, 2, 4, 6, 8]. Similarly, the blue dots represent the sequence [0, 4, 8, 12, 16].

如您所见,橙色点代表从0到10的线性序列,步长为2个单位,但由于不包括10,因此该序列为[0, 2, 4, 6, 8] 。 类似地,蓝点表示序列[0, 4, 8, 12, 16]



numpy.arange()与range() (numpy.arange() vs range())

The whole point of using the numpy module is to ensure that the operations that we perform are done as quickly as possible, since numpy is a Python interface to lower level C++ code.

使用numpy模块的全部目的是确保我们执行的操作尽快完成,因为numpy是较低级C ++代码的Python接口。

Many operations in numpy are vectorized, meaning that operations occur in parallel when numpy is used to perform any mathematical operation. Due to this, for large arrays and sequences, numpy produces the best performance.

numpy中的许多运算都是矢量化的 ,这意味着当使用numpy执行任何数学运算时,这些运算会并行发生。 因此,对于大型数组和序列, numpy产生最佳性能。

Therefore, the numpy.arange() is much faster than Python’s native range() function for generating similar linear sequences.

因此, numpy.arange()比Python的native range()函数生成相似的线性序列要快得多。

性能测试 (Performance Test)

We should not interleave numpy‘s vectorized operation along with a Python loop. This slows down performance drastically, as the code is iterating using native Python.

我们不应该将 numpy的向量化操作与Python循环一起插入。 由于代码正在使用本机Python进行迭代,因此这会极大地降低性能。

For example, the below snippet shows how you should NOT use numpy.

例如,下面的片段展示了如何应该使用numpy的。


for i in np.arange(100):
    pass

The recommended way is to directly use the numpy operation.

推荐的方法是直接使用numpy操作。


np.arange(100)

Let’s test the difference in performance using Python’s timeit module.

让我们使用Python的timeit模块测试性能差异。


import timeit
import numpy as np

# For smaller arrays
print('Array size: 1000')

# Time the average among 10000 iterations
print('range():', timeit.timeit('for i in range(1000): pass', number=10000))
print('np.arange():', timeit.timeit('np.arange(1000)', number=10000, setup='import numpy as np'))

# For large arrays
print('Array size: 1000000')

# Time the average among 10 iterations
print('range():', timeit.timeit('for i in range(1000000): pass', number=10))
print('np.arange():', timeit.timeit('np.arange(1000000)', number=10, setup='import numpy as np'))

Output

输出量


Array size: 1000
range(): 0.18827421900095942
np.arange(): 0.015803234000486555
Array size: 1000000
range(): 0.22560399899884942
np.arange(): 0.011916546000065864

As you can see, numpy.arange() works particularly well for large sequences. It’s almost 20 times (!!) as fast as the normal Python code for a size of just 1000000, which will only scale better for larger arrays.

如您所见, numpy.arange()对于大型序列特别有效。 它的速度几乎是普通Python代码的20倍(!!),大小仅为1000000 ,仅对较大的数组可扩展。

Therefore, numpy.arange() should be the unanimous choice among programmers when working with larger arrays.

因此,在使用较大数组时, numpy.arange()应该是程序员之间的一致选择。

For smaller arrays, when the difference in performance isn’t that much, you could use among either of the two methods.

对于较小的阵列,如果性能差异不那么大,则可以在两种方法中的任何一种中使用。



参考资料 (References)

  • SciPy Documentation on numpy.arange()

    numpy.arange()上的SciPy文档


翻译自: https://www.journaldev.com/34380/python-numpy-arange

你可能感兴趣的:(c++,python,java,机器学习,人工智能)