Lessons
NumPy Tutorial
NumPy linspace in Python
What is NumPy linspace in Python?
NumPy linspace()
function is a powerful method for generating evenly spaced values over a specified range. Unlike numpy.arange()
, which requires a step size, numpy.linspace()
allows users to define the number of points needed between a start and stop value. This function is particularly useful in scientific computing, data visualization, and machine learning applications where precise numerical ranges are essential.
Why Use NumPy linspace() Function?
Using numpy.linspace()
is beneficial when:
- You need a specific number of evenly spaced values between two endpoints.
- The step size is not known in advance.
- Working with mathematical functions, plotting graphs, or interpolation tasks.
- Generating test data for simulations.
For example, numpy.linspace(0, 10, 5)
generates five evenly spaced numbers between 0 and 10, making it easier to work with numerical data without manually calculating step sizes.
Syntax of NumPy linspace
The basic syntax of numpy.linspace()
is:
python
1numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
NumPy linspace() Arguments
start
(required): The beginning of the interval.stop
(required): The end of the interval.num
(optional, default=50): The number of evenly spaced values to generate.endpoint
(optional, default=True): IfTrue
, includesstop
in the sequence; ifFalse
,stop
is excluded.retstep
(optional, default=False): IfTrue
, returns the step size between values along with the array.dtype
(optional): Specifies the data type of the output array.axis
(optional, default=0): Defines the axis along which values are stored when using multidimensional arrays.
NumPy linspace() Return Type
The numpy.linspace()
function returns a NumPy array with evenly spaced values. If retstep=True
, it also returns the calculated step size as a separate value.
Including or Excluding the Stop Value
By default, numpy.linspace()
includes the stop value. To exclude it, set endpoint=False
.
Example:
python
1import numpy as np
2arr = np.linspace(0, 10, 5, endpoint=False)
3print(arr)
Output:
python
1[0. 2. 4. 6. 8.]
Explanation:
- The function generates 5 evenly spaced values between
0
and10
, but sinceendpoint=False
, the10
is excluded. - The output contains floating-point numbers (e.g.,
0.
,2.
,4.
) because NumPy defaults to floating-point representation, ensuring precision in numerical calculations.
Examples of Using NumPy linspace()
Let's explore different examples with Numpy
linspace()
:
Example 1: Create 1-Dimensional Arrays with linspace()
python
1import numpy as np
2# Create 5 evenly spaced values between 1 and 10.
3arr = np.linspace(1, 10, 5)
4print(arr)
Output:
1[ 1. 3.25 5.5 7.75 10. ]
Explanation:
- This generates 5 values evenly spaced between
1
and10
, including both endpoints. - The step size is: (10−1)/(5−1)=9/4=2.25
- The values are:
1.0
(start)3.25
5.5
7.75
10.0
(stop)
- Floating-point representation is necessary because the numbers in the sequence are not all integers.
Example 2: Create N-Dimensional Arrays with linspace()
By specifying an axis, you can generate multidimensional arrays.
python
1import numpy as np
2# Generate 6 evenly spaced values between 0 and 10 and reshape into 2x3.
3arr = np.linspace(0, 10, 6).reshape(2, 3)
4print(arr)
Output:
1[[ 0. 2. 4. ]
2 [ 6. 8. 10. ]]
Explanation:
- This generates 6 values from
0
to10
and reshapes them into a 2x3 matrix. - The step size is: (10−0)/(6−1)=10/5=2
- The values are:
[0., 2., 4., 6., 8., 10.]
reshaped into:
1[[ 0. 2. 4. ]
2 [ 6. 8. 10. ]]
- Even though
0, 2, 4, 6, 8, 10
are whole numbers, they are represented in floating-point format (e.g.,0.
,2.
) for precision consistency.
Alternatives to NumPy linspace()
There are alternative functions in NumPy that provide similar functionality but with different control mechanisms.
numpy.arange()
- Used when step size is known rather than the number of values.
python
1import numpy as np
2# Generate values from 0 to 10 with a step size of 2.
3arr = np.arange(0, 10, 2)
4print(arr)
Output:
1[ 0 2 4 6 8 ]
Explanation:
numpy.arange(start, stop, step)
generates values based on a fixed step size.- Since all values are whole numbers, NumPy defaults to an integer array, so no decimal points appear.
2. numpy.geomspace()
- Generates logarithmically spaced values instead of linear spacing.
python
1import numpy as np
2# Generate 4 values spaced logarithmically between 1 and 1000.
3arr = np.geomspace(1, 1000, num=4)
4print(arr)
Output:
text
1[ 1. 10. 100. 1000. ]
Explanation:
numpy.geomspace()
generates values spaced logarithmically rather than linearly.- The output contains floating-point values to maintain precision.
3. Python’s range()
- Works for integer sequences but lacks floating-point support.
python
1# Generate integer values from 0 to 10 with a step of 2.
2arr = list(range(0, 10, 2))
3print(arr)
Output:
1[0, 2, 4, 6, 8]
Explanation:
- Python’s built-in
range()
only supports integers, so no decimal points appear.
Key Summary
numpy.linspace()
generates evenly spaced values between a givenstart
andstop
.- Unlike
numpy.arange()
, which requires a step size,linspace()
specifies the number of values needed. - The
endpoint
parameter controls whether thestop
value is included. - The function is useful in data science, plotting, and numerical computing.
- Alternatives include
numpy.arange()
,numpy.geomspace()
, and Python’srange()
.