Return evenly spaced numbers over a specified interval in Numpy


To return evenly spaced numbers over a specified interval, use the numpy.linspace() method in Python Numpy. The 1st parameter is the "start" i.e. the start of the sequence. The 2nd parameter is the "end" i.e. the end of the sequence. The 3rd parameter is the num i.e. the number of samples to generate.

The stop is the end value of the sequence, unless endpoint is set to False. In that case, the sequence consists of all but the last of num + 1 evenly spaced samples, so that stop is excluded. Note that the step size changes when endpoint is False.

The dtype is the type of the output array. If dtype is not given, the data type is inferred from start and stop. The inferred dtype will never be an integer; float is chosen even if the arguments would produce an array of integers. The axis in the result to store the samples. Relevant only if start or stop are array-like. By default (0), the samples will be along a new axis inserted at the beginning. Use -1 to get an axis at the end.

Steps

At first, import the required library −

import numpy as np

Return evenly spaced numbers over a specified interval, use the numpy.linspace() method in Python Numpy −

arr = np.linspace(10, 20)
print("Array...
", arr)

Get the type−

print("
Type...
", arr.dtype)

Get the dimensions −

print("
Dimensions...
",arr.ndim)

Get the shape −

print("
Shape...
",arr.shape)

Get the number of elements −

print("
Number of elements...
",arr.size)

Example

import numpy as np

# To return evenly spaced numbers over a specified interval, use the numpy.linspace() method in Python Numpy
# The 1st parameter is the "start" i.e. the start of the sequence
# The 2nd parameter is the "end" i.e. the end of the sequence
# The 3rd parameter is the num i.s the number of samples to generate. Default is 50.
arr = np.linspace(10, 20)
print("Array...
", arr) # Get the type print("
Type...
", arr.dtype) # Get the dimensions print("
Dimensions...
",arr.ndim) # Get the shape print("
Shape...
",arr.shape) # Get the number of elements print("
Number of elements...
",arr.size)

Output

Array...
[10.        10.20408163 10.40816327 10.6122449  10.81632653 11.02040816
11.2244898  11.42857143 11.63265306 11.83673469 12.04081633 12.24489796
12.44897959 12.65306122 12.85714286 13.06122449 13.26530612 13.46938776
13.67346939 13.87755102 14.08163265 14.28571429 14.48979592 14.69387755
14.89795918 15.10204082 15.30612245 15.51020408 15.71428571 15.91836735
16.12244898 16.32653061 16.53061224 16.73469388 16.93877551 17.14285714
17.34693878 17.55102041 17.75510204 17.95918367 18.16326531 18.36734694
18.57142857 18.7755102  18.97959184 19.18367347 19.3877551  19.59183673
19.79591837 20. ]

Type...
float64

Dimensions...
1

Shape...
(50,)

Number of elements...
50

Updated on: 08-Feb-2022

169 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements