Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python – numpy.geomspace
numpy.geomspace() returns a set of numbers spaced evenly on a log scale (a geometric progression). This function is useful for creating exponentially spaced arrays where each element is a constant multiple of the previous one.
Key differences from similar functions ?
Linspace − Creates linearly spaced numbers between two endpoints
Logspace − Creates logarithmically spaced numbers using base and power endpoints
Geomspace − Creates geometrically spaced numbers using actual start and stop values
Syntax
numpy.geomspace(start, stop, num=50, endpoint=True, dtype=None)
Parameters
The above function accepts the following parameters ?
start − Starting value of the geometric sequence (must be non-zero)
stop − End value of the geometric sequence
num − Number of elements to generate between start and stop (default: 50)
endpoint − If True, stop is the last sample. If False, stop is not included (default: True)
dtype − Data type of the output array
Example 1: Basic Usage
Creating a geometric sequence from 1 to 2000 with 8 elements ?
import numpy as np
# Create geometric sequence
x = np.geomspace(1, 2000, num=8)
print("geomspace of X:")
print(x)
geomspace of X: [1.00000000e+00 2.96193630e+00 8.77306662e+00 2.59852645e+01 7.69666979e+01 2.27970456e+02 6.75233969e+02 2.00000000e+03]
Example 2: Using endpoint=False
Creating a sequence where the stop value is not included ?
import numpy as np
# geomspace with endpoint=False
x = np.geomspace(2, 800, num=9, endpoint=False)
print("geomspace of X:")
print(x)
geomspace of X: [ 2. 3.89177544 7.57295802 14.73612599 28.67484658 55.79803176 108.57670466 211.27807602 411.12341312]
Notice that 800 is not included in the output since endpoint=False.
Example 3: Negative Values
Working with negative geometric progressions ?
import numpy as np
# Geometric sequence with negative values
x = np.geomspace(-1, -1000, num=5)
print("Negative geomspace:")
print(x)
Negative geomspace: [ -1. -5.62341325 -31.6227766 -177.827941 -1000. ]
Comparison with Other Functions
| Function | Spacing Type | Best For |
|---|---|---|
linspace() |
Linear (arithmetic) | Even intervals |
logspace() |
Logarithmic (powers of base) | Scientific notation ranges |
geomspace() |
Geometric (constant ratio) | Exponential growth/decay |
Conclusion
NumPy's geomspace() is ideal for creating exponentially spaced arrays where you need a constant multiplicative ratio between consecutive elements. Use it when modeling exponential growth, decay, or when you need logarithmic scaling with specific start and end values.
