# The Numpy arange Function and Including Endpoints

In this article, we will be discussing NumPy arange function and including endpoints. The NumPy module (Numerical Python) is used for working with arrays. A vital data type in Numpy is a ndarray. It has functions for working on various domains in mathematics.

Contents

## What is arange?

To create an array conditional to numerical procedures in Python `np.arange()` is used. The `numpy.arange()` function returns a NumPy array object with spaced elements.

### Function Syntax & Parameters

``np.arange([start],[stop],[step],[dtype]`)`
• start – Indicates the start of the interval. Optional parameter with a default value of 0.
• stop – End of the interval range.
• step – Represents the size of the interval. Optional parameter with a default value of 1
• dtype – Describes the data type of the output array.

### Returns

Returns a NumPy array of evenly spaced values. The length of the output array is Ceil((Stop-Start) / Step)

## Creating an Array Using NumPy arange

Let’s look at how we can use this function to create a NumPy array of evenly spaced values.

```import numpy as np

myArray = np.arange(start=0, stop=12, step=3)
print(myArray)
```

Output

`[0 3 6 9]`

## Does NumPy arange Include Endpoints?

NumPy `arange()` does not include the endpoint. However, the MatLab equivalent does have an endpoint. In Python Numpy, the array is always one element short.

Why is this a feature?

In programming, it is more common to call `arange(0,10)` which returns 10 elements `[1,2,3,4,5,6,7,8,9]`. In Python, 0-based indexing is implemented, which means 0 is considered an element.

## Including Endpoints In Numpy arange Function

Let’s look at some implementations that include the endpoint in `arange()`

### Example 1

Increase the upper bound limit by 1 value. Initially, `np.arange()` does not include the endpoint. By increasing your upper value, the endpoint is obtained. The following program will attempt to create an array of elements from 1 – 10.

```import numpy as np

myArray = np.arange(start=0, stop=11, step=2)
print(myArray)
```

Output 1

`[0 2 4 6 8 10]`

### Example 2

Another solution is to include the endpoint in `arange()` is to create your own arange function like so:

```import numpy as np

def EParange(start, end):
return np.arange(start, end+1)

EParange(1,10)
```

Output 2

`array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10]`

### Example 3

Similar to Example 2, we can create a function like so:

```import numpy as np

def EParange(start, end):
return np.arange(start, end)

EParange(1,11)
```

Output

array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

## Alternatives to NumPy arange Function

### Using numpy.linspace()

The step value in `arange()` only takes integer arguments. Let’s see how we can create a step value of decimal increments.

In `arange()`assigning the step value as decimals may result in inaccurate values. Therefore, it is better to use `.linspace()` function in this scenario.

```import numpy as np
decimalArray = np.linspace(0.5, 1.0, 6)
print(decimalArray)
```

Output

`[0.5 0.6 0.7 0.8 0.9 1. ]`

### Using numpy.ogrid()

`ogrid` or “open grid” allow you to operate on specific pixels of an image based on row and column indexes.

Let’s see how we can create an array using `.ogrid()`

```import numpy as np
myArray = np.ogrid[0:10]
print(myArray)
```

Output

`[0 1 2 3 4 5 6 7 8 9]`

### Using numpy.mgrid()

The `mgrid()` function returns a multi-dimensional ‘mesh grid’.

The dimensions of the output arrays are equivalent to the number of the indexed dimensions.

```import numpy as np
myArray = np.mgrid[1:7, 1:7]
print(myArray)
```

Output

```[[[1 1 1 1 1 1]
[2 2 2 2 2 2]
[3 3 3 3 3 3]
[4 4 4 4 4 4]
[5 5 5 5 5 5]
[6 6 6 6 6 6]]

[[1 2 3 4 5 6]
[1 2 3 4 5 6]
[1 2 3 4 5 6]
[1 2 3 4 5 6]
[1 2 3 4 5 6]
[1 2 3 4 5 6]]]```

## FAQs on numpy.arange()

How to change the data type in NumPy arange()?

The `dtype` parameter allows you to change the data type of the output array.

Does the NumPy arange() include endpoints?

The `arange()` function does not include endpoints. This is because most Python functions follow 0-based indexing. This means the 0th value is taken into account.

Does the Python range function include endpoints?

`range()` generates numbers up to the end number but never includes it in its results because it generates numbers up to the endpoint only. This is similar to the above FAQ, and Python follows 0-based indexing.

How to use array_like in NumPy arange?

`array_like` parameter creates a reference object that allows us to create arrays that are not NumPy arrays. It guarantees the creation of an array object that’s compatible with the array passed through this parameter.

## Conclusion

In this article, we’ve looked at the NumPy module and its arange function. Different implementations to include the endpoint have been reviewed. The reason why most Python functions do not include an endpoint has been mentioned. Alternatives to arange function have been demonstrated.

Subscribe
Notify of 