AppDividend
Latest Code Tutorials

NumPy log10: Np log10() Method in Python

0

Numpy log10() mathematical function helps the user to calculate the Base-10 logarithm of x where x belongs to all the input array items.

Numpy log10 Example

Numpy log10() is a mathematical function that is used to get the natural logarithm of any object or an array with the base 10.

Syntax

numpy.log10(array[, out] = ufunc ‘log10’)

Parameters

The log10() function can take up to two main arguments:

  1. array: This is the input array or the object whose log is to be calculated.
  2. out: This is an optional field. A position the result is stored in. If given, it must have a form to which the inputs convey. If not given or None, it returns a freshly-allocated list. A tuple must have length the same to the number of outputs (possible only as a keyword argument).

Return Value

The log10() function returns an array of natural logarithms of the elements of the given array elements where the base is 10.

Program to show the working of numpy.log10()

See the following code.

# Program to show the working of numpy.log

# Importing numpy
import numpy as np

# We will create an 1D array
arr = np.array([4, 14, 10, 63, 11, 4, 64])
# Printing the array
print("The array is: ", arr)
# Shape of the array
print("Shape of the array is : ", np.shape(arr))

# Calculating natural log of value arr[i]+1
out = np.log10(arr)
print("Natural logarithm of the given array of base 10 is ")
print(out)

Output

The array is:  [ 4 14 10 63 11  4 64]
Shape of the array is :  (7,)
Natural logarithm of the given array of base 10 is
[0.60205999 1.14612804 1.         1.79934055 1.04139269 0.60205999
 1.80617997]

Explanation

In this program, we have first declared an array of shape 7, and then we have printed the array. Then we have called numpy.log10() to calculate the natural logarithm of the items of the given array.

Graphical representation of log10()

Let’s see the graphical representation of the log10() function.

# Program to show Graphical representation

# Importing numpy
import numpy as np
import matplotlib.pyplot as plt

# We will create an 1D array
arr = np.array([40, 2.4, 0.14, 63, 1.2, 1, 4])
# Printing the array
print("The array is: ", arr)
# Shape of the array
print("Shape of the array is : ", np.shape(arr))

# Calculating natural log of value arr[i]+1
out = np.log10(arr)
print("Natural logarithm of the given array of base 10 is ")
print(out)

# Ploting of original array in Graph
# Color will be in Green
plt.plot(arr, arr, color='green', marker='x')

# Ploting of natural log array in Graph
# Color will be in blue
plt.plot(out, arr, color='blue', marker='o')

# Showing the Graphical represntation
plt.title("numpy.log10()")
plt.xlabel("Natural Log Array")
plt.ylabel("Original Array")
plt.show()

Output

The array is:  [40.    2.4   0.14 63.    1.2   1.    4.  ]
Shape of the array is :  (7,)
Natural logarithm of the given array of base 10 is
[ 1.60205999  0.38021124 -0.85387196  1.79934055  0.07918125  0.
  0.60205999]

np log10() Function in Python

Explanation

In this program, we have first declared an array of shape 7, and then we have printed the array where array elements are in float data type.

Then we have called numpy.log10() to calculate the natural logarithm of the elements of the given array.

After that, we have plotted the original array in a 2D graph which indicates using the Greenline.

Then we have plotted the out array, which we got after finding the natural logarithm, and this indicates using the blue line.

We can see the result in the above-given image.

That is it for the numpy log10() method.

See also

Numpy log()

Numpy log2()

Numpy log1p()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.