#31 Python Tutorial for Beginners | Working with Matrix in Python

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

Udemy Courses:

For More Queries WhatsApp or Call on : +919008963671

Watch till 7:12 mins

In this lecture we will learn:
- What are mutli-dimeansional arrays?
- Two-dimensional array in Python
- Attributes available with a two-dimensional array
- How to convert the dimensions of an array
- Matrix in Python
- How to convert an array into a matrix?
- Functions used with a matrix

#1
- If a list contains multiple elements of the same type, then it is known as an array.
- When an array contains another array inside it, then it is known as a two-dimensional array.
- Two- dimensional array can be defined as an array within another array.
- It is a collection of rows and columns.

Syntax of a two-dimensional array:-
arr= array([
[arr1],
[arr2]
])

#2
Attributes with two-dimensional arrays:-
- dtype attribute returns the data type of the data with which you are working.
- ndim attribute gives the number of dimensions of an array and also gives the rank to an array.
e.g, if it is a single-dimensional array then it will return 1 if three-dimensional array then it returns 3.
It will also give the rank whether it is 1-D, 2-D and many more.
- shape attribute returns the number of rows and the number of columns present inside an array.
- size attribute gives the size of the entire block.
- flatten attribute convert the 2-dimensional array into a single-dimensional array.

#3
- reshape attribute is used to change the dimensions of an array. We can convert a 1-D array to a 2-D array by using the reshape method.
- reshape(r,c) method takes two parameters ie., the number of rows and a number of columns, that you want in an updated array.
- reshape attribute also works with a 3-D array. In 3-D, we have multiple 2-D arrays. And a 2-D array contains multiple 1-D arrays.

#4
- Matrices can be defined as a two-dimensional array that has multiple rows and columns.
- A matrix that has one row and multiple columns is known as a row matrix.
- A matrix having one column and multiple rows is known as a column matrix.
- Both the row matrix and column matrix is in the form of a single-dimensional array.
- A separate format available for a matrix in python.

#5
Functions with matrix:-
- matix() attribute is used to convert an array into a matrix format.
- We can also create a matrix without using an array. It can also be created directly by using the matrix() attribute.
matrix('1 2 3 6 ; 4 5 6 7')
In this, values are separated by using the space, and rows are separated by using the semicolon.
- A diagonal () function can be used to only print the diagonal elements of an array.
- min() function will give the minimum value element from an array.
- max() function is used to get the maximum value element of an array.
- There is a certain number of rules that should be followed in matrix multiplication.

Editing Monitors :

Editing Laptop :

Mics

Subscribe to our other channel:
Telusko Hindi :

Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
Рекомендации по теме
Комментарии
Автор

i am in final year. i will pay you the fees for this when i get my first salary of my first job. THANKS!

jayanthkumar
Автор

I have watched several python basic courses online. But none of them discussed array or specially multidimentional array using numpy in python. But this dude talked about each and every single topic from python. He is freaking awesome ! Seriously!! These things are specially added in premium courses of python may be! Anyway respect Sir. Highly appreciate your efforts .

dipbhowmik
Автор

Code:
from numpy import *

print("Data for matrix 1")
x = int(input("Enter the length of rows of array"))
y = int(input("Enter the length of columns of array"))
matrix1 = zeros([x, y], dtype=int)
for i in range(x):
a = array([])
for j in range(y):
element = int(input("Enter the element of array"))
a = append(a, element)
matrix1 = append(matrix1, [a], axis=0)
for i in range(x):
matrix1 = delete(matrix1, 0, 0)
print("Entered matrix is: ")
print(matrix1)

print("Data for matrix 2")
c = int(input("Enter the length of rows of array"))
if y == c:
d = int(input("Enter the length of columns of array"))
matrix2 = zeros([c, d], dtype=int)
for i in range(c):
a = array([])
for j in range(d):
element = int(input("Enter the element of array"))
a = append(a, element)
matrix2 = append(matrix2, [a], axis=0)
for i in range(c):
matrix2 = delete(matrix2, 0, 0)
print("Entered matrix is: ")
print(matrix2)

matrix3 = zeros([x, d], dtype=int)
for i in range(x):
for j in range(d):
for k in range(c):
matrix3[i][j] += matrix1[i][k] * matrix2[k][j]

print("Matrix after multiplication: ")
print(matrix3)

else:
print("Multiplication not possible as columns of matrix 1 is not equal to rows of matrix 2")
Output:
Data for matrix 1
Enter the length of rows of array2
Enter the length of columns of array3
Enter the element of array1
Enter the element of array2
Enter the element of array3
Enter the element of array4
Enter the element of array5
Enter the element of array6
Entered matrix is:
[[1. 2. 3.]
[4. 5. 6.]]
Data for matrix 2
Enter the length of rows of array3
Enter the length of columns of array2
Enter the element of array7
Enter the element of array8
Enter the element of array9
Enter the element of array10
Enter the element of array11
Enter the element of array12
Entered matrix is:
[[ 7. 8.]
[ 9. 10.]
[11. 12.]]
Matrix after multiplication:
[[ 58 64]
[139 154]]

praanshugrover_
Автор

You are the best teacher of python, i have ever seen sir ji happy to watch your python series

ajeettharu
Автор

When I saw you I remember Mr.Satya Nadela I will pray to god and support you through donations and watching ads without skipping any ad so, that you will be able to make a huge IT Company in India you are fabulous sir.

Siddharth-uozw
Автор

