NumPy logspace()

The logspace() method creates an array with evenly spaced numbers on a logarithm scale.

Example

import numpy as np

# create an array with 3 elements between 10^5 and 10^10 array1 = np.logspace(5, 10, 3)
print(array1) # Output: [1.00000000e+05 3.16227766e+07 1.00000000e+10]

logspace() Syntax

The syntax of logspace() is:

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

logspace() Argument

The logspace() method takes the following arguments:

  • start- the start value of the sequence
  • stop- the end value of the sequence
  • num(optional)- number of samples to generate
  • endpoint(optional)- specifies whether to include end value
  • dtype(optional)- type of output array
  • base(optional)- base of log scale
  • axis(optional)- the axis in the result to store the samples

Notes:

  • In linear space, the sequence generated by logspace() starts at base ** start (base to the power of start) and ends with base ** stop.
  • If dtype is omitted, logspace() will determine the type of the array elements from the types of other parameters.

logspace() Return Value

The logspace() method returns an array of evenly spaced values on a logarithmic scale.


Example 1: Create a 1-D Array Using logspace

import numpy as np
 
# create an array of 5 elements between 10^2 and 10^3 array1 = np.logspace(2.0, 3.0, num = 5)
print("Array1:", array1)
# create an array of 5 elements between 10^2 and 10^3 without including the endpoint array2 = np.logspace(2.0, 3.0, num = 5, endpoint = False)
print("Array2:", array2)
# create an array of 5 elements between 2^2 and 2^3 array3 = np.logspace(2.0, 3.0, num = 5, base = 2)
print("Array3:", array3)

Output

Array1: [ 100.          177.827941    316.22776602  562.34132519 1000.        ]
Array2: [100.         158.48931925 251.18864315 398.10717055 630.95734448]
Array3: [4.         4.75682846 5.65685425 6.72717132 8.        ]

Example 2: Create an N-d Array Using logspace

Similar to 1D arrays, we can also create N-d arrays using logspace. For this, we can simply pass a sequence to start and stop values instead of integers.

Let us look at an example.

import numpy as np
 
# create an array of 5 elements between [10^1, 10^2] and [10^5, 10^6]
array1 = np.logspace([1, 2], [5, 6], num=5)
print("Array1:")
print(array1)

# create an array of 5 elements between [1, 2] and [3, 4] along axis 1
array2 = np.logspace([1, 2], [5, 6], num=5, axis=1)
print("Array2:")
print(array2)

Output

Array1:
[[1.e+01 1.e+02]
 [1.e+02 1.e+03]
 [1.e+03 1.e+04]
 [1.e+04 1.e+05]
 [1.e+05 1.e+06]]
Array2:
[[1.e+01 1.e+02 1.e+03 1.e+04 1.e+05]
 [1.e+02 1.e+03 1.e+04 1.e+05 1.e+06]]

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges