# NumPy Array from Numerical Ranges

In this article, we will see the functions that use numerical ranges to create a NumPy `ndarray`.

• `arange() `function
• `linspace()` function
• `logspace()` function

### arange() function

The arange() function of the NumPy library returns a ndarray having evenly spaced numerical values within a given range.

Syntax:

``arange([start,] stop[, step,], dtype=None, *, like=None)``

The `arange()` function takes the following arguments:

• start: start of the interval(value included in the interval) and default is 0.
• stop: end of the interval(value not included in the interval).
• step: Spacing between two consecutive values in the interval and default is 1.
• dtype: Sets the data type of elements in the array.

The stop is a mandatory parameter and start, step, dtype, and like are optional parameters in the `arange()` function.

Whenever step is specified it is required to provide start. Both integer and real values can be passed as input to start, stop and step arguments.

#### NumPy Array using arange() function:

We import the NumPy package using the `import` statement. Creating a `ndarray `using `arange()` function.

``````import numpy as np
np_arr = np.arange(10)
np_arr #other than Jupyter Notebook users, use print(np_arr)
#Output:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])``````

arange() function with start, step and dtype:

``````np_arr = np.arange(1.5, 5, 0.5, dtype = complex)
np_arr #other than Jupyter Notebook users, use print(np_arr)
#Output:
array([1.5+0.j, 2. +0.j, 2.5+0.j, 3. +0.j, 3.5+0.j, 4. +0.j, 4.5+0.j])``````

### linspace() function

The linspace() function works similar to NumPy’s arange() function. It returns a ndarray having evenly spaced elements in a given interval. The linspace() function uses the number of samples whereas arange() function works on step size.

Syntax:

``linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)``

The arguments of `linspace()` function are:

• start, stop and dtype are same as mentioned in `arange()` function.
• num: Number of evenly spaced values to generate and default is 50.
• endpoint: If False, stop value not is included in the interval and default is True.
• retstep: If True, returns the evenly spaced values with step size between conscecutive values in the interval, default is False.

The start and stop parameters are mandatory and others are optional in the `linspace()` function.

#### NumPy Array using linspace() function:

``````np_arr = np.linspace(0, 10, num=5)
np_arr #other than Jupyter Notebook users, use print(np_arr)
#Output:
array([ 0. ,  2.5,  5. ,  7.5, 10. ])``````

The output array consists of 5 evenly spaced elements as the number of sample values is given 5(num=5).

linspace() function with retstep and endpoint:

``````np_arr = np.linspace(2, 12, num=5, dtype=int, retstep=True, endpoint=False)
np_arr #other than Jupyter Notebook users, use print(np_arr)
#Output:
(array([ 2,  4,  6,  8, 10]), 2.0)``````

The output array does not have the stop value as element(endpoint=False) and 2.0 is the step size(difference between two values in the interval).

### logspace() function

The logspace() function of NumPy returns a ndarray that has evenly spaced values on a log scale.

Syntax:

``np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)``

The parameters of `logspace()` function are:

• num, endpoint and dtype are same as mentioned in `linspace()` function.
• base: base value, default is 10.
• start: start point of the sequence is base(start).
• stop: end of the sequence is base(stop).

#### NumPy Array using logspace() function:

``````np_arr = np.logspace(1, 3, num=5)
np_arr #other than Jupyter Notebook users, use print(np_arr)
#Output
array([  10.        ,   31.6227766 ,  100.        ,  316.22776602,
1000.        ])``````

The output array has 5 even spaced values between 101 to 103.

logspace() function with base:

``````np_arr = np.logspace(0, 2, num=3, base=2)
np_arr #other than Jupyter Notebook users, use print(np_arr)
#Output
array([1., 2., 4.])``````

The output array has 3 even spaced values between 20 to 22.