In this tutorial, you’ll learn how to use the NumPy logspace function and how to use its different parameters. The
np.logspace() function is used to return numbers that are evenly spaced on a log scale. The function allows you to specify many different attributes, including modifying the base of the log scale you want to use.
By the end of this tutorial, you’ll have learned:
- What the
np.logspace()function is and how to use it
- How to modify the
np.logspace()function using its many different parameters
- How to use the
np.logspace()function to create visualizations using Matplotlib
What is the NumPy logspace Function?
np.logspace() function is used to generate a range of numbers that are evenly spaced on a log scale. This can be very helpful when you need to create ranges of values. While you’re able to modify the base of the function, the
np.logspace() function will default to a base of ten.
Before diving into examples, let’s take a look at the parameters of the
# The Parameters of the np.logspace() Function: import numpy as np np.logspace( start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0 )
Let’s break down the parameters of the function by explaining what each parameter does:
|N/A||The starting value of the sequence, such as 10start|
|N/A||The final value of the sequence, unless |
|50||The number of samples to generate|
|If set to |
|The base of the log space.|
|N/A||The type of the output array. If a data type is not provided, it will be inferred.|
|The axis in the result, which is only relevant is the start and stop are array-like values.|
To better understand what this looks like, let’s take a look at an example:
# An Example of the np.logspace() Function import numpy as np print(np.logspace(start=1, stop=10)) # Returns: # [1.00000000e+01 1.52641797e+01 2.32995181e+01 3.55648031e+01 # 5.42867544e+01 8.28642773e+01 1.26485522e+02 1.93069773e+02 # 2.94705170e+02 4.49843267e+02 6.86648845e+02 1.04811313e+03 # 1.59985872e+03 2.44205309e+03 3.72759372e+03 5.68986603e+03 # 8.68511374e+03 1.32571137e+04 2.02358965e+04 3.08884360e+04 # 4.71486636e+04 7.19685673e+04 1.09854114e+05 1.67683294e+05 # 2.55954792e+05 3.90693994e+05 5.96362332e+05 9.10298178e+05 # 1.38949549e+06 2.12095089e+06 3.23745754e+06 4.94171336e+06 # 7.54312006e+06 1.15139540e+07 1.75751062e+07 2.68269580e+07 # 4.09491506e+07 6.25055193e+07 9.54095476e+07 1.45634848e+08 # 2.22299648e+08 3.39322177e+08 5.17947468e+08 7.90604321e+08 # 1.20679264e+09 1.84206997e+09 2.81176870e+09 4.29193426e+09 # 6.55128557e+09 1.00000000e+10]
We can see that the function returns 50 values, ranging with equally spaced values from 10**1 through to 10**10. In the following section, you’ll learn how to specify the number of values in the NumPy logspace function.
Specifying the Numer of Values in the NumPy logspace Function
In many cases, you’ll want to specify the number of values your range includes. In the case of the
np.logspace() function, you can modify this by changing the
num= argument. Let’s see how we can replicate our earlier example but only return ten values:
# Modifying the Number of Values Returned import numpy as np print(np.logspace(start=1, stop=10, num=10)) # Returns: # [1.e+01 1.e+02 1.e+03 1.e+04 1.e+05 1.e+06 1.e+07 1.e+08 1.e+09 1.e+10]
In the code above, we specify:
- The default base of 10
- The start and end values of 1 and 10
- 10 values in our range
Because of this, we return the values 101, 102, … 1010.
Modifying the Base of Values in the NumPy logspace Function
By default, the NumPy logspace function uses a base of ten, meaning that every value in the linear space from
stop is a power raised to base 10. In some cases, however, you’ll want to modify the base of the values you return.
The base used in the NumPy logspace function can be modified using the
base= argument. Let’s see how we can change the base of our range to 2:
# Modifying the Base of the logspace() Function import numpy as np print(np.logspace(start=1, stop=10, num=10, base=2)) # Returns: # [ 2. 4. 8. 16. 32. 64. 128. 256. 512. 1024.]
In the example above, we raised the value of 2 to the powers ranging from 1 through 10, evenly spaced. In this case, this means we return the values 21, 22, … 210.
Modifying the Data Type of Values in the NumPy logspace Function
By default, NumPy will infer the data type to return values for. You’ll note, in the example above, that even though all of the returned values could be inferred to be integers, NumPy will never infer a data type as an integer.
We can specify the data type of range using the
dtype= argument. Let’s see how we can modify our above example to use a data type of
# Modify the Data Type of a Range in the logspace() Function import numpy as np print(np.logspace(start=1, stop=10, num=10, base=2, dtype='int')) # Returns: # [ 2 4 8 16 32 64 128 256 512 1024]
In the next section, you’ll learn how to add additional dimensions to the range returned by the
Modifying the Axis (Dimensionality) of Values in the NumPy logspace Function
By default, NumPy will create an array with a single dimension. However, we can also pass in arrays of values into the start and stop values and we can increase the dimensionality of what gets returned.
Let’s see how we can pass lists of values as our start and stop parameters:
# Changing the Dimensionality of Our Range import numpy as np print(np.logspace(start=[1,10], stop=[10,20], num=5, base=2, dtype='int')) # Returns: # [[ 2 1024] # [ 9 5792] # [ 45 32768] # [ 215 185363] # [ 1024 1048576]]
When we check the shape of our returned array using the
.shape attribute, we’ll see that it returns
Say we wanted to return a transposed version of this array, we could pass in a value of 1 into the
axis= parameter. Let’s see how we can modify this behavior:
# Changing the Dimensionality of Our Range import numpy as np print(np.logspace(start=[1,10], stop=[10,20], num=5, base=2, dtype='int', axis=1)) # Returns: # [[ 2 9 45 215 1024] # [ 1024 5792 32768 185363 1048576]]
Visualizing the NumPy logspace Function in Python
In this final section, you’ll learn how to visualize the outputs of the NumPy logspace function using Matplotlib. Visualizing the data can be a helpful way to understand how the data are returned.
Let’s take a look at how we can couple the
np.linspace() with the
np.logspace() functions to return a visualization of our data:
# Visualizing the NumPy logspace function import numpy as np import matplotlib.pyplot as plt x = np.linspace(1,5) y = np.logspace(1,5) plt.plot(x, y) plt.show()
This returns the following visualization:
In this tutorial, you learned how to use the NumPy logspace function to return arrays of numbers spaced evenly on a log scale. First, you learned how to understand all of the different parameters of the function. Then, you explored how to modify the number of values returned as well as the base of values used. Then, you learned how to change the data type of values returned and the dimensionality of the returned array. Finally, you learned how to visualize the output of the returned array.
To learn more about related topics, check out the tutorials below: