w3resource

NumPy: Create a 1-D array of 20 element spaced evenly on a log scale between 2. and 5., exclusive


20 Log-Spaced Values (2–5)

Write a NumPy program to create a 1-D array of 20 elements spaced evenly on a log scale between 2. and 5., exclusive.

Sample Solution:

Python Code:

# Importing the NumPy library with an alias 'np'
import numpy as np

# Generating an array of 20 values logarithmically spaced between 10^2 and 10^5 (excluding the endpoint)
x = np.logspace(2., 5., 20, endpoint=False)

# Printing the generated array
print(x)

Sample Output:

[   100.            141.25375446    199.5262315     281.83829313       
    398.10717055    562.34132519    794.32823472   1122.0184543        
   1584.89319246   2238.72113857   3162.27766017   4466.83592151       
   6309.5734448    8912.50938134  12589.25411794  17782.79410039       
  25118.8643151   35481.33892336  50118.72336273  70794.57843841]

Explanation:

In the above code -

x = np.logspace(2., 5., 20, endpoint=False): This line creates a NumPy array ‘x’ using the np.logspace() function. The function takes four arguments: the start value (2.0), the stop value (5.0), the number of samples to generate (20), and whether to include the endpoint (False). It generates a sequence of 20 values that are evenly spaced on a logarithmic scale between 10^2 (100) and 10^5 (100000), excluding the endpoint.


For more Practice: Solve these Related Problems:

  • Generate a 1D array of 20 logarithmically spaced values between 10^2 and 10^5 using np.logspace.
  • Create a function that returns a log-spaced array and then computes the logarithm of each element to verify spacing.
  • Validate that each value in the array is a power of 10 and that the sequence is strictly increasing.
  • Compare the output of np.logspace with a manually computed logarithmic scale to ensure accuracy.

Go to:


PREV : 30 Evenly Spaced Values (2.5–6.5)
NEXT : Create Zero/One Array Template


Python-Numpy Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.