Summation
We can add value to our array element based.
1 2 3 4 5 6 7 8 9 10 11 | import numpy as np from numpy import ndarray, array array_4 = np.array([ [7, 8, 9, 10], [10, 11, 12, 13]]) array_4 + 7 >>> array([[14, 15, 16, 17], [17, 18, 19, 20]]) |
We can add same shaped array to anothher array:
1 2 3 4 5 | np.ones((3,3)) + np.ones((3,3)) >>> array([[2., 2., 2.], [2., 2., 2.], [2., 2., 2.]]) |
Substraction
We can substract value to our array element based.
1 2 3 4 5 6 7 8 9 10 11 | import numpy as np from numpy import ndarray, array array_1 = np.array([ [7, 8, 9, 10], [10, 11, 12, 13]]) array_1 - 17 >>> array([[-10, -9, -8, -7], [ -7, -6, -5, -4]]) |
We can substract same shaped array to another array:
1 2 3 4 5 6 | np.ones((4,4)) - np.ones((4,4)) - np.ones((4,4)) >>> array([[-1., -1., -1., -1.], [-1., -1., -1., -1.], [-1., -1., -1., -1.], [-1., -1., -1., -1.]]) |
Muliplication
We can multiply value to our array element based.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import numpy as np from numpy import ndarray, array array_1 = array([ [1,2,3], [4,5,6], [7,8,9] ]) array_1 * 2 >>> array([[ 2, 4, 6], [ 8, 10, 12], [14, 16, 18]]) |
We can multiply same shaped array to another array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import numpy as np from numpy import ndarray, array array_1 = array([ [1,2,3], [4,5,6], [7,8,9] ]) array_2 = array([ [9,8,7], [6,5,4], [3,2,1] ]) array_1 * array_2 >>> array([[ 9, 16, 21], [24, 25, 24], [21, 16, 9]]) |
Division
We can divide our array to any value element based.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import numpy as np from numpy import ndarray, array array_1 = array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) array_1 / 3 >> > array([[0.33333333, 0.66666667, 1.], [1.33333333, 1.66666667, 2.], [2.33333333, 2.66666667, 3.]]) |
We can divide same shaped array to another array which does not contain 0:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import numpy as np from numpy import ndarray, array array_1 = array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) array_2 = array([[9, 8, 7], [6, 5, 4], [3, 2, 1]]) array_1 / array_2 >> > array([ [0.11111111, 0.25, 0.42857143], [0.66666667, 1., 1.5], [2.33333333, 4., 9.]]) |
Powers/Roots
We can take power of any array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import numpy as np from numpy import ndarray, array array_1 = array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) array_1 ** 2 >>> array([[ 1, 4, 9], [16, 25, 36], [49, 64, 81]]) |
or we can use method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import numpy as np from numpy import ndarray, array array_1 = array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) np.power(array_1, 3) >>> array([[ 1, 8, 27], [ 64, 125, 216], [343, 512, 729]], dtype=int32) |
we can take square by using power ** operator or numpy method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import numpy as np from numpy import ndarray, array array_1 = array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) array_1 ** 0.5 >>>array([ [1., 1.41421356, 1.73205081], [2., 2.23606798, 2.44948974], [2.64575131, 2.82842712, 3.] ]) np.sqrt(array_1) >>> array([ [1., 1.41421356, 1.73205081], [2., 2.23606798, 2.44948974], [2.64575131, 2.82842712, 3.] ]) np.power(array_1, (1/3)) >>> array([[1., 1.25992105, 1.44224957], [1.58740105, 1.70997595, 1.81712059], [1.91293118, 2., 2.08008382]]) |
Math Functions
Exp
Takes nth power of euler constant
1 2 3 4 5 | import numpy as np np.exp(5) >>> 148.4131591025766 |
sum
We can sum any array row based, column based or array based.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import numpy as np from numpy import ndarray, array array_1 = array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) np.sum(array_1) >>> 45 np.sum(array_1, axis=1) >>>array([ 6, 15, 24]) |
cumsum
cumsum computes the cumulative sum of elements along a specified axis of an array
1 2 3 4 5 6 7 8 9 10 11 12 13 | import numpy as np from numpy import ndarray, array array_1 = np.arange(1,9,1) array_1 >>> array([1, 2, 3, 4, 5, 6, 7, 8]) np.cumsum(array_1) >>> array([ 1, 3, 6, 10, 15, 21, 28, 36]) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | array_1 = np.arange(0,36,1).reshape(6,6) np.cumsum(array_1, axis=0) >>> array([[ 0, 1, 2, 3, 4, 5], [ 6, 8, 10, 12, 14, 16], [ 18, 21, 24, 27, 30, 33], [ 36, 40, 44, 48, 52, 56], [ 60, 65, 70, 75, 80, 85], [ 90, 96, 102, 108, 114, 120]]) np.cumsum(array_1, axis=1) >>> array([[ 0, 1, 3, 6, 10, 15], [ 6, 13, 21, 30, 40, 51], [ 12, 25, 39, 54, 70, 87], [ 18, 37, 57, 78, 100, 123], [ 24, 49, 75, 102, 130, 159], [ 30, 61, 93, 126, 160, 195]]) |
Other statisctical function in the statsic section.
Constans
There is already defined constants in Numpy:
- π (pi):
np.pi
- e (Euler’s number):
np.e
- NaN (Not a Number):
np.nan
- Infinity:
np.inf
1 2 3 4 5 6 7 8 9 10 11 | import numpy as np print("π (pi):", np.pi) print("e (Euler's number):", np.e) print("NaN (Not a Number):", np.nan) print("Infinity:", np.inf) >>> π (pi): 3.141592653589793 >>> e (Euler's number): 2.718281828459045 >>> NaN (Not a Number): nan >>> Infinity: inf |
Trigonometrical Functions
np.sin(x)
: Computes the sine of the arrayx
.np.cos(x)
: Computes the cosine of the arrayx
.np.tan(x)
: Computes the tangent of the arrayx
.np.arcsin(x)
: Computes the inverse sine (arcsin) of the arrayx
.np.arccos(x)
: Computes the inverse cosine (arccos) of the arrayx
.np.arctan(x)
: Computes the inverse tangent (arctan) of the arrayx
.np.deg2rad(x)
: Converts angles from degrees to radians.np.rad2deg(x)
: Converts angles from radians to degrees.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import numpy as np # Create an array of angles in degrees angles_deg = np.array([0, 30, 45, 60, 90]) # Convert angles from degrees to radians angles_rad = np.deg2rad(angles_deg) # Compute sine, cosine, and tangent of the angles sin_values = np.sin(angles_rad) cos_values = np.cos(angles_rad) tan_values = np.tan(angles_rad) # Compute arcsin, arccos, and arctan of some values x = np.array([0, 0.5, 1]) arcsin_values = np.arcsin(x) arccos_values = np.arccos(x) arctan_values = np.arctan(x) print("Angles (in degrees):", angles_deg) print("Sine:", sin_values) print("Cosine:", cos_values) print("Tangent:", tan_values) print("Arcsin of", x, ":", arcsin_values) print("Arccos of", x, ":", arccos_values) print("Arctan of", x, ":", arctan_values) >>> Angles (in degrees): [ 0 30 45 60 90] >>> Sine: [0. 0.5 0.70710678 0.8660254 1.] >>> Cosine: [1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01 6.12323400e-17] >>> Tangent: [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00 1.63312394e+16] >>> Arcsin of[0. 0.5 1.]: [0. 0.52359878 1.57079633] >>> Arccos of[0. 0.5 1.]: [1.57079633 1.04719755 0.] >>> Arctan of[0. 0.5 1.]: [0. 0.46364761 0.78539816] |
Apply Custom Function
We can apply any custom function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import numpy as np from numpy import array, ndarray def create_polynom(vals: ndarray) -> ndarray: return vals **2 + vals - 5 array_ = np.arange(0, 9, 1) array_ >>> array([0, 1, 2, 3, 4, 5, 6, 7, 8]) create_polynom(array_) >>> array([-5, -3, 1, 7, 15, 25, 37, 51, 67]) |