Return evenly spaced numbers over a specified interval and do not set the endpoint in Numpy

NumpyServer Side ProgrammingProgramming

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 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 using the numpy.linspace() method. 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. Default is 50. The 4th parameter is the "endpoint". If True, stop is the last sample. Otherwise, it is not included. Default is True −

arr = np.linspace(100, 200, num = 10, endpoint = False)
print("Array...\n", arr)

Get the array type −

print("\nType...\n", arr.dtype)

Get the dimensions of the Array −

print("\nDimensions...\n",arr.ndim)

Get the shape of the Array −

print("\nShape...\n",arr.shape)

Get the number of elements −

print("\nNumber of elements...\n",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.e the number of samples to generate. Default is 50.
# The 4th parameter is the "endpoint". If True, stop is the last sample. Otherwise, it is not included. Default is True.
arr = np.linspace(100, 200, num = 10, endpoint = False)
print("Array...\n", arr)

# Get the array type
print("\nType...\n", arr.dtype)

# Get the dimensions of the Array
print("\nDimensions...\n",arr.ndim)

# Get the shape of the Array
print("\nShape...\n",arr.shape)

# Get the number of elements
print("\nNumber of elements...\n",arr.size)

Output

Array...
[100. 110. 120. 130. 140. 150. 160. 170. 180. 190.]

Type...
float64

Dimensions...
1

Shape...
(10,)

Number of elements...
10
raja
Updated on 08-Feb-2022 07:03:07

Advertisements