Numpy Mathematica Functions
The NumPy is the best python library for mathematics. In NumPy Mathematical Functions blog going to learn most useful mathematical functions.
NumPy Arithmetic Operations
Using Python NumPy functions or operators solve arithmetic operations.
To use NumPy need to import it.
import numpy as np # import numpy package and np is short name given to it
Note: In this blog, all practical perform on Jupyter Notebook. If you are working on another IDE rather than it. So please assign the return value to any variable and to show it as output using a print() function.
Creating two arrays using np.arange() function and reshape it in 2D using np.reshape() function.
arr1 = np.arange(1,10).reshape(3,3)
arr2 = np.arange(1,10).reshape(3,3)
# print arr1 and arr2
print(arr1)
print(arr2)
Output >>>
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 2 3]
[4 5 6]
[7 8 9]]
Addition of Two Numpy Array
Using + Operator
arr1 + arr2
Output >>>
array([[ 2, 4, 6],
[ 8, 10, 12],
[14, 16, 18]])
using np.add() function
np.add(arr1, arr2)
Output >>>
array([[ 2, 4, 6],
[ 8, 10, 12],
[14, 16, 18]])
Subtraction of Two NumPy Array
using – Operator
arr1 - arr2
Output >>>
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
Using np.subtract() function
np.subtract(arr1, arr2)
Output >>>
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
Division of Two NumPy Array
using – Operator
arr1 / arr2
Output >>>
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
Using np.divide() function
np.divide(arr1, arr2)
Output >>>
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
Multiplication of Two NumPy Array
using * Operator
arr1 * arr2
Output >>>
array([[ 1, 4, 9],
[16, 25, 36],
[49, 64, 81]])
Using np.multiply() function
np.multiply(arr1, arr2)
Output >>>
array([[ 1, 4, 9],
[16, 25, 36],
[49, 64, 81]])
Matrix Product of Two NumPy Array (matrix)
using @ Operator
arr1 @ arr2
Output >>>
array([[ 30, 36, 42],
[ 66, 81, 96],
[102, 126, 150]])
Using np.dot() function
arr1.dot(arr2)
Output >>>
array([[ 30, 36, 42],
[ 66, 81, 96],
[102, 126, 150]])
NumPy Mathematical Built-in functions
np.max()
To find maximum value from an array.
arr1.max()
Output >>> 9
arr1.max(axis = 0) # return max value from each column
Output >>> array([7, 8, 9])
arr1.max(axis = 1) # return max value from each row
Output >>> array([3, 6, 9])
np.argmax()
arr1.argmax() # return index of max value of an array
output >>> 8
np.min()
To find minimum value from an array.
arr1.min()
Output >>> 1
arr1.min(axis = 0) # return min value from each column
Output >>> array([1, 2, 3])
np.argmin()
arr1.argmin() # return index of min value of an array
Output >>> 0
np.sum()
Return sumation of NuPy array.
np.sum(arr1)
Output >>> 45
np.sum(arr1, axis = 0) # return sum of each column
Output >>> array([12, 15, 18])
np.sum(arr1, axis = 1) # return sum of each row
Output >>> array([ 6, 15, 24])
np.mean()
Return mean of NumPy array.
np.mean(arr1)
Output >>> 5.0
np.sqrt()
Return square root of each element of NumPy array.
np.sqrt(arr1)
Output >>>
array([[1. , 1.41421356, 1.73205081],
[2. , 2.23606798, 2.44948974],
[2.64575131, 2.82842712, 3. ]])
np.std()
Return standerd division of Numpy array.
np.std(arr1)
Output >>> 2.581988897471611
no.exp()
Return exponential value of each element of the NumPy array.
np.exp(arr1)
Output >>>
array([[2.71828183e+00, 7.38905610e+00, 2.00855369e+01],
[5.45981500e+01, 1.48413159e+02, 4.03428793e+02],
[1.09663316e+03, 2.98095799e+03, 8.10308393e+03]])
no.log()
Return natural log of each element of NumPy array.
np.log(arr1)
Output >>>
array([[0. , 0.69314718, 1.09861229],
[1.38629436, 1.60943791, 1.79175947],
[1.94591015, 2.07944154, 2.19722458]])
np.log10()
Return log to the base 10 value of each element of NumPy array.
np.log10(arr1)
Output >>>
array([[0. , 0.30103 , 0.47712125],
[0.60205999, 0.69897 , 0.77815125],
[0.84509804, 0.90308999, 0.95424251]])
ConclusionÂ
In the blog of NumPy mathematical functions, we covered the most useful mathematical functions. These are the best to solve machine learning and data science project. If you want to learn more functions, then jump on the official website of Numpy.
Download Jupyter file of above source code.