2. Basic Operations on an Array
1. Arithmetic operations:
These operations are basic arithmetic operations like
addition,subtraction,division,power etc.
The operation takes place element wise.
Eg: arr1=np.arange(6)
O/P:array([0, 1, 2, 3, 4, 5])
print(arr1+3)....every no. is added by 3
print(arr1*2).... every no. is multiplied by 2
print(arr1/3).... every no. is divided by 3
Hence element wise operations are performed whereas in the list
it will repeat the same list .
Eg: lst=[10,20,30]
print(lst*3)
This will repeat the list thrice.
O/P: =[10,20,30,10,20,30,10,20,30]
3. Arithmetic operations between 2
Arrays
Even between two arrays the operations take
place element wise.
Eg 1 : arr1=np.arange(6).....[0,1,2,3,4,5]
arr2=np.arange(6) ).....[0,1,2,3,4,5]
print(arr1+arr2)
O/P: [ 0 2 4 6 8 10]
Eg 2: print(arr1>arr2)
Here element wise comparison takes place and
returns array of boolean values.As the values in
arr1 and arr2 are equal it will return an array of
boolean values.
O/P: [False False False False False False]
4. NOTE
If number of elements in both the arrays are not same then it will throw an error .
Hence in this case as the broadcasting is not possible number of elements in the array
needs to be equal.
In case of scalar multiplication element wise multiplication takes place while in vector
product dot product is considered . Hence number of rows should be equal to number
of columns.
Eg:2D array: arr1=np.arange(6).reshape(2,3)
arr2=np.arange(7,13).reshape(2,3)
arr1=array([[ 1,2,3], [4, 5, 6]])
arr2=array([[ 7, 8, 9], [10, 11, 12]])
np.dot(arr1,arr2) OR print(arr1.dot(arr2)
O/P: array([[ 31, 34], [112, 124]])
In all these examples every time new array is created and the original array remains as
it is.So the original array is not discarded.
5. 2.Update/Modify the existing
array
When an operation is performed on the array
always new array is created and original
remains as it is.To make changes in the
orignal array we use the below methd.
arr=np.array([10,20,30])
arr+=2............# arr=arr+2
print(arr)
O/P: [12 22 32]
This will make changes to the existing array.
6. 3.Unary Operations
A single operator or unary operation
is one which takes and performs an operation with a single
operand / argument.
Aggregation functions such as min(),max(),etc also used to
perform various operations on an array.
Let arr1=[10,20,30]
1. sum():finding sum of all the elements in the array.
arr1.sum()........O/P:60
2.min(): finding minimum value from all the elements in
the array
arr1.sum()........O/P:10
3.max(): finding maximum value from all the elements in
the array
arr1.sum()........O/P:30
7. Finding the aggregation of
elements along row or column
These functions can also be used to find the values
along the rows or columns.
Eg: arr1=np.arange(12).reshape(6,2)
arr1
O/P: array([[ 0, 1], [ 2, 3], [ 4, 5], [ 6, 7], [ 8, 9], [10,
11]])
np.sum(arr1,axis=0/1)
np.max(arr1,axis=0/1)
np.min(arr1,axis=0/1)
axis=0 :column wise addition/min/max value
axis=1:row wise addition/min/max value
8. Universal Functions
MATHS FINCTIONS
np.add()-adds values
np.add(2,3)......O/P: 5
np.multiply()-multiplication of the values
np.multiply(2,3)......O/P: 6
np.sqrt()-to find square root of elements in array
np.sqrt(9,16) )......O/P: 3,4
np.log()-to find log of a value
np.log(1) .....O/P: 0.0
np.square()-to find square of a value
np.log(5) .....O/P: 25
9. Trignometric functions
np.sin/cos/tan(angle)-to find value of specific angle
np.sin (np.pi/2) .....O/P: 1.0
To convert values into angles that is, to understand that
it is value in degrees multiply the angle by pi/180 else it
will consider them as usual numerical values.
Eg:
np.sin(np.array([0,30,60,90,120]))*(np.pi/180)
O/P: array([ 0. , -0.0172444 , -0.00531995, 0.01560319,
0.01013358])
To converting an array of values of angles to array
values in radians use np.radians(angles).
To find inverse trignometric functions use
arcsin(),arccos(),arctan() functions
10. Bitwise operators
and (&): (int,int) -> int :0011 & 0101 returns the result 0001 inclusive
Or (|): (int,int) -> int: 0011 & 0101 returns the result 0111
not (~): (int) -> int: ~01 returns the result 10
exclusive or(^): (int,int) -> int :0011 & 0101 returns the result 0110
shift left (<<): (int,int) -> int: 101 << 2 returns the result 10100
For shifting left, 0s are added on the right
shift right (>>): (int,int) -> int: 101 >> 2 returns the result 1
for shifting right, bits are removed from the right
Examples: print(np.bitwise_and(10,20))
print(np.bitwise_or(10,20))
To get the binary representation use below method:
print(np.binary_repr(30))........O/P: 11110
11. Statistical functions
arr_pop=np.array([200,300,707,505,50,800])
Mean:
finding mean of elements in the array
print(np.mean(arr_pop)).......O/P:427.0
Median:
finding median of elements in the array
print(np.median(arr_pop)) ......O/P:402.5
Standard deviation:
finding standard deviation of elements in the array
print(np.std(arr_pop)) ......O/P:268.762
Variance:
finding variance of elements in the array
print(np.var(arr_pop)) ......O/P:72233.333