NumPy log10()

The numpy.log10() method calculates the base-10 logarithm of elements in an array.

Example

import numpy as np

# create an array 
array1 = np.array([1, 10, 100, 1000])

# calculate base-10 logarithm of array1 elements result = np.log10(array1)
print(result) # Output: [0. 1. 2. 3.]

log10() Syntax

The syntax of the numpy.log10() method is:

numpy.log10(x, out = None, where = True, casting = 'same_kind', dtype = None)

log10() Arguments

The numpy.log10() method takes the following arguments:

  • x - an input array
  • out (optional) - the output array where the result will be stored
  • where (optional) - a boolean array indicating where to compute the logarithm
  • casting (optional) - casting behavior when converting data types
  • dtype (optional) - the data type of the returned output

log10() Return Value

The numpy.log10() method returns an array with the corresponding base-10 logarithmic values.


Example 1: Use of out and where in log10()

import numpy as np

array1 = np.array([1, -10, 10, 100, -1000, 10000])

# create an output array with the same shape as array1
result = np.zeros_like(array1, dtype=float)  

# compute log10 of elements in array1 # only where the element is greater than 1 np.log10(array1, where = array1 > 1, out = result)
print(result)

Output

[0. 0. 1. 2. 0. 4.]

Here, the numpy.log10() method is used to calculate the base-10 logarithm of the elements in array1. We have supplied the following additional arguments to this method:

  • where is used to specify the condition array1 > 1, indicating that the logarithm should only be computed for elements greater than 1.
  • out is set to result to store the computed logarithmic values in the result array.

Note: The logarithm of negative values is not computed, and those corresponding elements in the result array will be assigned as zeros.


Example 2: Using Optional casting Argument in log10()

The casting argument specifies the casting behavior when converting data types.

The casting can be:

  • 'no' - data types should not be cast at all
  • 'equiv' - only byte-order changes are allowed
  • 'safe' - only casts that can preserve the precision of values
  • 'same_kind' - only safe casts or casts within a kind are allowed
  • 'unsafe' - any data conversion may be done

Let's look at an example.

import numpy as np

# array of floating-point numbers
array1 = np.array([1, -2, 0, 4, -5], dtype = np.float32)

# no casting is allowed, same data type as array1 is maintained array2 = np.sign(array1, casting = 'no')
# casting is allowed to equivalent data types (floating-point numbers) array3 = np.sign(array1, casting = 'equiv')
# casting is allowed to safe data types # preserving precision (floating-point numbers) array4 = np.sign(array1, casting = 'safe')
# casting is allowed to data types of # the same kind (floating-point numbers) array5 = np.sign(array1, casting = 'same_kind')
# casting is allowed to any data type # without checks (signed integers) array6 = np.sign(array1, casting = 'unsafe')
print("Array with 'no' casting:", array2) print("Array with 'equiv' casting:", array3) print("Array with 'safe' casting:", array4) print("Array with 'same_kind' casting:", array5) print("Array with 'unsafe' casting:", array6)

Output

Array with 'no' casting: [ 1. -1.  0.  1. -1.]
Array with 'equiv' casting: [ 1. -1.  0.  1. -1.]
Array with 'safe' casting: [ 1. -1.  0.  1. -1.]
Array with 'same_kind' casting: [ 1. -1.  0.  1. -1.]
Array with 'unsafe' casting: [ 1. -1.  0.  1. -1.]

Note: The numpy.log10() method doesn't involve type casting or conversions, so the casting argument doesn't affect the results.


Example 3: Use of dtype Argument in log10()

import numpy as np

array1 = np.array([1, 10, 100, 1000, 10000])

# compute the log10 with dtype = float64 result1 = np.log10(array1, dtype = np.float64)
print("Result with dtype float64:", result1) # convert the logarithmic values to integers result2 = np.round(result1).astype(int) print("Result with dtype int:", result2)

Output

Result with dtype float64: [0. 1. 2. 3. 4.]
Result with dtype int: [0 1 2 3 4]

In this example, we calculated the logarithmic values using np.log10() with dtype = np.float64.

Then, we used np.round() to round off the floating-point values to the nearest integer. Finally, we converted the rounded values to integers using astype(int).

Now, the result2 array contains the logarithmic values rounded to the nearest integer.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges