Arithmetic operations on arrays
Mathematical operations are required in our daily life. These make our work very easy.
Let's create two arrays to perform basic mathematical operations.
>>import numpy as np
>>arr1=np.arange(1,10,2)
>>arr1
>>arr1=np.arange(1,10,2)
>>arr1
Output:
array([1, 3, 5, 7, 9])
>>arr2=np.arange(2,30,6)
>>arr2
Output:
array([ 2, 8, 14, 20, 26])Ex: Addition of two arrays
>>add=arr1+arr2 >>add
Output:array([ 3, 11, 19, 27, 35])Ex: Difference between two arrays.
>>substract= arr2-arr1
>>substract
Output:array([ 1, 5, 9, 13, 17])Ex: Multiplication of two arrays.
>>mul= arr1*arr2 >>mul
Output:array([ 2, 24, 70, 140, 234])Ex: Division of two arrays.
>>div=arr2/arr1 >>div
Output:array([2. , 2.66666667, 2.8 , 2.85714286, 2.88888889])Sometimes we need to perform these basic operations on arrays with a single value, such as increasing each value of the array by 2.
Arithmatic operations on arrays with a single value
Let's us consider arr1 created in the previous example.
Ex: Add 10 to all the elements in the array arr1.
>>arr1
Output:
array([1, 3, 5, 7, 9])
Output:
array([1, 3, 5, 7, 9])
>> arr1+10
Output:
array([11, 13, 15, 17, 19])
Ex: Subtract 1 from arr1.
>> arr1-1
Output:array([0, 2, 4, 6, 8])
Ex: Multiply arr2 created in aprevious example with 5.
>> 5*arr2
Output:array([ 10, 40, 70, 100, 130])
Ex: Find inverse of the values in the array (arr2).
>> 1/arr2
Output:array([0.5 , 0.125 , 0.07142857, 0.05 , 0.03846154])Ex: Find square of the values in the array (arr1).
>> arr1**2
Output:array([ 1, 9, 25, 49, 81], dtype=int32)Condition based selection of elements in the array:
a) Find the values in arr2 those are greater than 3.
>>arr2
Output:array([ 2, 8, 14, 20, 26])>> arr2 > 3
array([False, True, True, True, True])Applying conditional operators on the arrays will return boolean values. Boolean values are True and False. True means the element in the array satisfies the condition and False means the elements are not satisfying the condition. In this example 2 is less than 3 and the condition says 2>3 which is wrong so it returns False. but 8> 3 so returns True.
To get the values satisfying the condition, we need to give as
array_name[condition]
>> arr2[arr2 > 3]
Output:array([ 8, 14, 20, 26])b) Check whether the elements in both arr1 an arr2 are equal.
>> arr1==arr2
Output:array([False, False, False, False, False])All the elements of the result returned False, this tells us that none of the elements in both of the arrays are equal.
Matrix Operations:
a) Create a matrix.
>>mat=np.array([[1,2,3],[4,5,6],[7,8,9]]) >>mat
Output:array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b) Sum of all the elements in the matrix.
>> mat.sum()
Output:45
c) Sum of the column wise elements in the matrix.
>>mat.sum(axis=0)
Output:array([12, 15, 18])
d) Sum of the row wise elements in the matrix.
>>mat.sum(axis=1)
Output:array([ 6, 15, 24])e) Mean of each column in the matrix.
>>mat.mean(axis=0)
Output:array([4., 5., 6.])f) Maximum value of each row in the matrix.
>>mat.max(axis=1)
Output:array([3, 6, 9])Keep Learning and Keep Growing...
Comments
Post a Comment