In Python, numpy.linspace()
is a function that is used to create a NumPy array of evenly spaced numbers over a specified interval.
This function is often used in mathematical and scientific calculations, as it allows you to easily generate a sequence of numbers that can be used as input to other functions or algorithms.
Here is the syntax for numpy.linspace()
:
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
This function takes the following arguments:
start
: The starting value of the sequence.stop
: The ending value of the sequence.num
(optional): The number of evenly spaced values to generate. The default is 50
.endpoint
(optional): Whether to include the stop value in the sequence. The default is True
, which means that the stop value will be included. If False
, the stop value will not be included.retstep
(optional): Whether to return the step size used to generate the sequence. The default is False
, which means that the step size will not be returned. If True
, the step size will be returned as the second element of the tuple returned by numpy.linspace().dtype
(optional): The data type of the array. This specifies the type of the array elements. The default is None
, which means that the data type will be inferred from the other arguments. You can also specify a specific data type, such as float for floating-point numbers or int for integers.import numpy as np
# Create an array of evenly spaced numbers over the interval [0, 5] with 10 elements
numbers = np.linspace(0, 5, 10)
print(numbers) # [0. 0.56 1.12 1.68 2.24 2.8 3.36 3.92 4.48 5. ]
# Create an array of evenly spaced numbers over the interval [0, 5] with 5 elements
numbers = np.linspace(0, 5, 5)
print(numbers) # [0. 1.25 2.5 3.75 5. ]
# Create an array of evenly spaced numbers over the interval [2, 10] with 7 elements
numbers = np.linspace(2, 10, 7)
print(numbers) # [ 2. 3.6 5.2 6.8 8.4 10. ]
Related tutorials curated for you
NumPy: np.arrange()
How to use numpy.vstack() in Python
How to use numpy.round() in Python
How to use numpy.zeros() in Python
How to use numpy.random.uniform() in Python
How to use numpy.clip() in Python
How to use numpy.linspace() in Python
ModuleNotFoundError: No module named 'numpy' in Python
NumPy numpy.transpose()
How to use numpy.ravel() in Python
How to use numpy.percentile() in Python
What does numpy.arrange() do?