Numpy log10 – Return the base 10 logarithm of the input array, element-wise.

Python

In this article, We will solve one straightforward problem “How to get the base 10 logarithm of an input array, element-wise”. We are going to obtain the logarithmic values of base 10 for elements comprised in an array. You can much better understand by analyzing the example below.

Let us assume an array, input[2.35, 0, 0.36, 1, 5.69, 1000, 10] – We need to obtain corresponding logarithmic values for each element of this array in the form of a new array as [ 0.37106786 -inf -0.4436975 0. 0.75511227 3. 1. ].

  • log10(2.35) = 0.37106786
  • log10(0) = -inf
  • log10(0.36) = -0.4436975
  • log10(1) = 0.0
  • log10(5.69) = 0.75511227
  • log10(1000) = 3.0
  • log10(10) = 1.0

Before getting into our code snippet, Let us have quick look below!

Understanding Numpy Logarithmic Method

Python NumPy library provides some inbuilt methods for different mathematical operations. Among them, Numpy.log() is one of the most commonly used functions to perform logarithmic operations on an array. This method returns respective logarithmic values of base e = 2.71 for all elements in our input array(as we are using the NumPy module).

Numpy also provides individual methods to perform logarithmic base 10 (NumPy.log10()) and logarithmic base 2 (NumPy.log2()) operations. Today, We will learn how to implement the Numpy.log10() method in our code snippet today.

The Domain Numpy Logarithmic function lies in between the set of positive real numbers and The Range in the set of real numbers. Let us have a quick look below to understand the syntax for our function.

The syntax for Numpy.log10()

#syntax for log base 10
numpy.log10(x, out=None, where=True, casting='same_kind', order='K', subok : [bool, datatype])

#syntax for log base2
numpy.log2(x, out=None, where=True, casting='same_kind', order='K', subok : [bool, datatype])

The parameters used in the above snippet function as follows:

  • x: It can be a variable containing a value in Radian or it may be an array containing some value
  • out: A location in which the result is to be stored. If provided, it must have a shape similar to the input X. If not provided or None, a fresh value or result is returned. It’s optional.
  • where = true:  Where the condition is True, We must get our result or output for “where = False” We will not get any result. It’s optional. y default its value is True.
  • casting=’same_kind’: It means only float64 to float32 value or results are allowed. The function shout cast values in this range or datatype.
  • order = ‘K’: It always results in an output array in the form of k ordered. (Note: There are 4 types of orders as {‘K’, ‘C’, ‘F’, ‘A’}). It’s optional.
  • subok: [bool, datatype] to make a subclass of the result or not. if True then provide a name for the subclass. It returns an array with the same shape and type as a given array. It’s also optional.

Working with Numpy.log10() method

Now We will move into our code snippet sequentially that lets you explain How to implement our method. We are going with our google collab for our coding today. You guys must know that google collab provides the best platform to work on python as well. Without getting late. Let us get into it.

Step 1: Import Numpy

In this step, We will import the Numpy module to use NumPy.log10() function into our computer. We could not implement any NumPy methods without importing their dependency, the “Numpy Module”.

import numpy

Step 2: Creating arrays to obtain Logarithmic values

In this step, we will create an array comprising values, for which the logarithmic values are to be obtained.

y = numpy.array([2.35, 0, 0.36, 1, 5.69, 1000, 10])

In case a user needs to input values into the array manually. He/she can follow the below code snippet.

# creating  x array
x = []
n = int(input("Enter the number of values you want to enter : "))
for i in range(0, n):
    value = int(input())
    x.append(value) 

Executing the above code snippet will let a user enter the no. of values he wants to join. Moving Further, The user can enter individual values into the array as follows.

 Enter the number of values you want to enter : 3
1
2
3
[1, 2, 3]

Step 3: Implementing Numpy.log10() method

In this step, We will implement our function as below and pass both the arrays created in step 2.

a = numpy.log10(y)
b = numpy.log10(x)

Step 4: Getting output and Analyzing

In this final step, We will print our outputs loaded into variables a and b respectively.

print(a)
print(b)

#output
 [ 0.37106786        -inf -0.4436975   0.          0.75511227  3.    1.        ]
 [0.         0.30103    0.47712125]

You can see that The function returns an array comprising respective logarithmic values for all the elements in our input arrays. In this way, The Numpy.log10() method got successfully implemented.

Conclusion

Today, we learned how to use the numpy.log10() method to get the log of a value with the base 10. We tried two arrays as examples to help understand the concept better. If you’re interested in learning more about Numpy, don’t forget to check out our list of numpy tutorials.