Practical 3
Practical 3
Practical 3
import numpy as np
a=np.array([[1,2,3],[4,5,6],[7,8,9]])
b=np.array([[1,2,1],[4,3,2],[6,3,2]])
print("\nMatrix1: ")
print(a)
print("\nMatrix2: ")
print(b)
#matrix addition
c=a+b
print(c)
#matrix Subtraction
d=a-b
print(d)
#matrix Multiplication
e=a@b
print(e)
#matrix transpose
t=a.transpose()
print(t)
Output:
Matrix 1:
[[1 2 3]
[4 5 6]
[7 8 9]]
Matrix 2:
[[1 2 1]
[4 3 2]
[6 3 2]]
[[ 2 4 4]
[ 8 8 8]
[13 11 11]]
[[0 0 2]
[0 2 4]
[1 5 7]]
[[27 17 11]
[60 41 26]
[93 65 41]]
[2 5 8]
[3 6 9]]
#Q 2: Create Sparse Matrix using scipy.sparse module & apply different methods
import numpy as np
print(A)
print(SA)
#Viewing stored data (not the zero items) with the data property:
print("Data in the sparse matrix is : ",SA.data)
Output:
Original Matrix is:
[[1 2 0]
[0 0 0]
[0 0 2]]
Equivalent CSC matrix is: <Compressed Sparse Column sparse array of dtype 'int64'
with 3 stored elements and shape (3, 3)>
Coords Values
(0, 0) 1
(0, 1) 2
(2, 2) 2