np.arange() 详细教程

NumPy is the fundamental Python library for numerical computing. Its most important type is an array type called ndarray. NumPy offers a lot of array creation routines for different circumstances. arange() is one such function based on numerical ranges. It’s often referred to as np.arange() because np is a widely used abbreviation for NumPy.

Creating NumPy arrays is important when you’re working with other Python libraries that rely on them, like SciPyPandas, Matplotlib, scikit-learn, and more. NumPy is suitable for creating and working with arrays because it offers useful routines, enables performance boosts, and allows you to write concise code.

By the end of this article, you’ll know:

  • What np.arange() is
  • How to use np.arange()
  • How np.arange() compares to the Python built-in class range
  • Which routines are similar to np.arange()

Let’s see np.arange() in action!

Free Bonus: Click here to get access to a free NumPy Resources Guide that points you to the best tutorials, videos, and books for improving your NumPy skills.

 

 Remove ads

Return Value and Parameters of np.arange()

NumPy arange() is one of the array creation routines based on numerical ranges. It creates an instance of ndarray with evenly spaced values and returns the reference to it.

You can define the interval of the values contained in an array, space between them, and their type with four parameters of arange():

numpy.arange([start, ]stop, [step, ], dtype=None) -> numpy.ndarray

The first three parameters determine the range of the values, while the fourth specifies the type of the elements:

  1. start is the number (integer or decimal) that defines the first value in the array.
  2. stop is the number that defines the end of the array and isn’t included in the array.
  3. step is the number that defines the spacing (difference) between each two consecutive values in the array and defaults to 1.
  4. dtype is the type of the elements of the output array and defaults to None.

step can’t be zero. Otherwise, you’ll get a ZeroDivisionError. You can’t move away anywhere from start if the increment or decrement is 0.

If dtype is omitted, arange() will try to deduce the type of the array elements from the types of startstop, and step.

You can find more information on the parameters and the return value of arange() in the official documentation.

Range Arguments of np.arange()

The arguments of NumPy arange() that define the values contained in the array correspond to the numeric parameters startstop, and step. You have to pass at least one of them.

The following examples will show you how arange() behaves depending on the number of arguments and their values.

Providing All Range Arguments

When working with NumPy routines, you have to import NumPy first:

>>>

>>> import numpy as np

Now, you have NumPy imported and you’re ready to apply arange().

Let’s see a first example of how to use NumPy arange():

>>>

>>> np.arange(start=1, stop=10, step=3)
array([1, 4, 7])

In this example, start is 1. Therefore, the first element of the obtained array is 1step is 3, which is why your second value is 1+3, that is 4, while the third value in the array is 4+3, which equals 7.

Following this pattern, the next value would be 10 (7+3), but counting must be ended beforestop is reached, so this one is not included.

You can pass startstop, and step as positional arguments as well:

>>>

>>> np.arange(1, 10, 3)
array([1, 4, 7])

This code sample is equivalent to, but more concise than the previous one.

The value of stop is not included in an array. That’s why you can obtain identical results with different stop values:

>>>

>>> np.arange(1, 8, 3)
array([1, 4, 7])

This code sample returns the array with the same values as the previous two. You can get the same result with any value of stop strictly greater than 7 and less than or equal to 10.

However, if you make stop greater than 10, then counting is going to end after 10 is reached:

>>>

>>> np.arange(1, 10.1, 3)
array([ 1.,  4.,  7., 10.])

In this case, you get the array with four elements that includes 10.

Notice that this example creates an array of floating-point numbers, unlike the previous one. That’s because you haven’t defined dtype, and arange() deduced it for you. You’ll learn more about this later in the article.

You can see the graphical representations of these three examples in the figure below:

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值