Log in python numpy

How to Use NumPy log() in Python?

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()

  • 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.
Читайте также:  Готовый код футера html

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.

 # Output: [ 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') 

NumPy log

Yields below output

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

You may also like reading:

Источник

numpy.log#

The natural logarithm log is the inverse of the exponential function, so that log(exp(x)) = x. The natural logarithm is logarithm in base e .

Parameters : x array_like

out ndarray, None, or tuple of ndarray and None, optional

A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

where array_like, optional

This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None , locations within it where the condition is False will remain uninitialized.

For other keyword-only arguments, see the ufunc docs .

Returns : y ndarray

The natural logarithm of x, element-wise. This is a scalar if x is a scalar.

Logarithm is a multivalued function: for each x there is an infinite number of z such that exp(z) = x. The convention is to return the z whose imaginary part lies in (-pi, pi].

For real-valued input data types, log always returns real output. For each value that cannot be expressed as a real number or infinity, it yields nan and sets the invalid floating point error flag.

For complex-valued input, log is a complex analytical function that has a branch cut [-inf, 0] and is continuous from above on it. log handles the floating-point negative zero as an infinitesimal negative number, conforming to the C99 standard.

In the cases where the input has a negative real part and a very small negative complex part (approaching 0), the result is so close to -pi that it evaluates to exactly -pi.

Источник

numpy.log#

The natural logarithm log is the inverse of the exponential function, so that log(exp(x)) = x. The natural logarithm is logarithm in base e .

Parameters : x array_like

out ndarray, None, or tuple of ndarray and None, optional

A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

where array_like, optional

This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None , locations within it where the condition is False will remain uninitialized.

For other keyword-only arguments, see the ufunc docs .

Returns : y ndarray

The natural logarithm of x, element-wise. This is a scalar if x is a scalar.

Logarithm is a multivalued function: for each x there is an infinite number of z such that exp(z) = x. The convention is to return the z whose imaginary part lies in (-pi, pi].

For real-valued input data types, log always returns real output. For each value that cannot be expressed as a real number or infinity, it yields nan and sets the invalid floating point error flag.

For complex-valued input, log is a complex analytical function that has a branch cut [-inf, 0] and is continuous from above on it. log handles the floating-point negative zero as an infinitesimal negative number, conforming to the C99 standard.

In the cases where the input has a negative real part and a very small negative complex part (approaching 0), the result is so close to -pi that it evaluates to exactly -pi.

Источник

Оцените статью