# numpy.arange(), linspace(): Generate ndarray with evenly spaced values

Posted: 2020-07-25 / Tags: Python, NumPy

`numpy.arange()` and `numpy.linspace()` generate `numpy.ndarray` with evenly spaced values.

The difference is that the interval is specified for `np.arange()` and the number of elements is specified for `np.linspace()`.

• `numpy.arange()`: specify a interval
• `numpy.linspace()`: specify a num of elements
• Basic usage
• `endpoint`: include `stop` or not
• `retstep`: get the interval
• Convert to reverse order
• Convert to a multidimensional array

## numpy.arange(): specify a interval

`numpy.arange()` is similar to Python's built-in function `range()`. See the following article for `range()`.

`numpy.arange()` generate `numpy.ndarray` with evenly spaced values as follows according to the number of specified arguments.

• `numpy.arange(stop)`
• `0 <= n < stop`
• `numpy.arange(start, stop)`
• `start <= n < stop`
• `numpy.arange(start, stop, step)`
• `start <= n < stop` (increasing by `step`)
``````import numpy as np

print(np.arange(3))
# [0 1 2]

print(np.arange(3, 10))
# [3 4 5 6 7 8 9]

print(np.arange(3, 10, 2))
# [3 5 7 9]
``````

Unlike `range()`, you can specify `float` as an argument to `numpy.arange()`.

``````print(np.arange(0.3, 1.0, 0.2))
# [0.3 0.5 0.7 0.9]
``````

The behavior with negative values is the same as that of `range()`. Similarly, if there is no corresponding value, it generates an empty `numpy.ndarray`.

``````print(np.arange(-3, 3))
# [-3 -2 -1  0  1  2]

print(np.arange(10, 3))
# []

print(np.arange(10, 3, -2))
# [10  8  6  4]
``````

The data type `dtype` is automatically selected, but you can specify with the argument `dtype`.

``````print(np.arange(3, dtype=float))
# [0. 1. 2.]

print(np.arange(3, 10, dtype=float))
# [3. 4. 5. 6. 7. 8. 9.]

print(np.arange(3, 10, 2, dtype=float))
# [3. 5. 7. 9.]
``````

See the following article for more information about the data type `dtype` in NumPy.

## numpy.linspace(): specify a num of elements

### Basic usage

With `numpy.linspace()`, you can specify a number of elements instead of a interval.

Specify the starting value in the first argument `start`, the end value in the second argument `stop`, and the number of elements in the third argument `num`. The interval is automatically calculated according to those values.

``````import numpy as np

print(np.linspace(0, 10, 3))
# [ 0.  5. 10.]

print(np.linspace(0, 10, 4))
# [ 0.          3.33333333  6.66666667 10.        ]

print(np.linspace(0, 10, 5))
# [ 0.   2.5  5.   7.5 10. ]
``````

It also handles the case of `start > stop` properly.

``````print(np.linspace(10, 0, 5))
# [10.   7.5  5.   2.5  0. ]
``````

The data type `dtype` is automatically selected, but you can specify with the argument `dtype`.

``````print(np.linspace(0, 10, 3, dtype=int))
# [ 0  5 10]
``````

### endpoint: include "stop" or not

By default, the value of `stop` is included in the result.

If the argument `endpoint` is set to `False`, the result does not include `stop`.

``````print(np.linspace(0, 10, 5))
# [ 0.   2.5  5.   7.5 10. ]

print(np.linspace(0, 10, 5, endpoint=False))
# [0. 2. 4. 6. 8.]
``````

The relationship between the argument `endpoint` and the interval `step` is as follows.

• `endpoint=True` (default)
• `step = (stop - start) / (num - 1)`
• `endpoint=False`
• `step = (stop - start) / num`

### retstep: get the interval

If you want to get the interval, set the argument `retstep` to `True`.

In this case, `numpy.linspace()` returns a tuple with two elements, `(numpy.ndarray, step)`.

``````result = np.linspace(0, 10, 5, retstep=True)

print(result)
# (array([ 0. ,  2.5,  5. ,  7.5, 10. ]), 2.5)

print(type(result))
# <class 'tuple'>

print(result[0])
# [ 0.   2.5  5.   7.5 10. ]

print(result[1])
# 2.5
``````

If you want to check only `step`, get the second element with the index.

``````print(np.linspace(0, 10, 5, retstep=True)[1])
# 2.5

print(np.linspace(0, 10, 5, retstep=True, endpoint=False)[1])
# 2.0
``````

## Convert to reverse order

With `numpy.arange()`, you can get an array in reverse order if you specify the arguments properly, but it is troublesome.

``````print(np.arange(3, 10, 2))
# [3 5 7 9]

print(np.arange(9, 2, -2))
# [9 7 5 3]
``````

It is easy to use slice `[::-1]` or `numpy.flip()`.

``````print(np.arange(3, 10, 2)[::-1])
# [9 7 5 3]

print(np.flip(np.arange(3, 10, 2)))
# [9 7 5 3]
``````

In the case of `numpy.linspace()`, you can easily reverse the order by replacing the first argument `start` and the second argument `stop`. The result is the same with slice `[::-1]` and `numpy.flip()`.

``````print(np.linspace(0, 10, 5))
# [ 0.   2.5  5.   7.5 10. ]

print(np.linspace(10, 0, 5))
# [10.   7.5  5.   2.5  0. ]

print(np.linspace(0, 10, 5)[::-1])
# [10.   7.5  5.   2.5  0. ]

print(np.flip(np.linspace(0, 10, 5)))
# [10.   7.5  5.   2.5  0. ]
``````

## Convert to a multidimensional array

Neither `numpy.arange()` nor `numpy.linspace()` have any arguments to specify the shape. Use the `reshape()` to convert to a multidimensional array.

``````print(np.arange(12).reshape(3, 4))
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

print(np.arange(24).reshape(2, 3, 4))
# [[[ 0  1  2  3]
#   [ 4  5  6  7]
#   [ 8  9 10 11]]
#
#  [[12 13 14 15]
#   [16 17 18 19]
#   [20 21 22 23]]]
``````
``````print(np.linspace(0, 10, 12).reshape(3, 4))
# [[ 0.          0.90909091  1.81818182  2.72727273]
#  [ 3.63636364  4.54545455  5.45454545  6.36363636]
#  [ 7.27272727  8.18181818  9.09090909 10.        ]]

print(np.linspace(0, 10, 24).reshape(2, 3, 4))
# [[[ 0.          0.43478261  0.86956522  1.30434783]
#   [ 1.73913043  2.17391304  2.60869565  3.04347826]
#   [ 3.47826087  3.91304348  4.34782609  4.7826087 ]]
#
#  [[ 5.2173913   5.65217391  6.08695652  6.52173913]
#   [ 6.95652174  7.39130435  7.82608696  8.26086957]
#   [ 8.69565217  9.13043478  9.56521739 10.        ]]]
``````