Numpy Logspace Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn about the numpy.logspace() function of the Numpy library. This function is used to create an array by using the numbers that are evenly separated on a logarithmic scale.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/booleans -.-> lab-86475{{"`Numpy Logspace Function`"}} python/lists -.-> lab-86475{{"`Numpy Logspace Function`"}} python/tuples -.-> lab-86475{{"`Numpy Logspace Function`"}} python/importing_modules -.-> lab-86475{{"`Numpy Logspace Function`"}} python/numerical_computing -.-> lab-86475{{"`Numpy Logspace Function`"}} python/data_visualization -.-> lab-86475{{"`Numpy Logspace Function`"}} python/build_in_functions -.-> lab-86475{{"`Numpy Logspace Function`"}} numpy/bool_idx -.-> lab-86475{{"`Numpy Logspace Function`"}} numpy/fancy_idx -.-> lab-86475{{"`Numpy Logspace Function`"}} end

Syntax

The syntax to use this function is as follows:

numpy.logspace(start, stop, num, endpoint, base, dtype)

Parameters:

  • start: This parameter represents the starting value of the interval in the base.
  • stop: This parameter represents the stopping value of the interval in the base.
  • num: This parameter indicates the number of values between the range.
  • endpoint: This parameter's value is in boolean and it is used to make the value represented by stop as the last value of the interval.
  • base: This parameter represents the base of the log space.
  • dtype: This parameter represents the data type of the array items.

Returned Values:

This function will return the array in the specified range.

Example 1

Here is an example of code snippet where we will use this function:

import numpy as np

arr = np.logspace(20, 30, num=7, base=4, endpoint=True)
print("The array over the given range is ")
print(arr)

Output:

The array over the given range is
[1.09951163e+12 1.10823828e+13 1.11703419e+14 1.12589991e+15
    1.13483599e+16 1.14384301e+17 1.15292150e+18]

Example 2

In this example, we will cover the graphical representation of numpy.logspace() function using matplotlib:

import numpy as np
import matplotlib.pyplot as plt

N = 20
x1 = np.logspace(0.1, 1, N, endpoint=True)
x2 = np.logspace(0.1, 1, N, endpoint=False)
y = np.zeros(N)

plt.plot(x1, y, 'o')
plt.plot(x2, y + 0.8, 'o')
plt.ylim([-0.5, 1])
plt.show()

Summary

In this lab, we covered the numpy.logspace() function of the Numpy library. We learned its syntax, parameters as well as the value returned by this function along with multiple code examples.

Other Python Tutorials you may like