matrix operations with python and numpy -...

15
Matrix Operations with Python and Numpy 345 123 893 m n

Upload: others

Post on 04-Jun-2020

49 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Matrix Operations with Python and Numpy

3 4 51 2 38 9 3

m

n

Page 2: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Create Arrays in Python Numpy

Create array A with values. 3 x 3 array with float datatype.

Create array A with zeros. 3 x 3 array with float datatype.

Create array A with zeros. 1 Dimensional array with length of 10. Integer 16 bit depth datatype.

Page 3: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Element wise Addition

3 4 51 2 38 9 3

+2 5 98 0 412 0 3

=5 9 149 2 720 9 6

𝐴 + 𝐵 = 𝐶

C = A + B # Element wise addition

Page 4: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Adding Each Element of Matrix (Sum of all elements)

𝐴 =3 4 51 2 38 9 3

𝑖=0

𝑚−1

𝑗=0

𝑛−1

𝐴(𝑖,𝑗) = 38

total = sum(sum(A)) #Sum all elements

Page 5: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Element wise subtraction

3 4 51 2 38 9 3

−2 5 98 0 412 0 3

=1 − 1 − 4−7 2 − 1−4 9 6

𝐴 − 𝐵 = 𝐶

C = A - B # Element wise subtraction

Page 6: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Element wise multiplication

3 4 51 2 38 9 3

∗2 5 98 0 412 0 3

=6 20 458 0 1296 0 9

𝐴 ∗ 𝐵 = 𝐶

C = A * B # Element wise multiplication

Page 7: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Scalar Multiplication

3 4 51 2 38 9 3

∗ 3 =9 12 153 6 924 27 9

𝐴 ∗ 3 = 𝐶

C = A * 3 # Scalar wise multiplication

Page 8: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Dot Product (Multiplication)(𝐴𝐵)𝑖𝑗=

𝑘=0

𝑚 −1

𝐴𝑖𝑘𝐵𝑘𝑗

3 4 51 2 38 9 3

x2 5 98 0 412 0 3

=98 15 5854 5 26124 40 117

𝐴x𝐵 = 𝐶 or 𝐴𝐵 = 𝐶

C = np.dot(A, B) # Multiplication

Page 9: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Transpose Matrix

3 4 51 2 38 9 3

𝑇

= 3 1 84 2 95 3 3

𝐷 = 𝐴𝑇

D = np.transpose(A) # Transpose

Page 10: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Inverse Matrix

3 4 51 2 38 9 3

−1

≈1.5 − 2.35 − 0.14−1.5 2.21 0.280.5 − 0.36 − 0.14

𝐷 = 𝐴−1

D = np.linalg.inv(A) # Invert

Page 11: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Change datatype of Array

# Create Float Matrix

A = np.array([3.0, 4.0, 5.0])

# Convert to Int

B = A.astype(np.int)

#Convert to Float

C = A.astype(np.float)

Page 12: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Really Great Trick! Solving Systems of Equations

7𝑥 + 5𝑦 − 3𝑧 = 16

3𝑥 − 5𝑦 + 2𝑧 = −8

5𝑥 + 3𝑦 − 7𝑧 = 0

Solve for x, y, and z.

Page 13: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Systems of Equations: Convert to Matrix / Linear Algebra Expression

7 5 − 33 − 5 25 3 − 7

𝑥𝑦𝑧=16−80

𝐴 x 𝑋 = 𝐵

𝐴𝑋 = 𝐵

Page 14: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Solve for X with Linear Algebra

7 5 − 33 − 5 25 3 − 7

𝑥𝑦𝑧=16−80

𝐴𝑋 = 𝐵

𝑋 = 𝐴−1𝐵

Page 15: Matrix Operations with python and numpy - NeboMusicnebomusic.net/perception/Matrix_Operations_Python_Numpy.pdf · Matrix Operations with Python and Numpy 345 123 893 m n. Create Arrays

Implement in Python

7 5 − 33 − 5 25 3 − 7

𝑥𝑦𝑧=16−80

𝐴𝑋 = 𝐵

𝑋 = 𝐴−1𝐵𝑋 =132 x = 1, y = 3, z = 2