BetterDocs
Home
Docs

Creation | np.linspace()

Method:

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

Creates a NumPy array of values evenly spaced over a specified interval.

Returns:

(np.ndarray, step)

Parameters:

start: int-

Starting value of the NumPy array.

import numpy as np
linspace_array = np.linspace(start=0, stop=2, num=5)
# Both 'start' & 'stop' are included as endpoint=True
print(linspace_array)
'''
Output:
[0.  0.5 1.  1.5 2. ]
'''

stop: int-

End of the interval value of the NumPy array.

import numpy as np
linspace_array = np.linspace(start=0, stop=2, num=5)
# Both 'start' & 'stop' are included as endpoint=True
print(linspace_array)
'''
Output:
[0.  0.5 1.  1.5 2. ]
'''

If stop > start, an array is returned with data in decreasing order.

If stop < start, an array is returned with data in ascending order.

If stop = start, an array of 0 is returned.

num: int-

Number of the intervals in the NumPy array.

import numpy as np
linspace_array = np.linspace(start=0, stop=2, num=5)
# Both 'start' & 'stop' are included as endpoint=True
print(linspace_array)
'''
Output:
[0.  0.5 1.  1.5 2. ]
'''

endpoint: (True or False) Optional-

If endpoint is set to True, stop value is included in the returned NumPy array.

endpoint = True (default) +

endpoint = False +

retstep: (True or False) Optional-

If retstep is set to True, it returns the step size along with NumPy array.

retstep = False (default) +

retstep = True +

If retstep=True, you can access the returned np.ndarray at index 0 & step at index 1.

dtype: np.float64, Optional-

The desired data-type for the NumPy array is inferred from the dtype specified.

import numpy as np
linspace_array = np.linspace(start=0, stop=4, num=5, dtype=np.int32)
# Both 'start' & 'stop' are included
print(linspace_array)
'''
Output:
[0 1 2 3 4]
'''

Values: +

By passing the dtype, you are implicitly converting the values to the passed dtype.

axis: int Optional-

The axis in the result to store the samples. Relevant only if start or stop are array-like.

axis = 0 (default) +

axis = -1 +

axis is only useful when dealing with array-like input.

Plays a vital role in transformation of the input along your axis (columns or rows).

device: None, Optional-

In NumPy 2.1, the device parameter in np.linspace() is still in an early stage and currently only accepts "cpu" as a valid option. At this time, it is primarily there for future development, allowing the function to eventually support GPUs or other devices, but for now, it doesn't support GPU IDs directly.


Logo

BetterDocs

Support

EmailDiscordForms

Documentations

Python

Company

AboutDocs

Policies

Terms of ServicePrivacy Policy