SoFunction
Updated on 2025-03-02

Usage and description of arange in Numpy

1. Overview

In Numpy, range() is mainly used to generate arrays, and the specific usage is as follows;

2. arange()

2.1 Syntax

(start, stop, step, dtype = None)

Returns the value of uniformly spaced within a given interval.

The value is generated within the half-open interval [start, stop] (in other words, the interval that starts but does not include the stop),The return is ndarray.

2.2 Parameters

  • start——Start position, number,Optional, the default starting value is 0
  • stop——Stop position, number
  • step—— Step size, number,Optional, the default step size is 1, if step is specified, start must also be given.
  • dtype——The type of output array. If dtype is not given, the data type is inferred from other input parameters.

return:

arange:ndarray

An array of evenly spaced values.

Notice:

  • For floating point parameters (the parameter is floating point), the length of the result is ceil((stop - start)/step)) Due to floating point overflow, this rule may cause the last element to be larger than stop.
  • Therefore, pay special attention to it

2.3 Example

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2019/1/22 9:33
# @Author  : Arrow and Bullet
# @FileName: range_arange.py
# @Software: PyCharm
# @Blog    :/qq_41800366
from numpy import *  #Introduce numpy
A = arange(5)  # Only end itemsprint(A)  # Result [0 1 2 3 4] The result does not contain the end itemprint(type(A))  # Result <class ''>
A = arange(1, 5)  # The starting point is 1, the step size is 1 by defaultprint(A)  # Results [1 2 3 4]
A = arange(1, 5, 2)  # The default step size is 2print(A)  # Results [1 3]
A = arange(1, 5.2, 0.6)  # Floating point parameters may not be fully consistentprint(A)  # result [1.  1.6 2.2 2.8 3.4 4.  4.6 5.2]

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.