30 Mins

4. Numerical Range Creation

Generating numerical intervals using np.arange

The np.arange function is the NumPy equivalent of the built-in Python range() function, but returns an ndarray instead of a list. It generates values within a half-open interval [start, stop) based on a specified step size.

The function syntax is np.arange([start,] stop, [step,], dtype=None). If the start is omitted, it defaults to 0. If the step is omitted, it defaults to 1. One critical difference from standard Python is that np.arange can accept float values for the step, which allows for finer control over precision.

Operator Note: Avoid using np.arange with floating-point steps if you need predictable results, as the number of elements can be inconsistent due to floating-point rounding errors.
Python 3
import numpy as np
# Generate range from 0 to 9 with step 1
arr1 = np.arange(10)
# Generate float range from 0.0 to 1.0 with step 0.2
arr2 = np.arange(0, 1.1, 0.2)
print(arr1)
print(arr2)

Linear coordinate generation using np.linspace

While np.arange is defined by the step size, np.linspace is defined by the number of samples. This function returns evenly spaced numbers over a specified interval [start, stop].

The syntax is np.linspace(start, stop, num=50, endpoint=True, retstep=False). By default, the endpoint is included in the sequence. This is extremely useful for plotting functions or evaluating models over a fixed range where you need a specific count of data points.

Operator Note: Setting endpoint=False excludes the stop value, making the interval similar to np.arange's half-open behavior.
Python 3
import numpy as np
# Create 5 evenly spaced points between 0 and 1
lin_arr = np.linspace(0, 1, 5)
print(lin_arr)

Logarithmic and geometric intervals: np.logspace and np.geomspace

When dealing with data that spans multiple orders of magnitude, linear spacing is insufficient. np.logspace and np.geomspace provide sequences that grow exponentially.

np.logspace returns numbers spaced evenly on a log scale, while np.geomspace returns numbers spaced evenly on a geometric progression.
FunctionDescription
np.logspaceCalculates log10(start) to log10(stop)
np.geomspaceCalculates geometric progression
Operator Note: Use np.logspace when defining frequency bands or signal processing parameters that require logarithmic scaling.
Python 3
import numpy as np
# 3 values between 10^1 and 10^3
log_vals = np.logspace(1, 3, 3)
# 3 values between 1 and 100 geometrically
geo_vals = np.geomspace(1, 100, 3)
print(log_vals)
print(geo_vals)

Meshgrids and coordinates creation: np.meshgrid

np.meshgrid is essential for vectorized coordinate generation. It takes two or more 1D coordinate arrays and produces N-D coordinate matrices. This is the cornerstone of plotting 3D surfaces and evaluating functions over a 2D grid.

For inputs x (length M) and y (length N), np.meshgrid returns two 2D arrays of shape (N, M) representing the coordinates for every point in the grid.

Operator Note: Always ensure you are using meshgrids when calculating results for a surface plot (e.g., z = f(x, y)) to avoid dimension mismatch errors.
Python 3
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5])
X, Y = np.meshgrid(x, y)
print('X grid:\n', X)
print('Y grid:\n', Y)

Knowledge Check

Ready to test your understanding of 4. Numerical Range Creation?

Back