If you have not gone through indexing, please go through this link.
https://spacewidget.blogspot.com/2021/09/indexing-in-python-must-know-these-tips.html
In this blog, we will learn the mathematical operations that can be applied to all the values in the array.
Let's create an array using numpy. Always remember to import numpy before using it.
>>import numpy as np
>>arr_data=np.arange( 10, 100, 5)
Output:
array([10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75,
80, 85, 90, 95])
a) Minimum value in the array
Syntax:
array_name.min( )
or
np.min(array_name)
Ex: Find the minimum value in the array created with the name arr_data.
>> arr_data.min()
Output:
10
>> arr_data.min()
Output:
10
b) Maximum value in the array
Syntax:
array_name.max( )
or
np.max(array_name)
Ex: Find the maximum value in the array
>> arr_data.max()
Output:
95
>> arr_data.max()
Output:
95
c) Index of the minimum value in the array
Syntax:
array_name.argmin( )
or
np.argmin(array_name)
Ex: Find the index of the minimum value in the array
>> arr_data.argmin()
Output:
0
>> arr_data.argmin()
Output:
0
d) Index of the maximum value in the array
Syntax:
array_name.argmax( )
or
np.argmax(array_name)
Ex: Find the index of the maximum value in the array
>> arr_data.argmax()
Output:
17
>> arr_data.argmax()
Output:
17
e) Average value of the array
Syntax:
array_name.mean( )
or
np.mean(array_name)
Ex: Find the average value of the array.
>> arr_data.mean()
Output:
52.5
>> arr_data.mean()
Output:
52.5
f) Standard deviation value of the array
Syntax:
array_name.std( )
or
np.std(array_name)
Ex: Find the standard deviation of the values in the array.
>> arr_data.std()
Output:
25.940637360455632
>> arr_data.std()
Output:
25.940637360455632
g) Median value of the array
Syntax:
np.median(array_name)
Ex: Find the median of the values in the array.
>> np.median(arr_data)
Output:
52.5
>> np.median(arr_data)
Output:
52.5
Here in the array we have 18 elements (even number) so the median will be the average of the 9th and 10th elements (ie., with index 8 and 9). Median= (50+55)/2= 52.5.
h) Variance of the values in the array
Syntax:
np.var(array_name)
Ex: Find the variance of the values in the array.
>> np.var(arr_data)
Output:
672.9166666666666
>> np.var(arr_data)
Output:
672.9166666666666
variance = (standard deviation)^2
i) nth percentile of the array
Syntax:
np.percentile(array_name, nth percentile)
Ex: Find the 50th percentile values of the array.
>> np.percentile(arr_data,50)
Output:
52.5
>> np.percentile(arr_data,50)
Output:
52.5
Ex: Find the 70th percentile values of the array.
>> np.percentile(arr_data,70)
Output:
69.5
>> np.percentile(arr_data,70)
Output:
69.5
j) Squareroot value of the array
Syntax:
np.sqrt(array_name)
Ex: Find the square root of the values in the array.
>> np.sqrt(arr_data)
Output:
array([3.16227766, 3.87298335, 4.47213595, 5. , 5.47722558,
>> np.sqrt(arr_data)
Output:
array([3.16227766, 3.87298335, 4.47213595, 5. , 5.47722558,
5.91607978, 6.32455532, 6.70820393, 7.07106781, 7.41619849, 7.74596669, 8.06225775, 8.36660027, 8.66025404, 8.94427191, 9.21954446, 9.48683298, 9.74679434])
k) Log value of the array
Syntax:
np.log(array_name)
Ex: Find the log of the values in the array.
>> np.log(arr_data)
Output:
>> np.log(arr_data)
Output:
array([2.30258509, 2.7080502 , 2.99573227, 3.21887582, 3.40119738, 3.55534806, 3.68887945, 3.80666249, 3.91202301, 4.00733319, 4.09434456, 4.17438727, 4.24849524, 4.31748811, 4.38202663,
4.44265126, 4.49980967, 4.55387689])
l)Exponential value of the array
Syntax:
np.exp(array_name)
Ex: Find the exponential of the values in the array.
>> np.exp(arr_data)
Output:
>> np.exp(arr_data)
Output:
array([2.20264658e+04, 3.26901737e+06, 4.85165195e+08, 7.20048993e+10, 1.06864746e+13, 1.58601345e+15, 2.35385267e+17, 3.49342711e+19, 5.18470553e+21, 7.69478527e+23, 1.14200739e+26, 1.69488924e+28, 2.51543867e+30, 3.73324200e+32, 5.54062238e+34, 8.22301271e+36, 1.22040329e+39, 1.81123908e+41])
m) Converting degrees to radians
Syntax:
np.deg2rad()
Ex: Create an array of angles and convert them to radians.
Here we created anges between 0 to 120 witha stepsize of 30.
>>Angles = np.arange(0,120,30)
>>Angles
Output:
Output:
array([ 0, 30, 60, 90])
>> rad_angles = np.deg2rad(Angles)
>> rad_angles
Output:
Output:
array([0. , 0.52359878, 1.04719755, 1.57079633])
n) Converting radians to degrees
Syntax:
np.rad2deg( )
Ex: In the above example, we converted angles in degrees to radians. Now convert the angles in radians (rad_angles) into degrees
>> np.rad2deg(rad_angles)
Output:
>> np.rad2deg(rad_angles)
Output:
array([ 0., 30., 60., 90.])
o) Trigonometric Functions
Syntax:
np.sin()
np.cos()
np.tan()
Ex: Find the sine of the angles that were converted to radians in the array.
Use the rad-angles in the above
>> sine_values=np.sin(rad_angles)
>> sine_values
Output:
array([0. , 0.5 , 0.8660254, 1. ])
- Inverse trignometric functions
Syntax:
np.arcsin()
np.arccos()
np.arctan()
These inverse function returns angles in radians.
Ex: Find the inverse sine function of the sine_values created in the above example.
>> rad_angles=np.arcsin(sine_values)
>> rad_angles
Output:
array([0. , 0.52359878, 1.04719755, 1.57079633])
Now lets converts these angles which were in radians to degrees.
>> deg_angles=np.rad2deg(rad_angles)
>> deg_angles
Output:
array([ 0., 30., 60., 90.])
Now the resulted angles are in degrees.
- Hyperbolic functions
Syntax:
np.sinh()
np.cosh()
np.tanh()
Ex: Find the hyperbolic cosine of the angles in the array.
Use the Angles created in the above examples(in converting degrees to radians)
>> cosh_values=np.cosh(rad_angles)
>> cosh_values=np.cosh(rad_angles)
>> cosh_values
Output:
Output:
array([1. , 1.14023832, 1.60028686, 2.50917848])
- Inverse hyperbolic functions
np.arcsinh()
np.arccosh()
np.arctanh()
Ex: Find the inverse hyperbolic cosine functions values in the array.
>> cosh_angles = np.arccosh(cosh_values)
>> cosh_angles = np.arccosh(cosh_values)
>> cosh_angles
Output:
Output:
array([0. , 0.52359878, 1.04719755, 1.57079633])
Practice is the best way to learn coding...
Comments
Post a Comment