Numpy logspace() Function



The Numpy logspace() function is used to generate an array of numbers spaced evenly on a logarithmic scale. It is useful for creating ranges that increase exponentially rather than linearly, which is common in many scientific and engineering applications.

Using numpy.logspace(), we can specify the starting and ending exponents, as well as the base of the logarithm. This is especially useful when we need values that cover a wide range.

Syntax

Following is the syntax of the Numpy logspace() function −

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

Parameters

Following are the parameters of the Numpy logspace() function −

  • start - starting exponent for the range (basestart).
  • stop - ending exponent for the range (basestop).
  • num - number of samples to generate. Default is 50.
  • endpoint - If True, the stop value is included in the range otherwise, the stop value is excluded. Default is True.
  • base - base of the logarithmic scale. Default is 10.0.
  • dtype - It is the desired data type for the array.

Return Values

This function returns a numpy array of numbers spaced evenly on a log scale from basestart to basestop.

Example

Following is a basic example to generate a numpy array using Numpy logspace()

import numpy as np
logspace_array = np.logspace(0, 3, num=5)
print("Logarithmic space array:\n", logspace_array)

Output

The output will show 5 values spaced on a logarithmic scale:

Logarithmic space array:
 [   1.            5.62341325   31.6227766   177.827941   1000.  ]

Example : Changing the base in 'logspace()'

In the numpy.logspace() function, the base parameter can take any positive number as its value. Commonly used bases include −

  • Base 10 − Default base in numpy.logspace(), creating scales in powers of 10. For an example, (10^0), (10^1), (10^2), etc.
  • Base 2 − This base is useful for binary or exponential growth patterns. For an example, (2^0), (2^1), (2^2), etc.
  • Base e − Euler's number (approximately 2.718), useful in natural logarithmic scales, common in scientific applications (e.g., (e^0), (e^1), (e^2)).
  • Custom Bases − It can be any positive real number can be used to suit specific requirements. For example, a base of 1.5 or 3 could be used to model finer or coarser exponential growth, respectively.

This range of values allows for great flexibility in generating logarithmic scales suited to diverse contexts and applications.

In this example, we use a base of 2 to create a logarithmic scale from (2^0) to (2^4) −

import numpy as np
logspace_base2 = np.logspace(0, 4, num=5, base=2)
print("Logarithmic space array with base 2:\n", logspace_base2)

Output

The output will display values spaced on a logarithmic scale with base 2:

Logarithmic space array with base 2:
 [ 1.  2.  4.  8. 16.]

Example : Specifying Data Type in 'logspace()'

The numpy.logspace() function allows us to generate a NumPy array of values evenly spaced on a logarithmic scale. Additionally, we can specify the data type of the output array using the dtype parameter, ensuring that the generated values match the desired precision, such as float or integer.

In the following example, we have generated a numpy array with integer values on the logarithmic scale value by setting the dtype parameter to int −

import numpy as np
logspace_int = np.logspace(1, 3, num=4, dtype=int)
print("Logarithmic space array with integer data type:\n", logspace_int)

Output

Following is the output of the above code −

Logarithmic space array with integer data type:
 [  10   46  215 1000]

Example : Excluding Endpoint in 'logspace()'

The numpy.logspace() function provides an option to exclude the endpoint of the logarithmic sequence using the endpoint parameter. By default, endpoint=True, which means the final value in the specified range will be included. if endpoint=False is set, the final value will be excluded, which gives more control over the spacing of values. This can be useful when creating ranges for iterations or sampling.

In this example, we generate a sequence of values from (10^1) to (10^3) but exclude the endpoint (103) by setting endpoint=False

import numpy as np
logspace_no_endpoint = np.logspace(1, 3, num=4, endpoint=False)
print("Logarithmic space array without endpoint:\n", logspace_no_endpoint)

Output

Following is the output of the above code −

Logarithmic space array without endpoint:
 [ 10.          31.6227766  100.         316.22776602]
numpy_array_creation_routines.htm
Advertisements