24-Hour Flash Sale. Courses from just $11.99
Home Data Wrangling Python Numpy Quick Tutorial for Numpy Linspace with Examples for Beginners

Quick Tutorial for Numpy Linspace with Examples for Beginners

Tutorial for Numpy Linspace np.linspace with Examples

What is Linspace() in Numpy

In Numpy Linspace() function is used to create a numpy array with evenly spaced numbers between the two intervals provided as input.  In this tutorial, we will see the syntax of np.linspace() and see various examples by using various parameters.

Numpy Linspace: np.linspace()

Syntax

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

  • start – This signifies the starting value of the sequence and can be a number or an array-like value.
  • stop – This signifies the stop value of the sequence and can be a number or any array-like value. However, when the endpoint is False the stop value is excluded from the sequence.
  • num (optional) – It denotes the number of samples to be generated between the start and stop. The input should be of int type and non-negative. The default value is 50.
  • endpoint (optional) – When its True, the last value mentioned in the stop is included otherwise not included when False. The default value is True.
  • retstep (optional) – When True the function also returns the step size used to generate the sequence of samples. The default value is False.
  • dtype (optional) – It denotes the data type of the numpy array that is returned as the output. If it is not mentioned, then it will infer from the start and stop parameters. However, it should be noted that even though start and stop are integers, the output numpy array will be of type float only.
  • axis (optional) – It denotes the axis in the result to store the samples and is relevant only when start or stop values are array-like. The default value is 0, where samples are along a new axis inserted at the beginning. -1 is used to have the axis at the end.

Examples of Numpy Linspace

Import Numpy Library

Before going into the examples, let us first import the numpy library as below.

In [0]:

import numpy as np

 

Ad
Deep Learning Specialization on Coursera

Example 1 – Simple Example of Numpy Linspace

Let us begin with the simplest example where we are using only start and stop parameters in np.linspace function.

In this example, the start =1 and stop=10 are passed, and a 1-D numpy array is generated consisting of 50 evenly spaced numbers. (Do remember that when the parameter num is not used, it produces an array of 50 elements by default.)

In [1]:

arr = np.linspace(start = 1, stop = 10);

print(arr)

Out[1]:

[ 1.          1.18367347  1.36734694  1.55102041  1.73469388  1.91836735
  2.10204082  2.28571429  2.46938776  2.65306122  2.83673469  3.02040816
  3.20408163  3.3877551   3.57142857  3.75510204  3.93877551  4.12244898
  4.30612245  4.48979592  4.67346939  4.85714286  5.04081633  5.2244898
  5.40816327  5.59183673  5.7755102   5.95918367  6.14285714  6.32653061
  6.51020408  6.69387755  6.87755102  7.06122449  7.24489796  7.42857143
  7.6122449   7.79591837  7.97959184  8.16326531  8.34693878  8.53061224
  8.71428571  8.89795918  9.08163265  9.26530612  9.44897959  9.63265306
  9.81632653 10.        ]

 

Example 2 – Using Num Parameter

The num parameter is used to denote that how many elements would be there in the numpy array. In the below example, num=4 is passed and the output numpy array contains only 4 elements between 0 and 24.

In [2]:

arr = np.linspace(start = 0, stop = 24, num=4);

print(arr)

Out[2]:

[ 0.,  8., 16., 24.]

Example 3 – Using dtype Parameter

dtype parameter is used to explicitly assign the data type to the numpy array. In the above example, the output array consists of float. Let us apply dtype as int and we can see in the output that it now contains numbers as int.

In [3]:

arr = np.linspace(start = 0, stop = 24, num=4, dtype=int);

print(arr)
Out[3]:
[ 0 8 16 24]

Example 4 – Using endpoint in np.linspace

When endpoint parameter is kept True, the numbers generated the number in stop parameter. But when parameter is kept False it excludes the stop number as shown in the below example.
In [4]:
arr = np.linspace(start = 0, stop = 24, num=4, endpoint=False, dtype=int);

print(arr)

Out[4]:

[ 0 6 12 18]

Example 5 – Using retstep Parameter

When retstep parameter is kept True in np.linspace, it returns the step size used to create the samples along with the numpy array.
In the below example, it can be seen that along with the numpy array, linspace has also returned the step size 6 that was used to generate the numbers inside the array.
In [5]:
arr = np.linspace(start = 0, stop = 24, num=4, endpoint=False, dtype=int, retstep=True);

print(arr)

Out[5]:

(array([ 0, 6, 12, 18]), 6.0)

Example 6 – Using Linspace for creating 2-D Numpy Array

To create the 2-D numpy array with linspace you can use a vector of numbers in either start or stop or both the parameters as shown in the below examples

In [6]:

arr = np.linspace(start = (3,9), stop = 24, num=4,dtype=int);

print(arr)
[[ 3  9]
 [10 14]
 [17 19]
 [24 24]]

 

In [7]:

arr = np.linspace(start = (3,9), stop = (24,48), num=4,dtype=int);

print(arr)

Out[7]:

[[ 3  9]
 [10 22]
 [17 35]
 [24 48]]

 

In [8]:

arr = np.linspace(start = 9, stop = (24,48), num=4,dtype=int);

print(arr)

Out[8]:

[[ 9  9]
 [14 22]
 [19 35]
 [24 48]]

 

Reference – Numpy Documentation

LEAVE A REPLY

Please enter your comment!
Please enter your name here