KEMBAR78
CSE220 2D Numpy Array | PDF | Matrix (Mathematics) | Linear Map
0% found this document useful (0 votes)
13 views18 pages

CSE220 2D Numpy Array

CSE220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views18 pages

CSE220 2D Numpy Array

CSE220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Multi-Dimensional Array

Multi Dimensional Array


Scalar, Vector , Matrix and Tensor.

1
Multi-Dimensional Array

1D, 2D and 3D array

2
Multi-Dimensional Array

RGB image

A RGB image is actually a


3D array containing three
2D Matrices for Red, Green
and Blue values.

3
Multi-Dimensional Array

Matrix/2D Array Examples

● Seating Arrangement in a classroom.


● Linear Algebra Usage (Matrix representation).
● Displays are 2D array of pixels.

2D MATRIX

Dimensions of matrices
Rows x Columns

4
Multi-Dimensional Array

2D Matrix in Python using Numpy


import numpy as np
arr2D = np.zeros(shape=(row,col), dtype = datatype)

arr2D = np.zeros(shape=(3,4), dtype = int) Output


print(arr2D) [[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]

arr2D = np.zeros(shape=(3,4), dtype = float) Output


print(arr2D) [[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

arr2D = np.zeros(shape=(3,4), dtype = str) Output


print(arr2D) [['' '' '' '']
['' '' '' '']
['' '' '' '']]

5
Multi-Dimensional Array

2D Matrix from Python List using Numpy

arr2D=np.array( [[1,2,4,6],[5,7,9,8]] ) Output


print(arr2D) [[1 2 4 6]
print(arr2D.dtype) [5 7 9 8]]
int64

arr2D=np.array( [[1,2,4.2,6],[5,7,9,8]] ) Output


print(arr2D) [[1. 2. 4.2 6. ]
print(arr2D.dtype) [5. 7. 9. 8. ]]
float64

6
Multi-Dimensional Array

Indexing in 2D Matrix a

● a[row_num] will give us a


single linear array.

● a[row_num][col_num] can
access individual cell.

a = np.array( [[1,2,4,6],[5,6,7,8],[9,0,1,2]] ) Output


print(a) [[1 2 4 6]
print("First Row", a[0]) [5 6 7 8]
print("Second Row", a[1])
[9 0 1 2]]
print("Third Cell", a[0][2])
First Row [1 2 4 6]
Second Row [5 6 7 8]
Third Cell 4 7
Multi-Dimensional Array

Accessing the shape and size of a 2D Matrix

arr2D = np.zeros(shape=(3,4), dtype = int) Output


print(arr2D) [[0 0 0 0]
row, col = arr2D.shape [0 0 0 0]
total_cells = arr2D.size [0 0 0 0]]
print(“Row Amount”, row) Row Amount 3
print(“Column Amount”, col) Column Amount 4
print(“Total cells”, total_cells)
Total cells 12

8
Multi-Dimensional Array

Iteration of 2D Matrix

arr=np.array( [[1,2,4,6],[5,7,9,8]] ) Output


r_len , c_len = arr.shape 1 2 4 6
#The Outer loop iterates rows 5 7 9 8
for r in range(r_len):
#The inner loop iterates column of each row
for c in range(c_len):
print(arr[r][c],end=' ')
print()

9
Multi-Dimensional Array

Scalar & Matrix Multiplication

A=np.array( [[0,2,3],[1,1,0]] ) Output


print("Before",A) Before:
r_len , c_len = A.shape [[0 2 3]
#The outer loop iterates rows [1 1 0]]
for r in range(r_len): After:
#The inner loop iterates column of each row [[0 4 6]
[2 2 0]]
for c in range(c_len):
A[r][c] = 2*A[r][c]
print("After",A)

10
Multi-Dimensional Array

Matrix Multiplication Visual m=3


p=4
n=3

= = 1x2 + 2x6 + 1x1


Multi-Dimensional Array

Matrix Multiplication Visual m=3


p=4

=
Multi-Dimensional Array

Matrix Multiplication Visual m=3


p=4

=
Multi-Dimensional Array

Matrix Multiplication Visual m=3


p=4

=
Multi-Dimensional Array

Matrix Multiplication Visual m=3


p=4

=
Multi-Dimensional Array

Matrix Multiplication Visual m=3


p=4

=
Multi-Dimensional Array

Matrix Multiplication Visual m=3


p=4

=
Matrix & Matrix Multiplication Code Multi-Dimensional Array

A=np.array( [[0,5],[1,2],[3,4]] ) OUTPUT


B=np.array( [[3,2,1],[1,2,4]] )
print("A(3,2):") A(3,2):
print(A) [[0 5]
print("B(2,3):") [1 2]
print(B) [3 4]]
Ar_len , Ac_len = A.shape[0], A.shape[1]
Br_len , Bc_len = B.shape[0], B.shape[1] B(2,3):
#Here, Bc_len==1 and Ac_len==Br_len [[3 2 1]
C=np.zeros( shape=(Ar_len,Bc_len), dtype=int) [1 2 4]]
Cr_len, Cc_len = C.shape[0], C.shape[1]
for i in range(Cr_len): A X B = C(3,3):
for j in range(Cc_len): [[ 5 10 20]
sum=0 [ 5 6 9]
for k in range(Br_len): [13 14 19]]
sum += A[i][k] * B[k][j]
C[i][j] = sum
print("A X B = C(3,3):")
print(C) 18

You might also like