While working with machine learning or data science projects, you might be often be required to generate a numpy array with a sequence of numbers. In this post we will see how **numpy.arange()**, **numpy.linspace()** and n**umpy.logspace()** can be used to create such sequences of array.

At the end of this post, we will also summarize the differences between numpy arange, numpy linspace, and numpy logspace.

But first, let us import the numpy library.

```
import numpy as np
```

Contents

**Numpy Arange: np.arange()**

Numpy arange is useful when you want to create a numpy array, having a range of elements spaced out over a specified interval.

### Syntax

**np.arange(start, stop, step, dtype)**

** start** (optional) – This signifies the start of the interval. If it is not specified, then the default value is 0.

** stop** – This signifies the stop or end of the interval. This number is not included in the interval, however.

** step** (optional) – This signifies the space between the intervals. If it is not mentioned, then by default is 1.

** dtype** (optional) – This represents the output data type of the numpy array. If it is not mentioned, then it will inference from other input parameters.

### Example 1:

In the below example, we have just mentioned the mandatory input of **stop = 7**. This creates a numpy array with default **start=0** and default **step=1**. Do notice that the last element is exclusive of 7.

```
np.arange(7)
```

### Example 2:

In the below example, we have mentioned **start=5** and **stop=7**. This creates a numpy array having elements between 5 to 10 (excluding 11) and default **step=1**.

```
np.arange(5,11)
```

### Example 3:

In the below example, we have created a numpy array whose elements are between **5 to 15(exclusive)** having an **interval of 3**.

```
np.arange(5,15,3)
```

### Example 4:

Below is another example with float values. Here **start=5.2** , **stop=18.5** and **interval=2.1**

```
np.arange(5.2,18.5,2.1)
```

**Numpy Linspace: np.linspace()**

Numpy Linspace is used to create a numpy array whose elements are equally spaced between start and end.

### Syntax

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

** start** – It represents the starting value of the sequence in numpy array. The input can be a number or any array-like value.

** stop** – It represents the stop value of the sequence in numpy array. The input can be a number or any array-like value.

** num** (optional) – It represents the number of elements to be generated between the start and stop values. The input is of int type and should be non-negative, and if no input is given then the default is 50.

** endpoint** (optional) – It signifies if the value mentioned in stop has to be the last sample when True, otherwise it is not included. The input is bool and the default is True.

** retstep** (optional) – It signifies whether the value num is the number of samples (when False) or the step size (when True). The input is bool and by default False.

** dtype** (optional) – This represents the output data type of the numpy array. If it is not mentioned, then it will inference from other input parameters.

** axis** (optional) – This represents the axis in the result to store the samples. It is relevant only if the start or stop values are array-like. By default, when 0, the samples will be along a new axis inserted at the beginning. We can give -1 to get an axis at the end.

### Example 1:

In this example, let us only pass the mandatory parameters **start=5** and **stop=25**. It will create a numpy array having a **50 (default)** elements equally spaced between **5 and 25**. The output is looking like a 2-D array, but it is actually just a 1-D array, it is just that the output is formatted in this way.

```
np.linspace(5,25)
```

### Example 2:

In this example, we have explicitly mentioned that we required only **3** equally spaced numbers **between 5 and 25** in the numpy array. Do notice that the elements in numpy array are **float**.

```
np.linspace(start=5,stop=25,num=3)
```

### Example 3:

In this example, let us just modify the above example and give a data type as **int**.

```
np.linspace(start=5,stop=25,num=3,dtype=int)
```

We can also pass an array-like Tuple or List in start and stop parameter. It will expand the array with elements that are equally spaced.

```
np.linspace(start=(5,9),stop=25,num=3,dtype=int)
```

```
np.linspace(start=(5,9),stop=(25,36), num=3,dtype=int)
```

**Numpy Logspace: np.logspace()**

Numpy Linspace is used to create a numpy array whose elements are equally spaced between start and end on logarithmic scale.

### Syntax

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

** start** – It represents the starting value of the sequence in numpy array. The input can be a number or any array-like value.

** stop** – It represents the stop value of the sequence in numpy array. The input can be a number or any array-like value.

** num** (optional) – It represents the number of elements to be generated between start and stop values. The input is of int type and should be non-negative, and if no input is given then the default is 50.

** endpoint** (optional) – It signifies if the value mentioned in stop has to be the last sample when True, otherwise it is not included. The input is bool and the default is True.

** base** (optional) – It signifies the base of logarithmic space. The input is float and the default value is 10.

** dtype** (optional) – This represents the output data type of the numpy array. If it is not mentioned, then it will inference from other input parameters.

** axis** (optional) – This represents the axis in the result to store the samples. It is relevant only if the start or stop values are array-like. By default, when 0, the samples will be along a new axis inserted at the beginning. We can give -1 to get an axis at the end.

### Example 1:

In this example, let us only pass the mandatory parameters **start=5** and **stop=20**. It will create a numpy array having a **50 (default)** elements equally spaced between **5 and 20**, but they are on a **logarithmic scale**. The output is looking like a 2-D array, but it is actually just a 1-D array, it is just that the output is formatted in this way.

```
np.logspace(5,20)
```

### Example 2:

In this example, we have explicitly mentioned that we required only **6** equally spaced numbers **between 5 and 25** in the numpy array on **log base 10 (default)**. Do notice that the elements in the numpy array are **float**.

```
np.logspace(start=5,stop=20,num=6)
```

### Example 3:

In this example, let us just modify the above example and give a data type as **int**.

```
np.logspace(start=5,stop=20,num=6,dtype=int)
```

### Example 4:

In this example, we have passed **base=2** for logarithmic scale.

```
np.logspace(start=5,stop=20,num=6, base=2,dtype=int)
```

## Numpy Arange vs Linspace vs Logspace

Let us quickly summarize between Numpy Arange, Numpy Linspace, and Numpy Logspace, so that you have a clear understanding –

1) Numpy Arange is used to create a numpy array whose elements are between the start and stop range, and we specify the step interval.

2) Numpy Linspace is used to create a numpy array whose elements are between start and stop range, and we specify how many elements we want in that range.

3) Numpy Logspace is similar to Linsace but the elements are generated based on a logarithmic scale.

**Click Here To Download This Tutorial in Interactive Jupyter Notebook**

- Also Read – Python Numpy Array – A Gentle Introduction to beginners
- Also Read – Tutorial – numpy.arange() , numpy.linspace() , numpy.logspace() in Python
- Also Read – Complete Numpy Random Tutorial – Rand, Randn, Randint, Normal
- Also Read – Tutorial – Numpy Shape, Numpy Reshape and Numpy Transpose in Python

*Reference-* https://numpy.org/doc/