How to Use NumPy log() in Python?

Spread the love

NumPy log() function in Python is used to compute the natural logarithm of x where x, such that all the elements of the given array. The natural logarithm log is the inverse of the numpy.exp(), so that log(exp(x)) = x. The natural logarithm is log in base e. Using the logspace() function we can get the uniformly spaced values on the log scale between the start and stop.

In this article, I will explain the NumPy log() function syntax and parameters and how we can get the log values of the given array.

1. Quick Examples of NumPy log() Function

Following are some quick examples of how to use the log() function in Python NumPy.


# Below are the quick examples
# Example 1: Get the log value of scalar
arr = np.array(2)
arr1 = np.log(arr)
 
# Example 2: Get the log values of an array
arr = np.array([1, 4, 6])
arr1 = np.log(arr)

# Example 3: Get the log values of 2-D array
arr = np.arange(1, 7).reshape(2,3)
arr1 = np.log(arr)

# Example 4 : get the log value of an array with base 2
arr = np.array([1, 4, 6])
arr1 = np.log2(arr)

# Example 5:  get the log value of an array with base 10
arr = np.array([1, 4, 6])
arr1 = np.log10(arr)

2. Syntax of NumPy log()

Following is the syntax of log() function.


# Syntax of log()
numpy.log(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) =  

2.1 Parameters of log()

Following are the parameters of the log() function.

  • x :  Input array
  • out : ndarray(In a location, in which the result is stored. If it is provided, it must have a shape that the inputs provided. If not provided or None, it will, return a freshly-allocated array).
  • where : Array and optional(This condition is broadcast over the input. At locations where the condition is set to True, the out array will be set to the function result. Elsewhere, the output array will retain its original value.
  • **kwargs : For other keyword-only arguments, see the ufunc docs.

2.2 Return Value of log()

It returns an array with a Natural logarithmic value of x, element-wise. It will return the scalar if x is the scalar.

  • If x is a real-valued data type, the return type will also be a real value. If a value cannot be written as a real value, then NaN is returned.
  • If x is a complex-valued input, the numpy.log method has a branch cut [-inf,0], and it is continuous above it.

3. Usage of NumPy log()

Numpy is a package for working with numeric data in Python. As we know NumPy has many functions that are used for calculating mathematical operations of the data in an array. The numpy.log() is a mathematical function used to calculate the natural logarithm of x where x belongs to all the input array elements.

As we discussed above, if we pass scalar into this function, it will return the natural algorithm value of scalar. For example,


import numpy as np
# Get the log value of scalar
arr = np.array(2)
arr1 = np.log(arr)
print(arr1)

# Output :
# 0.6931471805599453

Let’s create a 1-D NumPy array and pass it into this function, it will return the array of natural logarithm values.


# Get the log values of an array
arr = np.array([1, 4, 6])
arr1 = np.log(arr)
print(arr1)

# Output :
# [0.         1.38629436 1.79175947]

4. Get the Log Values of 2-D NumPy Array

Create a 2-Dimensional array using numpy.arange() function and apply this function, it will return the 2-D array of logarithm values of all elements in a given array.


# Get the log values of 2-D array
arr = np.arange(1, 7).reshape(2,3)
arr1 = np.log(arr)
print(arr1)

# Output :
# [[0.         0.69314718 1.09861229]
 [1.38629436 1.60943791 1.79175947]]

5. NumPy Logarithm with Base 2

We can calculate the log value of a NumPy array or a scalar with the base 2 using this function, It will return the array of log values with base2. For example,


# get the log value of an array with base 2
arr = np.array([1, 4, 6])
arr1 = np.log2(arr)
print(arr1)

# Output :
# [0.        2.        2.5849625]

6. NumPy Logarithm with Base 10

We can also calculate the natural logarithmic value of an array element-wise with the base 10 using this function, it will return the array of log values.


# get the log value of an array with base 10
arr = np.array([1, 4, 6])
arr1 = np.log10(arr)
print(arr1)

# Output :
# [0.         0.60205999 0.77815125]

7. Get the log Value Using logspace()

NumPy logspace() function is used to create an array of evenly spaced values between two numbers on the logarithmic scale.

The below example creates an array of equally spaced numbers on the log scale between 2 and 3. It returns 50 values in the returned array. In linear space, the sequence starts at base ** start and ends with base ** stop.


import numpy as np
# equally spaced values on log scale between 2 and 3
arr = np.logspace(2, 3)
print(arr)

Yields below output. By default, it returns 50 values.


[ 100.          104.81131342  109.8541142   115.13953993  120.67926406
  126.48552169  132.57113656  138.94954944  145.63484775  152.64179672
  159.98587196  167.68329368  175.75106249  184.20699693  193.06977289
  202.35896477  212.09508879  222.29964825  232.99518105  244.20530945
  255.95479227  268.26957953  281.1768698   294.70517026  308.88435965
  323.74575428  339.32217719  355.64803062  372.75937203  390.69399371
  409.49150624  429.19342601  449.8432669   471.48663635  494.17133613
  517.94746792  542.86754393  568.9866029   596.36233166  625.05519253
  655.12855686  686.648845    719.685673    754.31200634  790.60432109
  828.64277285  868.51137375  910.29817799  954.09547635 1000.        ]

8. Graphical representation of NumPy log

To get the graphical representation of numpy.log() use matplot library module. This module visualizes the even spaced values.


import matplotlib.pyplot as plt
arr = np.array([5, 10, 15, 20])
arr1 = np.log(arr)
plt.plot(arr1, arr, marker='*', color='blue')

Yields below output

NumPy log

9. Conclusion

In this article, I have explained NumPy log() and using this how to get the natural logarithm values of all elements in a given array.

References

Leave a Reply

You are currently viewing How to Use NumPy log() in Python?