Creating Intervals with Numpy

In numerical programming, it is often necessary to create evenly spaced numerical values or intervals. Numpy provides several useful functions for this purpose, such as arange, linspace, and logspace.

Bernd Fischer

Bernd Fischer

11. Oct 2024 · 3 min read

Creating Intervals with Numpy

Installing Numpy

If numpy is not yet installed, it can be installed via pip:

pip install numpy

1. Creating intervals with numpy.arange

The function numpy.arange is a simple method to generate evenly spaced values. It works similarly to the Python function range but creates a Numpy array.

Example: Creating an interval from 0 to 9 with arange

import numpy as np

# Ein Intervall von 0 bis 9 erstellen (Schrittweite 1)
intervall = np.arange(0, 10)

# Visualisierung des Intervalls
print("Intervall von 0 bis 9:\n", intervall)

Output:

Intervall von 0 bis 9:
 [0 1 2 3 4 5 6 7 8 9]

In this example, a closed interval from 0 to 9 is created. The right endpoint is not part of the interval because arange excludes the end element by default.

Example: Creating an interval with a custom step size

# Ein Intervall von 0 bis 10 erstellen, mit einer Schrittweite von 2
intervall_mit_schrittweite = np.arange(0, 10, 2)

# Visualisierung des Intervalls mit Schrittweite
print("Intervall von 0 bis 10 mit Schrittweite 2:\n", intervall_mit_schrittweite)

Output:

Intervall von 0 bis 10 mit Schrittweite 2:
 [0 2 4 6 8]

This example shows an interval from 0 to 10 with a step size of 2. The value 10 is not included, representing an open interval.

2. Evenly spaced values with numpy.linspace

The function numpy.linspace creates an interval containing a specified number of values that are evenly spaced between two boundaries. The difference to arange is that here the number of values to produce and the behavior of the endpoints can be defined.

Example: Creating an interval from 0 to 1 with 5 values (closed interval)

# Ein Intervall von 0 bis 1 erstellen, das aus 5 gleichmäßig verteilten Werten besteht
linspace_intervall = np.linspace(0, 1, 5)

# Visualisierung des linspace-Intervalls
print("Linspace Intervall von 0 bis 1 mit 5 Werten (geschlossenes Intervall):\n", linspace_intervall)

Output:

Linspace Intervall von 0 bis 1 mit 5 Werten (geschlossenes Intervall):
 [0.   0.25 0.5  0.75 1.  ]

By default, linspace creates a closed interval that includes the start and end points (0 and 1).

Example: Creating an open interval with linspace

# Ein Intervall von 0 bis 1 erstellen, das aus 5 gleichmäßig verteilten Werten besteht, wobei der Endpunkt ausgeschlossen ist
linspace_offenes_intervall = np.linspace(0, 1, 5, endpoint=False)

# Visualisierung des offenen Intervalls
print("Linspace Intervall von 0 bis 1 mit 5 Werten (offenes Intervall):\n", linspace_offenes_intervall)

Output:

Linspace Intervall von 0 bis 1 mit 5 Werten (offenes Intervall):
 [0.   0.2  0.4  0.6  0.8]

Using the option endpoint=False, the endpoint (in this case 1) is excluded, resulting in an open interval.

3. Logarithmically spaced values with numpy.logspace

The function numpy.logspace creates an interval in which the values are distributed logarithmically on a scale. This is particularly useful when exponential scales are needed.

Example: Logarithmic interval from 10^0 to 10^3 (closed interval)

# Ein logarithmisches Intervall von 10^0 bis 10^3 mit 4 Werten erstellen
logspace_intervall = np.logspace(0, 3, 4)

# Visualisierung des logspace-Intervalls
print("Logarithmisches Intervall von 10^0 bis 10^3 mit 4 Werten (geschlossenes Intervall):\n", logspace_intervall)

Output:

Logarithmisches Intervall von 10^0 bis 10^3 mit 4 Werten (geschlossenes Intervall):
 [   1.   10.  100. 1000.]

By default, logspace creates a closed interval that includes both the start and end points.

Example: Logarithmic interval from 10^0 to 10^3 (open interval)

# Ein logarithmisches Intervall von 10^0 bis 10^3 mit 4 Werten erstellen, Endpunkt ausschließen
logspace_offenes_intervall = np.logspace(0, 3, 4, endpoint=False)

# Visualisierung des logspace-Intervalls
print("Logarithmisches Intervall von 10^0 bis 10^3 mit 4 Werten (offenes Intervall):\n", logspace_offenes_intervall)

Output:

Logarithmisches Intervall von 10^0 bis 10^3 mit 4 Werten (offenes Intervall):
 [  1.   5.62341325  31.6227766  177.827941]

By using the option endpoint=False, the endpoint (here (10^3)) is excluded, creating an open interval.