Hi Navin Sir ! I'm yash from Ahmedabad
I'm really enjoying the series of python in the condition of lockdown and I'm about to finish in some days,
WHAT I WANT TO TELL YOU IS!
please you also teach us Machine learning too !! I know there are lots of
International institutes who teaches data science and machine learning but i had experience form one the institution and that was bad, and i realized they doing just job they actually not intrested to teach which I've noticed,
NOW, WHEN IT COMES TO YOU.!
It' seems like you're enthusiastic towards teaching us with lots of energies and fun and that's how we can learn, no matter we're not getting certificates here but what really important is KNOWLEDGE, and in the series of python i realized you're really a worth teacher to gain the knowledge
So please I'm requesting you to teach us machine learning!
And Thank you so much for sharing your KNOWLEDGE here !!
Lots of love and wishes to you and your family ❤❤❤

yashsolanki
Автор

from numpy import *
arr1=array([
[1, 2, 3],
[4, 5, 6]])
arr2=arr1.reshape(3, 2)
arr3=ones((2, 2))
for i in range(2):
for j in range(2):
c=0
for k in range(3):
f=arr1[i][k]*arr2[k][j]
c=c+f
arr3[i][j]=c
print(arr3)

mortezaoskuei
Автор

OMG... you are so young in the matrix multiplication video. Thank you for these python & math videos!

RhoChalmers
Автор

waoh!! what a good class about matrices, am a new learner with completely no knowledge of coding and learning this through personal initiative but I bet am growing in some good speed, courtesy of Navin. I pray GOD never to lack something for us your trainees

danielkamau
Автор

2 array that contains 2 sub array of 3 element each

yashjadhav
Автор

Multiplication of two matrices m1 ( m * n ) and m2 ( n * o ) output m3 ( m * o)
from numpy import *
m1=matrix('1, 2, 3;2, 3, 4;5, 6, 7')
m2=matrix('2;4;6')
m3=zeros((len(m1[:, 1]), len(m2.T)))
if len(m1.T) != len(m2[:, 0]):

print('wrong dimensions')
else:
for i in range(len(m1[:, 1])):
for j in range(len(m2.T)):
a = array(m1[i, :])
b = array(m2[:, j].T)
m3[i, j] = sum(a * b)
print(m3)

saikumarpulicharla
Автор

Awesome teaching skills,
You are one of the best teachers I have ever seen!!!! 👍👍👍

_ritesh
Автор

you are indeed an excellent teacher of Python!

boooringlearning
Автор

Matrix Mutiplication, with user inputs :

from numpy import *

m1row = int(input("Enter the number of rows of first matrix : "))
m1column = int(input("Enter the number of columns of first matrix : "))
m2row = int(input("Enter the number of rows of second matrix : "))
m2column = int(input("Enter the number of columns of second matrix : "))

m1 = [[0 for i in range(m1column)] for j in range(m1row)]
m2 = [[0 for i in range(m2column)] for j in range(m2row)]
m3 = [[0 for i in range(m2column)] for j in range(m1row)]

print("Enter the values for First Matrix : ")
for i in range(m1row):
for j in range(m1column):
m1[i][j] = int(input("Enter the value for [" + str(i) + "]" + "[" + str(j) + "] : "))

print("Enter the values for second Matrix : ")
for i in range(m2row):
for j in range(m2column):
m2[i][j] = int(input("Enter the value for [" + str(i) + "]" + "[" + str(j) + "] : "))

for i in range(m1row):
for j in range(m2column):
res = 0
for k in range(m2row):
res += m1[i][k] * m2[k][j]
m3[i][j] = res
print("Result matrix value for [" + str(i) + "]" + "[" + str(j) + "] : " + str(m3[i][j]))

print("Matrix 1 : " + str(m1))
print("Matrix 2 : " + str(m2))
print("Matrix resultant : " + str(m3))

kirubhaharkannan
Автор

from numpy import *

array1 = zeros((3, 3))
array2 = zeros((3, 3))

print('Enter 9 value in first matrix: ')
for i in range(3):
for j in range(3):
array1[i][j] = int(input())

print('Enter 9 value in second matrix: ')
for i in range(3):
for j in range(3):
array2[i][j] = int(input())

for i in range(3):
for j in range(3):
m = 0
for t in range(3):
m = m + array1[i][t] * array2[t][j]
print(m, end=' ')
print()

just_a_living_being
Автор

The way u teach is really awesome sir. Thank you sir

mogalheena
Автор

You python tutorial is best of all waiting for further upload

ishansaha
Автор

You r the finest teacher of python as im from civil enginerring and got placed in It company...and the way u taught that made me pro in python

raghavarora
Автор

from numpy import *
m = array([
[1, 2], [3, 1], [2, 3]
])
m1 = array([
[1, 0, 2], [2, 1, 3]
])
result = array([
[0, 0, 0], [0, 0, 0], [0, 0, 0]
])
for i in range(len(m)):
for j in range(len(m1[0])):
for d in range(len(m1)):
result[i][j] += m[i][d] * m1[d][j]
print(result)

rei-rvpy
Автор

from numpy import *

m1 = matrix ('1 2 3; 4 5 6 ; 7 8 9 ')
m2 = matrix ('1 2 3; 4 5 6 ; 7 8 9 ')

m3 = matrix('0 0 0 ; 0 0 0 ; 0 0 0 ')
for i in range(3):
for j in range(3):
m3[i, j]=0
for k in range(3):
m3[i, j] = m3[i, j]+m1[i, k]*m2[k, j]


print(m3)

infoplanet
join shbcf.ru