Tutorial – numpy.arange() , numpy.linspace() , numpy.logspace() in Python

numpy.arange() , numpy.linspace() , numpy.logspace() in Python
numpy.arange() , numpy.linspace() , numpy.logspace() in Python

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 numpy.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 Library
In [1]:
import numpy as np

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)

Ad
Deep Learning Specialization on Coursera

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.

In [2]:
np.arange(7)
Out[2]:
array([0, 1, 2, 3, 4, 5, 6])

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.

In [3]:
np.arange(5,11)
Out[3]:
array([ 5,  6,  7,  8,  9, 10])

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.

In [4]:
np.arange(5,15,3)
Out[4]:
array([ 5,  8, 11, 14])

Example 4:

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

In [6]:
np.arange(5.2,18.5,2.1)
Out[5]:
array([ 5.2,  7.3,  9.4, 11.5, 13.6, 15.7, 17.8])

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.

In [7]:
np.linspace(5,25)
Out[7]:
array([ 5.        ,  5.40816327,  5.81632653,  6.2244898 ,  6.63265306,
        7.04081633,  7.44897959,  7.85714286,  8.26530612,  8.67346939,
        9.08163265,  9.48979592,  9.89795918, 10.30612245, 10.71428571,
       11.12244898, 11.53061224, 11.93877551, 12.34693878, 12.75510204,
       13.16326531, 13.57142857, 13.97959184, 14.3877551 , 14.79591837,
       15.20408163, 15.6122449 , 16.02040816, 16.42857143, 16.83673469,
       17.24489796, 17.65306122, 18.06122449, 18.46938776, 18.87755102,
       19.28571429, 19.69387755, 20.10204082, 20.51020408, 20.91836735,
       21.32653061, 21.73469388, 22.14285714, 22.55102041, 22.95918367,
       23.36734694, 23.7755102 , 24.18367347, 24.59183673, 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.

In [8]:
np.linspace(start=5,stop=25,num=3)
Out[8]:
array([ 5., 15., 25.])

Example 3:

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

In [9]:
np.linspace(start=5,stop=25,num=3,dtype=int)
Out[9]:
array([ 5, 15, 25])
Example 4:

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.

In [10]:
np.linspace(start=(5,9),stop=25,num=3,dtype=int)
Out[10]:
array([[ 5,  9],
       [15, 17],
       [25, 25]])
In [11]:
np.linspace(start=(5,9),stop=(25,36), num=3,dtype=int)
Out[11]:
array([[ 5,  9],
       [15, 22],
       [25, 36]])

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.

In [12]:
np.logspace(5,20)
Out[12]:
array([1.00000000e+05, 2.02358965e+05, 4.09491506e+05, 8.28642773e+05,
       1.67683294e+06, 3.39322177e+06, 6.86648845e+06, 1.38949549e+07,
       2.81176870e+07, 5.68986603e+07, 1.15139540e+08, 2.32995181e+08,
       4.71486636e+08, 9.54095476e+08, 1.93069773e+09, 3.90693994e+09,
       7.90604321e+09, 1.59985872e+10, 3.23745754e+10, 6.55128557e+10,
       1.32571137e+11, 2.68269580e+11, 5.42867544e+11, 1.09854114e+12,
       2.22299648e+12, 4.49843267e+12, 9.10298178e+12, 1.84206997e+13,
       3.72759372e+13, 7.54312006e+13, 1.52641797e+14, 3.08884360e+14,
       6.25055193e+14, 1.26485522e+15, 2.55954792e+15, 5.17947468e+15,
       1.04811313e+16, 2.12095089e+16, 4.29193426e+16, 8.68511374e+16,
       1.75751062e+17, 3.55648031e+17, 7.19685673e+17, 1.45634848e+18,
       2.94705170e+18, 5.96362332e+18, 1.20679264e+19, 2.44205309e+19,
       4.94171336e+19, 1.00000000e+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.

In [13]:
np.logspace(start=5,stop=20,num=6)
Out[13]:
array([1.e+05, 1.e+08, 1.e+11, 1.e+14, 1.e+17, 1.e+20])

Example 3:

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

In [14]:
np.logspace(start=5,stop=20,num=6,dtype=int)
Out[14]:
array([     100000,   100000000, -2147483648, -2147483648, -2147483648,
       -2147483648])

Example 4:

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

In [15]:
np.logspace(start=5,stop=20,num=6, base=2,dtype=int)
Out[16]:
array([     32,     256,    2048,   16384,  131072, 1048576])

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

 

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

Like and Comment section (Community Members)

Create Your ML Profile!

Don't miss out to join exclusive Machine Learning community

Comments

No comments yet