NumPy log10()
The numpy.log10()
function computes the base-10 logarithm of each element in the input array, element-wise.
Syntax
</>
Copy
numpy.log10(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | Input values for which base-10 logarithm is computed. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array to store the results. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the function. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array with the base-10 logarithm values of the input elements. If the input is a scalar, a scalar is returned. If x
contains negative values, NaN
is returned for those elements.
Examples
1. Computing Log Base 10 of a Single Value
Here, we compute the base-10 logarithm of a single number.
</>
Copy
import numpy as np
# Define a single value
value = 1000
# Compute the base-10 logarithm
result = np.log10(value)
# Print the result
print("log10 of 1000:", result)
Output:
log10 of 1000: 3.0

2. Computing Log Base 10 for an Array
We compute the log base 10 for multiple values in an array.
</>
Copy
import numpy as np
# Define an array of values
values = np.array([1, 10, 100, 1000, 10000])
# Compute the base-10 logarithm for each element
log_values = np.log10(values)
# Print the results
print("Input values:", values)
print("Base-10 logarithm:", log_values)
Output:
Input values: [ 1 10 100 1000 10000]
Base-10 logarithm: [0. 1. 2. 3. 4.]

3. Using the out
Parameter
Using an output array to store results instead of creating a new array.
</>
Copy
import numpy as np
# Define an array of values
values = np.array([1, 10, 100, 1000])
# Create an output array with the same shape
output_array = np.empty_like(values, dtype=np.float64)
# Compute log10 and store the result in output_array
np.log10(values, out=output_array)
# Print the results
print("Computed log10 values:", output_array)
Output:
Computed log10 values: [0. 1. 2. 3.]

4. Handling Negative Values
If the input array contains negative values, the result will contain NaN values.
</>
Copy
import numpy as np
# Define an array with negative values
values = np.array([10, -5, 100, -20])
# Compute the base-10 logarithm
log_values = np.log10(values)
# Print the results
print("Computed log10 values:", log_values)
Output:
Computed log10 values: [ 1. nan 2. nan]

5. Using the where
Parameter
The where
parameter allows selective computation for elements that meet a condition.
</>
Copy
import numpy as np
# Define an array of values
values = np.array([10, -5, 100, 1000])
# Define a mask (compute log only for positive values)
mask = values > 0
# Compute log10 values where mask is True
result = np.log10(values, where=mask)
# Print the results
print("Computed log10 values with mask:", result)
Output:
Computed log10 values with mask: [ 1. 0. 2. 3.]

The logarithm is computed only for positive values, and negative values remain unchanged.