Numpy Linspace in Python featured image

Numpy Linspace in Python: Know np.linspace() in 4 Examples

Numpy is the best python module for array creation and manipulation. In this entire tutorial you will learn numpy linspace implementation with examples.

What does np linspace do ?

Numpy linspace creates an array whose elements are evenely spaced between the two interverals. Here you specify a starting point and end point. Then the elements are breaked according with the the line break( integer type).The np.linspace() method has more  than 6 parameters. Below is the syntax and parameters explaination of this function.

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

parameters:

start: The starting value of the sequence.

stop : The end value of the sequence.

num: The number of samples you want. Default is 50. It should be negative.

endpoint: The default is true. If it is true then stop is the last sample, otherise stop is not included.

retstep: Step size.

dtype: Type of the returned array.

axis : The axis in the result to store the samples.

Examples: How to use and apply Numpy linspace

Example 1 : Return a numpy array between two interveral

Suppose I want to return numpy array(ndarray object) between the two intervals. Then I will use the following code.

np.linspace(start=10,stop=100,num=10)

Here I am finding 10 elements between 10 and 100 with step size or space of 10.

You will get the following output.

numpy array elements between two interveral
Returning numpy array elements between two interveral

If you remove num = 10, then all the elements between the interval will be returned.

Example 3:  Return a numpy array of custom data type.

You can also define the type of the returned numpy array. To do so you have to pass dtype as an argument inside the np.linspace() method.

np.linspace(start=10,stop=100,num=10,dtype="int")

The code will output the elements between the interveal of 10 and 100 and of integer type.

Output

Returning a numpy array of custom data type
Returning a numpy array of custom data type

Example 3:  Return a numpy array between two interval and step size.

The above example returned only numpy array of equally spaced elements. But it has not told you about the spacing between two consecutive elements. To do so you have to you have to pass the retstep to true.

Execute the following code.

np.linspace(start=10,stop=100,num=10, retstep=True)

The above code will return two elements of ndarray type. The first one is the arry of the 10 elements and the second is the step size. In our example it returns 10.

Below is the output for the above code.

Returning numpy array between two interveral and step size
Returning numpy array between two interveral and step size

 

Example 4: How to use the endpoint parameter

In all the above examples you can see the end elements had been added the last element. Suppose I do not want it. To remove it you have to pass the endpoint as False.

import numpy as np
np.linspace(start=10,stop=100,num=10,endpoint=False)

Output

Returning a numpy array between intervals without end value
Returning a numpy array between intervals without end value

You can see I am getting all the elements between 10 to 100 of 10 step size but without the last element 100.

Difference Between np.linspace and np.arange

If you are already familiar with np.arange then you must be thinking numpy linspace is similar to to numpy.arange. Yes you are correct. But the major difference between them is that np.arange simply creates evenly spaced array.But the np.linspace allows more controling over increments of the element. And also it allows you to include or exclude the last element.

 

Hope this article on numpy linspace in python has helped you to know more about it. If you want to us to solve your queries then you can contact us.

Thanks

Data Science Learner Team

 

Source:

Offical np.linspace() Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner