NumPy linspace in Python [7+ Examples]

This Python tutorial will explain what the NumPy linspace in Python is. What is its syntax, parameters required, and return values? We will also see some use cases where we will use different conditions to generate different arrays in Python.

NumPy linspace function creates an array in Python of evenly spaced numbers over a specified interval. It can used to create an array with only float values, only integer values, or complex values. We can also create a 2D array.

NumPy linspace in Python

In Python’s NumPy library, the linspace function is used to create an array of evenly spaced values over a specified interval.

The name “linspace” stands for “linearly spaced.” This function is particularly useful when we need to generate a specific number of points between a start and an endpoint, for example, when trying to create a range of x values for plotting a graph through Python.

Example: Let’s see one basic example to see what the linspace() function is in the Python NumPy library.

import numpy as np

linear_space = np.linspace(0, 1, num=10)
print(linear_space)

Output: The implementation of the code with the screenshot is mentioned below:

[0.         0.11111111 0.22222222 0.33333333 0.44444444 0.55555556
 0.66666667 0.77777778 0.88888889 1.        ]
NumPy linspace in Python

Now let’s see how the NumPy linspace() function works in Python systematically.

READ:  Python Tkinter Button – How to use

np.linspace Function Syntax

The basic syntax of the NumPy linspace function in Python is:

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

linspace Python NumPy parameters required

The parameters within the NumPy linspace in Python are:

NameDescription
startThe starting value of the sequence.
stopThe end value of the sequence, unless the endpoint is set to False. In that case, the stop is the value just before the end of the sequence.
numThe number of evenly spaced samples to generate. The default is 50.
endpointIf True (default), the stop is the last sample. Otherwise, it is not included.
retstepIf True, return (samples, step), where step is the spacing between samples.
dtypeThe type of the output array in Python. If dtype is not given, the data type of the output array is inferred from the start and stop. The inferred dtype will never be an integer; it will be at least float64.
axisThe axis in the result stores the samples. Relevant only if start or stop are array-like in Python.
List of parameters required in NumPy linspace in Python.

np.linspace Python Return Value

The numpy.linspace function returns an array of evenly spaced numbers over a specified interval in Python. The primary return value is a NumPy array.

linspace Function in Python NumPy Use Cases

We can obtain many different NumPy arrays in Python by giving different parameters. Let’s see them one by one as an example:

Case 1: Python np linspace with start, stop, and num parameter

Here, let’s take an example where we will take a basic parameter required in the np.linspace() function in Python.

import numpy as np

arr = np.linspace(0, 10, num=5)
print(arr)

Output: The implementation of the code is mentioned below:

[ 0.   2.5  5.   7.5 10. ]
linspace function in Python NumPy

This is the basic use of the NumPy linspace in Python with only start, stop, and num parameters.

READ:  Python Turtle Mouse + Multiple Examples

Case 2: linspace NumPy in Python with endpoint

Consider a situation where we have to put the endpoint=False parameter within the linspace() in Python NumPy.

import numpy as np

arr = np.linspace(0, 10, num=5, endpoint=False)
print(arr)

Output: The result after implementing the code is:

[0. 2. 4. 6. 8.]
python linspace in NumPy

This way we can use the endpoint=False within the NumPy linspace in Python.

Case 3: NumPy linspace float in Python

Floats are the most common data type used with linspace. This is because NumPy Python linspace() function inherently deals with the division of intervals, which often results in floating-point numbers:

import numpy as np

float_array = np.linspace(1.0, 2.0, num=10)
print(float_array)

Output: After the implementation of the Python code, we get:

[1.         1.11111111 1.22222222 1.33333333 1.44444444 1.55555556
 1.66666667 1.77777778 1.88888889 2.        ]
linspace in python NumPy

To get all the float values inside an array, we can use the NumPy linspace in Python.

Case 4: Python linspace integers

While np.linspace is designed for floating-point numbers in Python, it can be used to generate integers by setting the dtype to an integer type in Python.

import numpy as np

int_array = np.linspace(10, 50, num=5, dtype=int)
print(int_array)

Output: The output of the above Python code is:

[10 20 30 40 50]
np linspace only integers in Python

To get only the integer values inside an array, we can use the NumPy linspace in Python.

Case 5: np linspace complex in Python

The numpy.linspace can also handle complex numbers. This is particularly useful when dealing with applications in signal processing or complex analysis in Python.

import numpy as np

complex_array = np.linspace(1+1j, 10+10j, num=4)
print(complex_array)

Output: The implementation of the Python code with a screenshot is given below:

[ 1. +1.j  4. +4.j  7. +7.j 10.+10.j]
numpy linspace complex in Python

To get numbers between two complex values inside an array, we can use the NumPy linspace in Python.

READ:  Python Tkinter Progress bar widget - How to use

Case 6: np linspace 1D array in Python

Creating one-dimensional NumPy arrays is the default behavior of np.linspace() in Python.

import numpy as np

one_d_array = np.linspace(0, 1, num=20)
print(one_d_array)

Output: The result with a screenshot is mentioned below:

[0.         0.05263158 0.10526316 0.15789474 0.21052632 0.26315789
 0.31578947 0.36842105 0.42105263 0.47368421 0.52631579 0.57894737
 0.63157895 0.68421053 0.73684211 0.78947368 0.84210526 0.89473684
 0.94736842 1.        ]
np linspace 1D array in Python

To create a 1D array of numbers, we can use the NumPy linspace in Python.

Case 7: np linspace 2D array in Python

Although numpy.linspace inherently generates a 1D array in Python, we can easily reshape it to 2D or use it in combination with other functions to create 2D arrays in Python.

import numpy as np

two_d_array = np.linspace(0, 1, num=20).reshape(4, 5)
print(two_d_array)

Output: The implementation of the code is given below:

[[0.         0.05263158 0.10526316 0.15789474 0.21052632]
 [0.26315789 0.31578947 0.36842105 0.42105263 0.47368421]
 [0.52631579 0.57894737 0.63157895 0.68421053 0.73684211]
 [0.78947368 0.84210526 0.89473684 0.94736842 1.        ]]
numpy linspace 2d in Python

To create a 2D array of numbers, we can use the NumPy linspace in Python.

Conclusion

NumPy linspace in Python is a powerful and versatile tool that provides an easy way to generate linearly spaced values for various applications. It’s essential for creating test data, simulations, and finely-spaced grids for evaluations and visualizations.

By understanding how to apply np.linspace across different data types and dimensions in Python, we can harness its full potential for a wide range of scientific computing tasks.

You may also like to read: