NumPy in Python:
- NumPy is a Python package that is used to work with arrays. It also has functions for working with linear algebra, the Fourier transform, and matrices.
- Travis Oliphant designed NumPy in 2005. It is an open-source project that you are free to use.
- NumPy is an abbreviation for Numerical Python.
- Lists in Python serve the same purpose as arrays, although they are slower to process.
- NumPy’s goal is to provide array objects that are up to 50 times faster than ordinary Python lists.
- The array object in NumPy is named ndarray, and it comes with many helper functions that make working with ndarray simple.
- Arrays are often utilized in data science, where speed and resources are critical.
numpy.log() Function in Python:
Numpy log function: The NumPy Python module is concerned with the production and manipulation of array data items.
The numpy.log() method is used to compute the natural logarithmic value a data value of an element or array values.
Syntax:
numpy.log(element or array)
Example1
Approach:
- Import numpy module as np using the import Keyword.
- Give the number as static input and store it in a variable.
- Pass the given number as an argument to the np.log() function to get the natural logarithmic value of it.
- Store it in another variable.
- Print the logarithmic value of the given number.
- The Exit of the Program.
Below is the implementation:
# Import numpy module as np using the import Keyword
import numpy as np
# Give the number as static input and store it in a variable.
gvn_numb = 8.5
# Pass the given number as an argument to the np.log() function to get the
# natural logarithmic value of it.
# Store it in another variable.
rslt_logvalu = np.log(gvn_numb)
# Print the logarithmic value of the given number.
print("The log value of the given number = ", rslt_logvalu)
Output:
The log value of the given number = 2.1400661634962708
Example2
# Import numpy module as np using the import Keyword
import numpy as np
# Give the np.e as static input and store it in a variable.
gvn_data = np.e
# Pass the given data as an argument to the np.log() function to get the
# natural logarithmic value of it.
# Store it in another variable.
rslt_logvalu = np.log(gvn_data)
# Print the natural logarithmic value of the given data.
print("The natural log value of the given data = ", rslt_logvalu)
Output:
The natural log value of the given data = 1.0
2)Logarithm in NumPy with base 2
Python numpy log”: Aside from the log() method’s usual functionality, we can use the following command to calculate the log value of a NumPy array or element with the base 2:
Syntax:
numpy.log2(element)
Example
# Import numpy module as np using the import Keyword
import numpy as np
# Give the number as static input and store it in a variable.
gvn_numb = 3
# Pass the given number as an argument to the np.log2() function to get the
# base-2 logarithmic value of it.
# Store it in another variable.
rslt_logvalu = np.log2(gvn_numb)
# Print the base-2 logarithmic value of the given number.
print("The base-2 log value of the given number = ", rslt_logvalu)
Output:
The base-2 log value of the given number = 1.584962500721156
3)Logarithm in NumPy with base 10
np.log python: The numpy.log10() method is used to compute an element’s natural logarithmic value to the base 10.
Example
# Import numpy module as np using the import Keyword
import numpy as np
# Give the number as static input and store it in a variable.
gvn_numb = 200
# Pass the given number as an argument to the np.log10() function to get the
# base-10 logarithmic value of it.
# Store it in another variable.
rslt_logvalu = np.log10(gvn_numb)
# Print the base-10 logarithmic value of the given number.
print("The base-10 log value of the given number = ", rslt_logvalu)
Output:
The base-10 log value of the given number = 2.3010299956639813
4)Logarithm in NumPy with Custom base
Numpy logarithm: The log() function in NumPy allows you to find logarithmic values with respect to user-defined bases.
Syntax:
numpy.log(data)/numpy.log(base)
Example
# Import numpy module as np using the import Keyword import numpy as np # Give the number as static input and store it in a variable. gvn_numb = 500 # Give the base value as static input and store it in a variable. gvn_basevalu = 25 # Calculate the value of log of given number divided by log of given base using the log() function # Store it in another variable. # (user defined base value i.e, 500 with base 25) rslt_logvalu = np.log(gvn_numb)/np.log(gvn_basevalu) # Print the above result print(rslt_logvalu)
Output:
1.9306765580733931
Formula:
![]()
As a result, the abovementioned mathematical formula is utilized to calculate the log value of a data value to a specific base value.
Logarithm in NumPy for 2-Dimensional Arrays
Natural log python numpy: To calculate the logarithmic values of all array elements, use the numpy.log() method on a 2-D NumPy array.
Syntax:
numpy.log(array)
Here, we built a 3*3 array with the numpy.reshape() function and used random integers to generate data values with the numpy.arange() technique.
Example
# Import numpy module as np using the import Keyword
import numpy as np
# Create an array by Giving the row, column size as the aruments to the reshape()
# function, and pass some random numbers to arange() function to
# create data values.
# Store it in a variable.
gvn_arry = np.arange(1, 10).reshape(3, 3)
# Print the given array
print("The given Array is: \n")
print(gvn_arry)
# Get the log values of all the array elements using the log() function by passing
# the given array as an argument to it.
# Store it in another variable.
rslt_logvalus = np.log(gvn_arry)
# Print the log values of all the array elements
print("The log values of all the given array elements:")
print(rslt_logvalus)
Output:
The given Array is: [[1 2 3] [4 5 6] [7 8 9]] The log values of all the given array elements: [[0. 0.69314718 1.09861229] [1.38629436 1.60943791 1.79175947] [1.94591015 2.07944154 2.19722458]]
Logarithm in NumPy for NumPy Arrays
# Import numpy module as np using the import Keyword
import numpy as np
# Give the some random array as static input using the np.array() function
# Store it in a variable.
gvn_arry = np.array([6,10,100,200])
# Print the given array
print("The given Array is: ")
print(gvn_arry)
# Get the log values of given array elements using the log() function by passing
# the given array as an argument to it.
# Store it in another variable.
rslt_logvalus = np.log(gvn_arry)
# Print all the log values of the given array elements
print("The log values of the given array elements:")
print(rslt_logvalus)
Output:
The given Array is: [ 6 10 100 200] The log values of the given array elements: [1.79175947 2.30258509 4.60517019 5.29831737]