Machine Learning Prerequisites (Numpy)

25 / 32

Numpy - Mathematical Operations on NumPy Arrays - Division, Modulus

Division

The division between two NumPy arrays is element-wise division and is represented by /

e.g.

import numpy as np
R = np.array( [ 20, 30, 40, 50] )
S = np.arange( 1, 5 )

T = R / S 

print(R)
array([ 20, 30, 40, 50 ])

print(S)
array( [ 1, 2, 3, 4 ] )

print(T)

Output will be

array([ 20, 15, 13.33333, 12.5 ] )

Integer Division

Integer Division between two NumPy arrays is also element-wise division and is represented by //. In integer division, the resulting element values are converted to integers (instead of float).

e.g.

import numpy as np
Ri = np.array( [ 20, 30, 40, 50] )
Si = np.arange( 1, 5 )

Ti = Ri // Si 

print(Ri)
array([ 20, 30, 40, 50 ])

print(Si)
array( [ 1, 2, 3, 4 ] )

print(Ti)

Output will be

array([ 20, 15, 13, 12 ] )

Modulus

Modulus between two NumPy arrays is also applied element-wise and is represented by %

e.g.

import numpy as np
Rm = np.array( [ 20, 30, 40, 50] )
Sm = np.arange( 1, 5 )

Tm = Rm % Sm 

print(Rm)
array([ 20, 30, 40, 50 ])

print(Sm)
array( [ 1, 2, 3, 4 ] )

print(Tm)

Output will be

array([ 0, 0, 1, 2 ] )
INSTRUCTIONS

Please follow the below steps:

(1) Please import numpy as np

Division

(2) Please create two NumPy arrays - R_div and S_div - as shown below

R_div = np.array([ [ 25,65], [40, 14] ])
S_div = np.array([ [2, 10], [4, 5] ] )

(3) Divide (using normal division) above created NumPy array R_div by S_div and store the result in a variable T_div

<<your code comes here>> = R_div  << your code comes here>> S_div

(4) Print the array T_div to see its values

print(<<your code comes here>>)

Integer Division

(1) Please import numpy as np

(2) Please create two NumPy arrays - R_int_div and S_int_div - as shown below

R_int_div = np.array([ [ 25,65], [40, 70] ])
S_int_div = np.array([ [2, 10], [8, 3] ] )

(3) Divide (using integer division) above created NumPy array R_int_div by S_int_div and store the result in a variable T_int_div

<<your code comes here>> = R_int_div  << your code comes here>> S_int_div

(4) Print the array T_int_div to see its values

print(<<your code comes here>>)

Modulus

(1) Please import numpy as np

(2) Please create two NumPy arrays - R_mod and S_mod - as shown below

R_mod = np.array([ [ 20,65], [40, 70] ])
S_mod = np.array([ [2, 10], [8, 3] ] )

(3) Apply modulus operation between above created NumPy arrays R_mod and S_mod and store the result in a variable T_mod

<<your code comes here>> = R_mod  << your code comes here>> S_mod

(4) Print the array T_mod to see its values

print(<<your code comes here>>)
See Answer

No hints are availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...