Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
NumPy logspace()
In this tutorial, you will learn about the numpy.logspace() method with the help of examples.
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 sequencestop- the end value of the sequencenum(optional)- number of samples to generateendpoint(optional)- specifies whether to include end valuedtype(optional)- type of output arraybase(optional)- base of log scaleaxis(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
dtypeis 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]]