Understand numpy.logspace() Function for Beginners – NumPy Tutorial

By | September 15, 2019

numpy.logspace() function is defined as:

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

This function can generate geometric series based on base( such as base = 10.0).

In this tutorial, we will write some examples to show you how to use this function correctly.

Parameters:

start: the start value of the sequence, it is equal to basestart

stop: the end value of the sequence, it is equal to basestop

num: integer, optional, Number of samples to generate. Default is 50.

endpoint: boolean, optional
If true, stop is the last sample. Otherwise, it is not included. Default is True.

base: float, optional
The base of the log space. Default is 10.0.

dtype: dtype
The type of the output array.

axis : int, optional
The axis in the result to store the samples.

Returns:
samples : ndarray

Here is an simple example to show how to use this function.

import numpy as np
x = np.logspace(3,5,8,base=2)
print(x)

The result is:

[ 8.          9.75210923 11.88795431 14.49157863 17.66543222 21.53440308
 26.25073139 32.        ]

Because endpoint = True, so the mininum value is 23 = 8 and the maximum is 28= 32.

How about is endpoint = False?

x = np.logspace(3,5,8,base=2, endpoint = False)
print(x)

The value is:

[ 8.          9.51365692 11.3137085  13.45434264 16.         19.02731384
 22.627417   26.90868529]

Why the maximum value is 26.90868529?

We should comfirm the step size between start and end by num.

When endpoint = False, the step size = (end – start) / num.

For example:

As to:

x = np.logspace(3,5,8,base=2, endpoint = False)

The step size is (5-3)/8 = 0.25

np.logspace() function calculate squence based on: [3. 3.25 3.5 3.75 4. 4.25 4.5 4.75]

so the value of sequence is:

[ 8.          9.51365692 11.3137085  13.45434264 16.         19.02731384
 22.627417   26.90868529]

Leave a Reply

Your email address will not be published. Required fields are marked *