numpy(Python for data science)

numpy
In [1]:
import numpy as np
In [2]:
arr=np.array([[1,2,3],[4,5,6]])
In [3]:
arr  #int array, element seperated by commas
Out[3]:
array([[1, 2, 3],
       [4, 5, 6]])
In [4]:
np.ndim(arr)
Out[4]:
2
In [5]:
arr.ndim
arr.size
arr.shape
arr.dtype
Out[5]:
dtype('int32')
In [6]:
print(arr.ndim,arr.size,arr.shape,arr.dtype)
2 6 (2, 3) int32
In [13]:
print("dimension :{}\nsize : {}".format(arr.ndim,arr.size))
dimension :2
size : 6
In [14]:
arr_dict={"dimension":arr.ndim,"size":arr.size,"shape":arr.shape,"data-type":arr.dtype}
for keys in arr_dict:
    print(keys+" : ",arr_dict[keys])
dimension :  2
size :  6
shape :  (2, 3)
data-type :  int32
In [16]:
arr=np.array([[1,2,3],[4,5,6]],dtype=float)   # float array, element seperation without commas
print(arr)
[[1. 2. 3.]
 [4. 5. 6.]]
In [21]:
zero_arrays_lst=list([np.zeros(3,4),np.zeros(5),np.zeros(1,2),np.zeros(2,1)])
for i in range(len(zero_arrays_lst)):
    print(i+1," :  ",zero_arrays_lst[i])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-df95ea37a2a8> in <module>
----> 1 zero_arrays_lst=list([np.zeros(3,4),np.zeros(5),np.zeros(1,2),np.zeros(2,1)])
      2 for i in range(len(zero_arrays_lst)):
      3     print(i+1," :  ",zero_arrays_lst[i])

TypeError: data type not understood
In [22]:
np.zeros(3,4)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-f4e1d508c148> in <module>
----> 1 np.zeros(3,4)

TypeError: data type not understood
In [23]:
np.zeros((3,4))  # need to pass in a tuple
Out[23]:
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
In [27]:
zero_arrays_lst=list([np.zeros((3,4)),np.zeros((5)),np.zeros((1,2)),np.zeros((2,1))])
for i in range(len(zero_arrays_lst)):
    print(i+1," :  ",zero_arrays_lst[i],'\n')
1  :   [[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]] 

2  :   [0. 0. 0. 0. 0.] 

3  :   [[0. 0.]] 

4  :   [[0.]
 [0.]] 

In [30]:
# Create a constant value array of complex type 
np.full((3,4),10,dtype="complex")
Out[30]:
array([[10.+0.j, 10.+0.j, 10.+0.j, 10.+0.j],
       [10.+0.j, 10.+0.j, 10.+0.j, 10.+0.j],
       [10.+0.j, 10.+0.j, 10.+0.j, 10.+0.j]])
In [32]:
np.ones((3,4))
Out[32]:
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
In [40]:
np.arange(1,30,6)  # from 1 to 30 with steps of 6
Out[40]:
array([ 1,  7, 13, 19, 25])
In [41]:
# Create a sequence of 10 values in range 0 to 5
np.linspace(0,5,10)
Out[41]:
array([0.        , 0.55555556, 1.11111111, 1.66666667, 2.22222222,
       2.77777778, 3.33333333, 3.88888889, 4.44444444, 5.        ])
In [42]:
np.linspace(0,10,4)  #10/3 then add it from start for next
                     # start 0 and end 10 fix
Out[42]:
array([ 0.        ,  3.33333333,  6.66666667, 10.        ])
In [51]:
np.linspace(2,16,4) #(16-2)/3 then add it from start(i.e 2+....) for next
Out[51]:
array([ 2.        ,  6.66666667, 11.33333333, 16.        ])
In [53]:
arr=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print("original array : \n",arr,'\n')
print("reshaped array : \n",arr.reshape(2,6))
original array : 
 [[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]] 

reshaped array : 
 [[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]]
In [54]:
# Reshaping 3X4 array to 2X2X3 array
arr.reshape(2,2,3)     # 2(no of second bracket decided by first element) banenge 2X3 =>3D
Out[54]:
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])
In [55]:
arr.reshape(1,4,3)   # 1 banenge 4X3 =>3D
Out[55]:
array([[[ 1,  2,  3],
        [ 4,  5,  6],
        [ 7,  8,  9],
        [10, 11, 12]]])
In [56]:
arr.reshape(2,3,2) # 2 banenge 3X2
Out[56]:
array([[[ 1,  2],
        [ 3,  4],
        [ 5,  6]],

       [[ 7,  8],
        [ 9, 10],
        [11, 12]]])
In [3]:
import numpy as np
arr=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
arr.reshape(4,3,1)                                    # 4 banenge 3X1
Out[3]:
array([[[ 1],
        [ 2],
        [ 3]],

       [[ 4],
        [ 5],
        [ 6]],

       [[ 7],
        [ 8],
        [ 9]],

       [[10],
        [11],
        [12]]])
In [4]:
arr=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20],[21,22,23,24]])
arr.reshape(4,2,3)            # 4 banenge 2X3
Out[4]:
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]],

       [[13, 14, 15],
        [16, 17, 18]],

       [[19, 20, 21],
        [22, 23, 24]]])
In [5]:
arr.flatten()
Out[5]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23, 24])
In [6]:
arr.flatten('F')     # column wise from oriinal declared array
                     # also see in your screenshots or visit
                     # https://bit.ly/2OxK5J6
Out[6]:
array([ 1,  5,  9, 13, 17, 21,  2,  6, 10, 14, 18, 22,  3,  7, 11, 15, 19,
       23,  4,  8, 12, 16, 20, 24])
In [11]:
arr.ravel()                                  # for ravel visit https://bit.ly/2DRTBVr
Out[11]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23, 24])
In [12]:
arr.ravel('F')
Out[12]:
array([ 1,  5,  9, 13, 17, 21,  2,  6, 10, 14, 18, 22,  3,  7, 11, 15, 19,
       23,  4,  8, 12, 16, 20, 24])
In [13]:
# for flatten vs ravel visit 
#https://www.geeksforgeeks.org/differences-flatten-ravel-numpy/
# a.ravel():
# (i) Return only reference/view of original array
# (ii) If you modify the array you would notice that the value of original array also changes.
# (iii) Ravel is faster than flatten() as it does not occupy any memory.
# (iv) Ravel is a library-level function.

# a.flatten() :
# (i) Return copy of original array
# (ii) If you modify any value of this array value of original array is not affected.
# (iii) Flatten() is comparatively slower than ravel() as it occupies memory.
# (iv) Flatten is a method of an ndarray object.
In [15]:
print(arr,"\n")
print("tranpose of arr :\n",arr.T)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]
 [17 18 19 20]
 [21 22 23 24]] 

tranpose of arr :
 [[ 1  5  9 13 17 21]
 [ 2  6 10 14 18 22]
 [ 3  7 11 15 19 23]
 [ 4  8 12 16 20 24]]
In [22]:
# maximum element of array 
print ("Largest element is:", arr.max()) 
print ("Row-wise maximum elements:", 
                    arr.max(axis = 1)) 
  
# minimum element of array 
print ("Column-wise minimum elements:", 
                        arr.min(axis = 0)) 
  
# sum of array elements 
print ("Sum of all array elements:", 
                            arr.sum()) 
  
# cumulative sum along each row 
print ("Cumulative sum along each row:\n", 
                        arr.cumsum(axis = 1)) 
Largest element is: 24
Row-wise maximum elements: [ 4  8 12 16 20 24]
Column-wise minimum elements: [1 2 3 4]
Sum of all array elements: 300
Cumulative sum along each row:
 [[ 1  3  6 10]
 [ 5 11 18 26]
 [ 9 19 30 42]
 [13 27 42 58]
 [17 35 54 74]
 [21 43 66 90]]
In [23]:
a = np.array([[1, 2], 
            [3, 4]]) 
b = np.array([[4, 3], 
            [2, 1]]) 
  
# add arrays 
print ("Array sum:\n", a + b) 
  
# multiply arrays (elementwise multiplication) 
print ("Array multiplication:\n", a*b) 
  
# matrix multiplication 
print ("Matrix multiplication:\n", a.dot(b)) 
Array sum:
 [[5 5]
 [5 5]]
Array multiplication:
 [[4 6]
 [6 4]]
Matrix multiplication:
 [[ 8  5]
 [20 13]]
In [26]:
arr=np.array([[4,1,8],[6,2,5],[9,7,3]])
np.sin(arr)  # similarly np.exp(arr)  np.sqrt(a)
Out[26]:
array([[-0.7568025 ,  0.84147098,  0.98935825],
       [-0.2794155 ,  0.90929743, -0.95892427],
       [ 0.41211849,  0.6569866 ,  0.14112001]])
In [30]:
# sorted array 
print ("Array elements in sorted order:\n", 
                    np.sort(arr, axis = None)) 
  
# sort array row-wise 
print ("Row-wise sorted array:\n", 
                np.sort(arr, axis = 1)) 
  
# specify sort algorithm 
print ("Column wise sort by applying merge-sort:\n", 
            np.sort(arr, axis = 0, kind = 'mergesort'))
Array elements in sorted order:
 [1 2 3 4 5 6 7 8 9]
Row-wise sorted array:
 [[1 4 8]
 [2 5 6]
 [3 7 9]]
Column wise sort by applying merge-sort:
 [[4 1 3]
 [6 2 5]
 [9 7 8]]
In [39]:
# Example to show sorting of structured array 
# set alias names for dtypes 
dtypes = [('name', 'S10'), ('grad_year', int), ('cgpa', float)] # if use only s no letter of name shown for s1 one letter shown
  
# Values to be put in array 
values = [('Hrithik', 2009, 8.5), ('Ajay', 2008, 8.7),  
           ('Pankaj', 2008, 7.9), ('Aakash', 2009, 9.0)] 
             
# Creating array 
arr = np.array(values, dtype = dtypes) 
print ("\nArray sorted by names:\n", 
            np.sort(arr, order = 'name')) 
              
print ("Array sorted by grauation year and then cgpa:\n", 
                np.sort(arr, order = ['grad_year', 'cgpa'])) 
Array sorted by names:
 [(b'Aakash', 2009, 9. ) (b'Ajay', 2008, 8.7) (b'Hrithik', 2009, 8.5)
 (b'Pankaj', 2008, 7.9)]
Array sorted by grauation year and then cgpa:
 [(b'Pankaj', 2008, 7.9) (b'Ajay', 2008, 8.7) (b'Hrithik', 2009, 8.5)
 (b'Aakash', 2009, 9. )]
In [40]:
import numpy as np 
     
# make an array with numpy 
gfg = np.array([1, 2, 3, 4, 5]) 
     
# applying ndarray.__rshift__() method   => binary value nikalkar right shift
#using 2 right shift e.g 1 is 001=>0, 2=10=>1, 3=011=>0, 4=100=>1, 5=101=>1
print(gfg.__rshift__(2))
[0 0 0 1 1]
In [41]:
gfg = np.array([[1, 2, 3, 4, 5], 
                [6, 5, 4, 3, 2]])    

print(gfg.__rshift__(1))

# 1 right shift, 4=100=>10=2, 5=101=>10=2, 6=110=>11=3
[[0 1 1 2 2]
 [3 2 2 1 1]]
In [42]:
gfg = np.array([1, 2, 3, 4, 5, 6]) 
  
# applying numpy.__lt__() method 
print(gfg.__lt__(4))  # kaunsa element 4 se smaller hai uske liye True
[ True  True  True False False False]
In [44]:
gfg = np.array([[1, 2, 3, 4, 5, 6], 
                [6, 5, 4, 3, 2, 1]]) 
print(gfg.__lt__(4))
[[ True  True  True False False False]
 [False False False  True  True  True]]
In [48]:
print('Right shift 40 by two positions:') 
print(np.right_shift(40,2)) 
print('\n')  

print('Binary representation of 40:') 
print(np.binary_repr(40, width = 8) )
print('\n')  

print('Binary representation of 10\n')
print(np.binary_repr(10, width = 8) )
Right shift 40 by two positions:
10


Binary representation of 40:
00101000


Binary representation of 10

00001010
In [49]:
gfg = np.array([1, 2, 3, 4, 5])  
print(gfg.__pow__(3)) 
[  1   8  27  64 125]
In [50]:
gfg = np.array([[1, 2, 3, 4, 5], 
                [6, 5, 4, 3, 2]]) 
     
print(gfg.__pow__(2))
[[ 1  4  9 16 25]
 [36 25 16  9  4]]
In [52]:
gfg = np.array([1, 2, 3, 4, 5, 6]) 
  
# applying numpy.__eq__() method    
print(gfg.__eq__(4))     # True jo 4 ke equal hai
[False False False  True False False]
In [53]:
gfg = np.array([1, 2, 3, 4, 5]) 
    
# applying ndarray.__divmod__() method 
print(gfg.__divmod__(3))    # first array gives quatient , second gives remainder
(array([0, 0, 1, 1, 1], dtype=int32), array([1, 2, 0, 1, 2], dtype=int32))
In [54]:
gfg = np.array([1, 2, 3, 4, 5]) 
    
# applying ndarray.__invert__() method 
print(gfg.__invert__())  # gives -(element+1)
[-2 -3 -4 -5 -6]
In [56]:
gfg = np.array([1, 2.5, 3, 4.8, 5]) 
    
# applying ndarray.__mod__() method 
print(gfg.__mod__(2))  # gives remainder
[1.  0.5 1.  0.8 1. ]
In [58]:
gfg = np.array([1, 2, 3, 4, 5, 6]) 
  
# applying numpy.__ge__() method 
print(gfg.__ge__(4))   # True for element>=4
[False False False  True  True  True]
In [59]:
gfg = np.array([1.2, 2.6, 3, 4.5, 5]) 
    
# applying ndarray.__iadd__() method 
print(gfg.__iadd__(5))  # gives element+5
[ 6.2  7.6  8.   9.5 10. ]
In [60]:
gfg = np.array([1, 2.5, 3, 4.8, 5]) 
   
# applying ndarray.__floordiv__() method 
print(gfg.__floordiv__(2))    # division ke baad floor value 
                              # 1/2=0.5=>0, 2.5/2=1.25=>1, 3/2=1.5=>1, 4.8/2=2.4=>2, 5/2=2.5=>2
[0. 1. 1. 2. 2.]
In [62]:
gfg = np.array([[1, 2, 3, 4.45, 5], 
                [6, 5.5, 4, 3, 2.62]]) 
   
# applying ndarray.__truediv__() method 
print(gfg.__truediv__(3))
[[0.33333333 0.66666667 1.         1.48333333 1.66666667]
 [2.         1.83333333 1.33333333 1.         0.87333333]]
In [65]:
gfg = np.array([[1, 2, 3, 4, 5], 
                [6, 5, 4, 3, 2]]) 
   
# applying ndarray.__add__() method 
print(gfg.__add__(5))  # gives element+5

# similarly can use __sub__(5) =>element+5, isub, mul ,imul , and , xor , or ,etc.
[[ 6  7  8  9 10]
 [11 10  9  8  7]]
In [67]:
gfg = np.array([1, -2, 3, 4, 5, 6]) 
  
# applying numpy.__neg__() method 
print(gfg.__neg__())     # gives -(element)
[-1  2 -3 -4 -5 -6]
In [69]:
gfg = np.array([1, 2, 3, 4, 5]) 
     
# applying ndarray.__lshift__() method 
print(gfg.__lshift__(2)) 
# e.g 1=>001=>100=4, 5=00101=>10100=20
[ 4  8 12 16 20]
In [70]:
gfg = np.array([1, 2, 3, 4, 5, 6]) 
  
# applying numpy.__gt__() method 
print(gfg.__gt__(3))    # stands for greater than , True for element>3
[False False False  True  True  True]
In [73]:
gfg = np.array([[1, 2, 3, 4, 5, 6], 
                [6, 5, 4, 3, 2, 1]]) 
  
# applying numpy.__le__() method 
print(gfg.__le__(4))      # stands for less than , True for element<4
[[ True  True  True  True False False]
 [False False  True  True  True  True]]
In [74]:
gfg = np.array([1, -2, 3, 4, 5, 6]) 
  
# applying numpy.__pos__() method 
print(gfg.__pos__())  # element*1 i.e same as given array
[ 1 -2  3  4  5  6]
In [75]:
# Python program to demonstrate a need of NumPy 
  
list1 = [1, 2, 3, 4 ,5, 6] 
list2 = [10, 9, 8, 7, 6, 5] 
  
# Multiplying both lists directly would give an error. 
print(list1*list2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-75-67b419f051c0> in <module>
      5 
      6 # Multiplying both lists directly would give an error.
----> 7 print(list1*list2)

TypeError: can't multiply sequence by non-int of type 'list'
In [76]:
list1 = [1, 2, 3, 4, 5, 6] 
list2 = [10, 9, 8, 7, 6, 5] 
  
# Convert list1 into a NumPy array 
a1 = np.array(list1) 
  
# Convert list2 into a NumPy array 
a2 = np.array(list2) 
  
print(a1*a2)
[10 18 24 28 30 30]
In [77]:
# Create a sequence of integers from 10 to 1 with a step of -2 
a = np.arange(10, 1, -2)  
print("\n A sequential array with a negative step: \n",a) 
  
# Indexes are specified inside the np.array method. 
newarr = a[np.array([3, 1, 2 ])] 
print("\n Elements at these indices are:\n",newarr)
 A sequential array with a negative step: 
 [10  8  6  4  2]

 Elements at these indices are:
 [4 8 6]
In [78]:
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) 
  
# Index values can be negative. 
arr = x[np.array([1, 3, -3])] 
print("\n Elements are : \n",arr)
 Elements are : 
 [2 4 7]
In [79]:
# Arrange elements from 0 to 19 
a = np.arange(20) 
print("\n Array is:\n ",a) 
  
# a[start:stop:step] 
print("\n a[-8:17:1]  = ",a[-8:17:1])  
  
# The : operator means all elements till the end. 
print("\n a[10:]  = ",a[10:]) 
 Array is:
  [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]

 a[-8:17:1]  =  [12 13 14 15 16]

 a[10:]  =  [10 11 12 13 14 15 16 17 18 19]
In [85]:
# Python program for indexing using basic slicing with ellipsis 
import numpy as np 
  
# A 3 dimensional array. 
b = np.array([[[1, 2, 3],[4, 5, 6]], 
              [[7, 8, 9],[10, 11, 12]]]) 
  
print(b[...,1]) #Equivalent to b[: ,: ,1 ] 
print('\nanother is \n\n',b[...,2])
[[ 2  5]
 [ 8 11]]

another is 

 [[ 3  6]
 [ 9 12]]
In [86]:
# You may wish to select numbers greater than 50 
import numpy as np 
  
a = np.array([10, 40, 80, 50, 100]) 
print(a[a>50])
[ 80 100]
In [90]:
# You may wish to square the multiples of 40  
  
a = np.array([10, 40, 80, 50, 100]) 
print(a[a%40==0]**2)
[1600 6400]
In [94]:
# You may wish to select those elements whose 
# sum of row is a multiple of 10. 

b = np.array([[5, 5],[4, 5],[16, 4]]) 
sumrow = b.sum(-1) 
print(b[sumrow%10==0]) 
[[ 5  5]
 [16  4]]
In [96]:
np.random.rand(5)
Out[96]:
array([0.90115055, 0.25908956, 0.22340836, 0.52975218, 0.95453086])
In [98]:
np.random.rand(5)
Out[98]:
array([0.84243976, 0.54451483, 0.75091248, 0.40162208, 0.89923032])
In [100]:
np.random.rand(4,4)
Out[100]:
array([[0.08313813, 0.99868251, 0.90379924, 0.80465275],
       [0.45614038, 0.02415368, 0.91492293, 0.69835641],
       [0.90168459, 0.76386689, 0.12334041, 0.534295  ],
       [0.51654206, 0.92258495, 0.87340776, 0.32100201]])
In [101]:
np.random.randn(5)
Out[101]:
array([0.2400755 , 2.11152045, 0.11469874, 1.44907797, 0.75152789])
In [103]:
np.random.randn(4)
Out[103]:
array([ 0.5146328 , -0.24683385, -2.8203805 , -0.28247116])
In [104]:
np.random.randint(5)
Out[104]:
0
In [106]:
np.random.randint(1,10)
Out[106]:
8
In [107]:
np.random.randint(1,10)
Out[107]:
6
In [108]:
np.random.randint(1,10)
Out[108]:
3
In [109]:
np.random.randint(1,50)
Out[109]:
8
In [116]:
np.random.randint(1,50)
Out[116]:
20
In [117]:
np.random.randint(1,50,10)
Out[117]:
array([36, 17, 16, 35, 23, 27, 32, 28, 31, 36])
In [118]:
np.random.randint(1,50,10)
Out[118]:
array([32,  5, 12, 14, 13,  2, 35, 27, 49, 49])
In [119]:
np.random.randint(1,100,10)
Out[119]:
array([90, 64, 29, 67, 22, 11,  2, 74, 23,  6])
In [120]:
np.random.randint(1,50,10)
Out[120]:
array([21, 39, 43, 14, 30, 13, 14,  3, 32, 29])
In [121]:
np.eye(4)
Out[121]:
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
In [122]:
np.eye(2)
Out[122]:
array([[1., 0.],
       [0., 1.]])
In [126]:
np.eye(3,4)  # 3X4 => upto square matrix 3X3 identity next column all 0
Out[126]:
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.]])
In [128]:
arr = np.array([25, 1.33, 1, 1, 100])  
  
print('Our array is:') 
print(arr) 
  
print('\nAfter applying reciprocal function:')  
print(np.reciprocal(arr)) 
  
arr2 = np.array([25], dtype = int) 
print('\nThe second array is:') 
print(arr2) 
  
print('\nAfter applying rec')
Our array is:
[ 25.     1.33   1.     1.   100.  ]

After applying reciprocal function:
[0.04      0.7518797 1.        1.        0.01     ]

The second array is:
[25]

After applying rec
In [129]:
arr = np.array([5, 10, 15])  
  
print('First array is:')  
print(arr) 
  
print('\nApplying power function:')  
print(np.power(arr, 2)) 
  
print('\nSecond array is:')  
arr1 = np.array([1, 2, 3])  
print(arr1) 
  
print('\nApplying power function again:')  
print(np.power(arr, arr1)) 
First array is:
[ 5 10 15]

Applying power function:
[ 25 100 225]

Second array is:
[1 2 3]

Applying power function again:
[   5  100 3375]
In [130]:
arr = np.array([5, 15, 20])  
arr1 = np.array([2, 5, 9])  
  
print('First array:')  
print(arr)  
  
print('\nSecond array:')  
print(arr1) 
  
print('\nApplying mod() function:')  
print(np.mod(arr, arr1)) 
  
print('\nApplying remainder() function:')  
print(np.remainder(arr, arr1))
First array:
[ 5 15 20]

Second array:
[2 5 9]

Applying mod() function:
[1 0 2]

Applying remainder() function:
[1 0 2]
In [133]:
# input two matrices 
mat1 = ([1, 6, 5],[3 ,4, 8],[2, 12, 3]) 
mat2 = ([3, 4, 6],[5, 6, 7],[6,56, 7]) 
  
# This will return dot product 
res = np.dot(mat1,mat2) 
  
  
# print resulted matrix 
print(res) 
[[ 63 320  83]
 [ 77 484 102]
 [ 84 248 117]]
In [ ]:
 

Comments

Popular posts from this blog

bookmarks

problem from contest

Rural Development