Professional Documents
Culture Documents
#1.1 Create numpy array filled with zeros (integer array of size 3 by 4)
import numpy as np
np.zeros((3,4),dtype=np.int)
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
#1.2 Creat an array Arr = [[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15],
#[16,17,18,19,20]]and check whether given lists lst are rows of Arr :
arr=np.array(([1,2,3,4,5],[26,27,28,29,30],[41,52,63,74,85],[16,17,18,19,20]))
print(arr)
print([1,2,3,4,5]in arr.tolist())
print([6,7,8,9,10] in arr.tolist())
print([11,12,13,14,15] in arr.tolist())
print([16,17,18,19,20] in arr.tolist())
[[ 1 2 3 4 5]
[26 27 28 29 30]
[41 52 63 74 85]
[16 17 18 19 20]]
True
False
False
True
#1.3 Create 2 by 2 matrix of integers and use matrix.max() method to find
#highest entry of the matrix
import numpy as np
arr=np.array([[14,-76],[0,9],[54,34]])
print("large number: ",arr.max())
print("large number: ",arr.min())
large number: 54
#1.4 Use np.arange() and .reshape to create 3 by 3 matrix with entries from 41
#to 50.
import numpy as np
V=np.arange(41,50,1)
M=V.reshape(3,3)
print(M)
[[41 42 43]
[44 45 46]
[47 48 49]]
#1.5 Use .sum() method to find sum of all entries of 2 by 3 matrix.
import numpy
N=np.array([41,51,1],[2,4,1])
print(N.sum())
https://colab.research.google.com/drive/1nc7vJKRidFdYsE-t3JjzSmWf7Xtn7Byn#scrollTo=hFO_ADtKrn0q&printMode=true 1/3
9/16/21, 7:17 PM practical 4 - Colaboratory
p ( ())
455
#1.6 Find sum of diagonal elements of a matrix of order 3 by 3.
import numpy
N=np.array([[3,5,1],[2,4,1],[4,4,9]])
M=N.trace()
print(M)
16
#1.7 Print two matrices A and B of order 3 by 3 and print A+B, A-B, AB
A=np.array([[1,0,0],[0,1,0],[0,0,1]])
B=np.array([[1,2,3],[2,4,5],[1,4,3]])
print(A+B)
print(A-B)
print(A@B)
[[2 2 3]
[2 5 5]
[1 4 4]]
[[ 0 -2 -3]
[-2 -3 -5]
[-1 -4 -2]]
[[1 2 3]
[2 4 5]
[1 4 3]]
#1.8 Generate a random matrix A of order 3 by 3. Print inverse of A if exists,
#else print matrix is not invertible.
import numpy
A=np.random.randint(low=2,size=(3,3))
print(A)
if np.linalg.det(A)!=0:
print("the inverse of matrix A is : ",np.linalg.inv(A))
else :
print("matrix A is not invertibe")
[[1 0 0]
[0 1 1]
[0 0 0]]
#1.9 Solve the system of equations 2x + 3y = 4 and x − y = 6.
import numpy as np
A=np.array([[2,3],[1,-1]]) #coefficient matrix of given euation
B=np.array([4,6]) #contant matrix
print(('solution of linear equations is:',np.linalg.solve(A,B)))
https://colab.research.google.com/drive/1nc7vJKRidFdYsE-t3JjzSmWf7Xtn7Byn#scrollTo=hFO_ADtKrn0q&printMode=true 2/3
9/16/21, 7:17 PM practical 4 - Colaboratory
https://colab.research.google.com/drive/1nc7vJKRidFdYsE-t3JjzSmWf7Xtn7Byn#scrollTo=hFO_ADtKrn0q&printMode=true 3/3