Python – numpy.logspace


numpy.logspace returns a set of numbers spaced evenly on a log scale. Its syntax is as follows −

numpy.logspace(start, stop, num = 50, endpoint = True/False, base = 10.0, dtype = None)

Parameters

The logspace function can accept the following parameters −

  • start − Start of the sequence; default is zero.

  • stop − Endpoint of the sequence.

  • num − Number of elements to be generated between the start and stop sequence.

  • endpoint − It controls whether the stop value is included in the output array or not. If the endpoint is True, then the stop parameter is included as the last item in the nd.array. If endpoint=false, then the stop parameter is not included.

  • base − Base of the logspace. By default, it is 10.0.

  • dtype − It describes the type of output array.

Example 1

Let us consider the following example −

# Import the required library
import numpy as np

# logspace() function
x = np.logspace(start = 1, stop = 8, endpoint = False)
print ("logspace of X : \n", x)

Output

The above program will generate the following output −

logspace of X :
 [1.00000000e+01 1.38038426e+01 1.90546072e+01 2.63026799e+01
 3.63078055e+01 5.01187234e+01 6.91830971e+01 9.54992586e+01
 1.31825674e+02 1.81970086e+02 2.51188643e+02 3.46736850e+02
 4.78630092e+02 6.60693448e+02 9.12010839e+02 1.25892541e+03
 1.73780083e+03 2.39883292e+03 3.31131121e+03 4.57088190e+03
 6.30957344e+03 8.70963590e+03 1.20226443e+04 1.65958691e+04
 2.29086765e+04 3.16227766e+04 4.36515832e+04 6.02559586e+04
 8.31763771e+04 1.14815362e+05 1.58489319e+05 2.18776162e+05
 3.01995172e+05 4.16869383e+05 5.75439937e+05 7.94328235e+05
 1.09647820e+06 1.51356125e+06 2.08929613e+06 2.88403150e+06
 3.98107171e+06 5.49540874e+06 7.58577575e+06 1.04712855e+07
 1.44543977e+07 1.99526231e+07 2.75422870e+07 3.80189396e+07
 5.24807460e+07 7.24435960e+07]

Example 2

Let us take another example. consider the following example −

# Import numpy
import numpy as np

# logspace() function
x = np.logspace(start = 2, stop = 4, num = 4, base = 3.0)
print ("logspace of X :\n", x)

Output

It will generate the following output −

logspace of X :
[ 9. 18.72075441 38.9407384 81. ]

Here, we have num=4, so it generates only 4 elements in between start and stop. And we have taken the base as 3.0 instead of the default 10.0.

Updated on: 03-Mar-2022

203 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements