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 .
Given are 2 similar dimensional numpy arrays, how to get a numpy array output in which every element is an element-wise sum of the 2 numpy arrays? pls help me with this
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]]
Given are 2 similar dimensional numpy arrays, how to get a numpy array output in which every element is an element-wise sum of the 2 numpy arrays? pls help
@@mukulyadav9011 Take for Example One Dimensional Numpy Array ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- from numpy import * arr1 = array([1,4,3,7]) # 1st Array arr2 = array([5,3,8,1]) # 2nd Array arr3 = arr1 + arr2 # This will add element-wise as u said print(arr3) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Output:- >>> [6 7 11 8] This is also Know as "Vectorised Operation" He Literally Mentioned about this in the Previous Video (#30) .See from 1:35
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 ❤❤❤
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.
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
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 .
Getting different results when typing below code from numpy import * m1=matrix('1 2') m2=matrix('3 4') m3=m1+m2; print(m3) Output :- C:\Users\chauhv1\PycharmProjects\pythonProject1\venv\Scripts\python.exe C:/Users/chauhv1/PycharmProjects/pythonProject1/venv/Scripts/numpy.py Traceback (most recent call last): File "C:\Users\chauhv1\PycharmProjects\pythonProject1\venv\Scripts umpy.py", line 1, in from numpy import * File "C:\Users\chauhv1\PycharmProjects\pythonProject1\venv\Scripts umpy.py", line 3, in m1=matrix('1 2;') NameError: name 'matrix' is not defined Process finished with exit code 1
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)
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)
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()
works for all kinds of matrix, changing the formation of a and b will work too... from numpy import * a=array([[2,3],[4,5]]) b=array([[2,4,3],[8,4,2]]) c=zeros(len(a)*len(b[0]),int) c=c.reshape(len(a),len(b[0])) i=0 k=0 while i
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))
By taking user input and using inbuilt numpy library: import numpy as np from numpy import * import mathematics as ma print(" .........Enter the 1st matrix.........") print() print("Enter the no of rows: ",end=" ") rows1=int(input()) print("Enter the no of column: ", end=" ") columns1=int(input()) m=[] print("Enter all the elements of the matrix following rows to column: ") for i in range(rows1*columns1): x=float(input()) m.append(x) fmatrix1=np.array(m) m1=fmatrix1.reshape(rows1,columns1) print() print("The 1st Matrix is: ") print(m1) print() print(" .........Enter the 2nd matrix.........") print() print("Enter the no of rows: ",end=" ") rows2=int(input()) print("Enter the no of column: ", end=" ") columns2=int(input()) n=[] print("Enter all the elements of the matrix following rows to column: ") for i in range(rows2*columns2): y=float(input()) n.append(y) fmatrix2=np.array(n) m2=fmatrix2.reshape(rows2,columns2) print() print("The 2nd Matrix is: ") print(m2) print() print() print("The multiplication of two matrices is: ") ma1=matrix(m1) ma2=matrix(m2) ma3=ma1*ma2 print() print(ma3) print() print(np.dot(ma1,ma2))
Hi Mr Navin, i got one question and hope that you could address that. For the matrix multiplication what is the scenario that we will actually be using. For statistical calculations?
# Multiplication (R=AxB) =================== from numpy import * A = array([ [1, 2, 3], [4, 5, 6], # 2x3 ]) B = array([ [1, 2, 3], [2, 3, 4], [4, 5, 6] # 3x3 ]) # printing Matrix A print("A =", end="") for i in range(2): # A row for j in range(3): # A column print(end="\t") print(A[i][j], end="") print() # printing Matrix B print(" B =", end="") for i in range(3): # B row for j in range(3): # B column print(end="\t") print(B[i][j], end="") print() # creating Matrix R(AxB) blueprint R = zeros(6, int).reshape(2, 3) # 2x3(A=row x B=column) # AxB operation sum = 0 for i in range(2): # R row for j in range(3): # R column for k in range(3): # A column sum += A[i][k] * B[k][j] R[i][j] = sum sum = 0 # printing Matrix R(AxB) print(" R =", end="") for i in range(2): # R row for j in range(3): # R column print(end="\t") print(R[i][j], end="") print()
Assignment: from numpy import* arr1= array([[1,2,3],[4,5,6]]) arr2= array([[7,8],[9,10],[11,12]]) q= array ([[0,0], [0,0]]) for x in range(2): for y in range (2): for i in range(3): q[x][y]=q[x][y]+ (arr1[x][i]*arr2[i][y]) print(q)
from numpy import * ar = int(input('Enter no. of rows in matrix a:')) ac = int(input('Enter no. of columns in matrix a:')) br = int(input('Enter no. of rows in matrix b:')) bc = int(input('Enter no. of columns in matrix b:')) if ac != br: print('Multiplication NOT possible') else: a = zeros((ar, ac), int) b = zeros((br, bc), int) c = zeros((ar, bc), int) for i in range(ar): for j in range(ac): a[i, j] = int(input('Enter element row wise:')) print('matrix A is ', a) for i in range(br): for j in range(bc): b[i, j] = int(input('Enter element row wise:')) print('matrix B is ', b) for i in range(ar): for j in range(bc): x = 0 for k in range(ac): x = a[i, k] * b[k, j] + x c[i, j] = x print("A*B is ", c)
Multiplication of two matrix: (r1, c1) = shape(m1) (r2, c2) = shape(m2) arr3 = array([],int) x = 0 for i in range(r1): for k in range(c2): for j in range(c1): x = (arr1[i][j]*arr2[j][k] ) + x arr3 = append(arr3,x) x=0 print(arr3) m3 = matrix(reshape(arr3,(r1,c2))) print(m3)
from numpy import * tryarray = array([100,26,3,475,5]) max = tryarray[0] currentmax = tryarray[0] for x in tryarray: print("currentmax", currentmax) for j in range(1, len(tryarray)): print("value of x", x) y = tryarray[j] print("value of y", y) if(x>y): max = x print("current value",max) else: max = y print("herevalue",max) if(currentmax< max): currentmax = max print("finalvalue",currentmax)
"program to multiply 2matrix usin 2d array using for loop" from numpy import* m1=array([[1,2,3],[4,5,6],[7,8,9]]) m2=array([[9,8,7],[6,5,4],[3,2,1]]) m3=[] for i in range(0,3): m3.append([]) for j in range(0,3): sum=0 for k in range(0,3): sum=sum+(m1[i][k]*m2[k][j]) m3[i].append(sum) print(matrix(m3))
Small Correction to your Code : Range of for loop should not be manually defined, it should take from array rows x columns as below. from numpy import * M1 = array([[1,3,5],[2,4,6]]) M2 = array([[2,4],[6,1],[3,5]]) M3 = [] print(" Matrix 1 : ", M1, "
Matrix 2 : ", M2) if M1.shape[0] == M2.shape[1]: for i in range(M1.shape[0]): M3.append([]) for j in range(M2.shape[1]): sum = 0 for k in range(M1.shape[1]): sum = sum + (M1[i][k] * M2[k][j]) M3[i].append(sum) print(" Matrix 1 & Matrix 2 Multiplication using for loop : ", matrix(M3)) Output : Matrix 1 : [[1 3 5] [2 4 6]] Matrix 2 : [[2 4] [6 1] [3 5]] Matrix 1 & Matrix 2 Multiplication using for loop : [[35 32] [46 42]]
Thank u sir u are doing an amazing job. my code to multiply two matrices from numpy import * b = array([[1, 2, 3], [4, 5, 6]]) a = array([[4, 5], [6, 2], [3, 9]]) m = matrix(a) n = matrix(b) o = m.tolist() p = n.tolist() d = '' for i in range(m.shape[0]): for j in range(m.shape[0]): v = 0 for k in range(m.shape[1]): v += o[i][k] * p[k][j] d += str(v) + ' ' if i != (n.shape[1] - 1): d += '; ' print(matrix(d))
@@OliFarhaan hmm let me explain it is like, I am concatenating in the string d all the products now to make it matrix I have to add some kind of delimiter i.e. ';' so, normally if we know the rule the size of the matrix will be number of row of first matrix (m)* number of columns of second matrix(n)....... so that's the thing ------> my if block is adding delimiter after getting n number of elements i.e 3 in our case
@VG ARMY hmm let me explain it is like, I am concatenating in the string d all the products now to make it matrix I have to add some kind of delimiter i.e. ';' so, normally if we know the rule the size of the matrix will be number of row of first matrix (m)* number of columns of second matrix(n)....... so that's the thing ------> my if block is adding delimiter after getting n number of elements i.e 3 in our case
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)
multiplication of two matrices m=matrix('1 2 3;4 5 6;7 8 9') m1=matrix('9 8 7;6 5 4;3 2 1') m2 =matrix('0 0 0;0 0 0;0 0 0') for y in range(0,3): for j in range(0,3): z=0 for k in range(0,3): z=z+m[y,k]*m1[k,j] m2[y,j]=z print(m2,int)
Hei , sir, you can see mine too. The channel has both Python Crash course tutorials, and R beginning course tutorials. All the videos have source files downloadable, you can check in the description of the video. Hope they will be useful.
# multiply 2 matrices using loop import numpy as np a = np.array([[1, 2, 12], [4, 9, 6]]) b = np.array([[7, 8], [9, 10], [11, 12]]) ma, na = a.shape mb, nb = b.shape c = np.full((ma, nb), 0) for i in range(ma): for j in range(nb): for k in range(na): c[i][j] += a[i][k] * b[k][j] print(c)
from numpy import * a1 = array([[1,2,3],[4,5,6]]) a2 = array([[1,2,7],[3,4,8],[5,6,9]]) if a1.shape[1] == a2.shape[0]: r,c = a1.shape # c = a2.shape[0] -> Second Matrix Rows # r -> First Matrix Rows prod = [] for i in range(0,r): row = a1[i] pr = [] for k in range(a2.shape[1]): vals = sum([row[j] * a2[j][k] for j in range(c)]) pr.append(vals) prod.append(pr) print(prod) else: print('Multiplication Not possible!!')
from numpy import * m1 = int(input('enter rows of the metrix ')) n1= int(input('eter rows of the metrix ')) A = zeros((m1,n1),int) for i in range(m1): for j in range(n1): x=int(input('enter next element of the metrix')) A[i][j]=x print(A) m2 = int(input('enter rows of the metrix ')) n2 = int(input('eter rows of the metrix ')) B = zeros((m2,n2),int) for i in range(m2): for j in range(n2): x=int(input('enter next element of the metrix')) B[i][j]=x print(B) C = zeros((m1,n2),int) for i in range(m1): for j in range(n2): x=0 for k in range(m2): x=x+A[i][k]*B[k][i] C[i][j] = x print(C)
from numpy import * a=ones((3,3),int) b=ones((3,3),int) result=zeros((3,3),int) print(a) print(b) for i in range(len(a)): for j in range(len(b)): for k in range(len(b)): result[i][j]+=+(a[i][k]*b[k][j]) for r in result: print(r)
# Multiplying 2 matrices of any order m1 = matrix(input('Type the elements of the first matrix')) m2 = matrix(input('Type the elements of the second matrix')) c = matrix(array(zeros(m1.shape[0]*m2.shape[1], int).reshape(m1.shape[0],m2.shape[1]))) if m1.shape[1] != m2.shape[0]: print('The matrices cannot be multiplied') else: print('The matrices can be multiplied') for i in range(m1.shape[0]): for j in range(m2.shape[1]): for k in range(m2.shape[0]): c[i,j] += m1[i,k] * m2[k,j] for r in c: print(r)
from numpy import * m1 = array([ [1,2], [3,4] ]) m2 = array([ [2,1], [4,3] ]) m3=array([[0,0], [0,0]]) for i in range(2): for j in range(2): for k in range(2): m3[i,j] = m1[i,k] * m2[k,j] + m3[i,j] print(m1) print(m2) print(m3)
Hi sir Ur teaching is awesome and I have a question after watching all ur sessions I used to think that I need memorize certain functions what u have thought during the class
from numpy import* m1=int(input("enter the number of rows of 1 matrix")) n1=int(input("enter the number of colmn of 1 matrix")) mat1 =zeros((m1,n1),dtype=int) u1=len(mat1) for i in range(u1): for j in range(len(mat1[i])): x=int(input("enter the value")) mat1[i][j]=x print(mat1) m2=int(input("enter the number of rows of 2 matrix")) n2=int(input("enter the number of colmn of 2 matrix")) mat2 =zeros((m2,n2),dtype=int) u2=len(mat2) for i in range(u2): for j in range(len(mat2[i])): x=int(input("enter the value")) mat2[i][j]=x print(mat2) new=zeros((m1,n2),dtype=int) for i in range(u1): for j in range(len(mat2[0])): for k in range(u2): new[i][j]=new[i][j] + mat1[i][k] * mat2[k][j] print("multiplaction of mat1 and mat2 is") matr=matrix(new) print(type(matr)) print(matr)
Q1.Solution : # I will just try 2D matrix multiplication from numpy import * row1=2 col1=row2=3 col2=2 arr1=array([[1,2,4], [3,5,6]]) arr2=array([[0,0], [0,2], [1,1]]) # row1,col1=row2,col2 and arr1,arr2 can be arranged a/c to user i/p arr1f=arr1.flatten() arr2f=arr2.flatten() arr3=zeros(4) for i1 in range(0,row1): for j2 in range(0,col2): store=0 for j1 in range(0, col1): store = store + arr1[i1][j1] * arr2[j1][j2] arr3[i1*col2+j2]=store arr4=arr3.reshape(row1,col2) print(arr4)
from numpy import * m1=matrix('1 2 3;4 5 6;7 8 9') m2=matrix('4 2 1;7 5 2;6 5 3') sum=0 for i in range(len(m1)): for j in range(len(m1)): for k in range(len(m1)): sum=m1[i,k]*m2[k,j]+sum m3[i,j]=sum sum=0 print(m3)
from numpy import * arr1=array([[2,4,6], [3,6,9], [8,12,16]]) arr2=array([[4,8,12], [5,10,15], [16,20,24]]) print("Matrix Multiplication using inbuilt method:",arr1 * arr2) print("Matrix Multiplication using for loop") for i in range (3): ctr=0 for j in range (3): ctr=arr1[i][j]*arr2[i][j] print(ctr,end=",") print()
mission completed: final problem of solution from numpy import * R = int(input("Enter the size of rows:")) C =int(input("Enter the size of columns:")) m1 = zeros((R,C),int) for i in range(R): for j in range(C): x =int(input("Enter the Element")) m1[i][j] =x R1 = int(input("Enter the size of rows:")) C1 =int(input("Enter the size of columns:")) m2 = zeros((R1,C1),int) for i in range(R1): for j in range(C1): x =int(input("Enter the Element")) m2[i][j] =x print(m1) print(m2) R2 = int(input("Enter the size of rows:")) C2 =int(input("Enter the size of columns:")) m3 = zeros((R2,C2),int) #itrate through row of m1 for i in range(len(m1)): #itrate through colomns of m2 for j in range(len(m2[0])): #itrate through row of m2 for k in range(len(m2)): m3[i][j]+=m1[i][k]*m2[k][j]
print(m3)
output:Enter the size of rows:2 Enter the size of columns:2 Enter the Element1 Enter the Element2 Enter the Element3 Enter the Element4 Enter the size of rows:2 Enter the size of columns:2 Enter the Element1 Enter the Element2 Enter the Element3 Enter the Element4 [[1 2] [3 4]] [[1 2] [3 4]] Enter the size of rows:2 Enter the size of columns:2 [[ 7 10] [15 22]]
finally got output :) from numpy import * from array import * a1 = array('i',[1,2,3,4]) print(a1) a2 = array('i',[5,6,7,8]) print(a2) a3 = array('i',[]) m1 = matrix(a1) m2 = matrix(a2) print(m1) print(m2) for j in range(1): for k in range(2): x = (a1[j]*a2[k])+(a1[j+1]*a2[k+2]) a3.append(x) for i in range(2): for e in range(1,2,1): y = (a1[e+1]*a2[i])+(a1[e+2]*a2[i+2]) a3.append(y) print(a3) m3 = matrix(a3) print(m3)
The answer of the assignment is: import numpy as np R = int(input("Enter the number of rows:")) C = int(input("Enter the number of columns:")) print("Enter {}entries in a single line (separated by space): ".format(R*C)) #The format() is a method which replaces {} in print function and put the values there. # User input of entries in a # single line separated by space entries = list(map(int, input().split())) #The split() method splits a string into a list.You can specify the separator, default separator is any whitespace. #string.split(separator, maxsplit) if (len(entries))
#Matrix multiplication 3*2 with 2*3 resulting 3*3 from numpy import * M1=array([ [7,8], [9,10], [11,12] ]) M2=array([ [1,2,3], [4,5,6] ]) M_res=array([[0,0,0],[0,0,0],[0,0,0]]) for i in range(3): for j in range(3): for k in range(2): M_res[i][j]+=M1[i][k]*M2[k][j] print(M_res)
from numpy import * m1 = matrix(' 1 2 3; 4 5 6; 7 8 9') m2 = matrix('9 8 7; 6 5 4; 3 2 1') print('m1=',m1,' ','m2=',m2) m3=zeros((m1.shape[0],m2.shape[1]),int) # blank matrix which will be 3*3 (Row of m1 and Col of m2) print(m3) for i in range(m1.shape[0]): # row in 1st matrix for j in range(m2.shape[1]): #col in 2nd matrix for k in range(m1.shape[1]): #col in 1st matrix m3[i,j]= m3[i,j]+ (m1[i,k]* m2[k,j]) print((m3))
For 3 x 3 matrix (eagerly waiting for reply) m1 = np.array([ [1,2,3],[6,4,5],[1,6,7] ]) m2 = np.array([ [1,2,3],[6,8,5],[2,6,7] ]) k = [] for i in range(len(m1)): for j in range(len(m2)): x = sum(m1[i]*m2[:,j]) k.append(x) m3 = np.array((k[0:3],k[3:6],k[6:9])) print(m3)
multiplication code by taking user input from numpy import * a1=array([ [1,2,3], [4,5,6],[5,6,8],[3,7,1] ]) r1=int(input("enter no. of rows of mat 1 : ")) c1= int(input("enter no. of columns of mat 1 : ")) r2=c1 c2=int(input("enter no. of columns of mat 2 : ")) a1=zeros(r1*c1,int).reshape(r1,c1) a2=zeros(r2*c2,int).reshape(r2,c2) a3=zeros(r1*c2,int).reshape(r1,c2) for i in range(r1): for j in range(c1): a1[i][j]=int(input("enter a1 element : ")) for i in range(r2): for j in range(c2): a2[i][j]=int(input("enter a2 element : ")) m1=matrix(a1); print("m1 : ",m1) m2=matrix(a2); print("m2 : ",m2) for i in range(r1): for j in range(c2): for k in range(r2): a3[i][j]=a1[i][k]*a2[k][j] + a3[i][j] m3=matrix(a3); print("m3 : ",m3)
Hi navin Can you please make videos on graphs and implementation using python I really like your approach and way of teaching please make video on graphs
Hello Sir! i tried an assignment problem myself. Whether i am not sure that code i had done is correct answer for your question or not. plz let me know if it is wrong. Que:Write a code to multiply 2 matrices using 2D array and using loop. Ans: from numpy import * arr1 = array ([ [1,2,3], [5,6,8], [3,4,6] ]) m1 = matrix(arr1) arr2 = array ([ [4,2,1], [8,6,7], [3,4,7] ]) m2 = matrix(arr2) print(m1) print(m2) for i in range(1): m1 = m1 * m2 m3 = m1 print(m3)
from numpy import * m1 = [[1,2,3], [9,5,6], [7,8,10]] m2 = [[2,9,6], [1,7,2], [1,5,9]] res =[[0,0,0], [0,0,0], [0,0,0]] for i in range(3): for j in range(3): for k in range(3): res[i][j] += m1[i][k] * m2[k][j] for r in res: print(r)
ans: import numpy as np # input by uesers a1 = np.array([[1, 2, 3], [6, 5, 4]]) a2 = np.array([[6, 5], [9, 5], [6, 2]]) def matrixMul(arr1, arr2): shape1 = np.shape(arr1) shape2 = np.shape(arr2) if shape1[1] != shape2[0]: print("can not be multiplied.") else: arrMul = np.zeros((shape1[0], shape2[1])) for i in range(shape1[0]): # shape1[0] is 2 for j in range(shape2[1]): # shape2[1] is 2 for k in range(shape1[1]): # either shape1[1] or shape2[0] is ok arrMul[i][j] += arr1[i][k] * arr2[k][j] return arrMul
Multiplication of Two Matrix by taking Input From User from numpy import * a1 = int(input("Enter number of rows ")) b1 = int(input("Enter number of Columns ")) a2 = int(input("Enter number of rows ")) b2 = int(input("Enter number of Columns ")) if b1 == a2: c = b1 ab1 = a1 * b1 ab2 = a2 * b2 a1b2 = a1 * b2 arr1 = (zeros(ab1).reshape(a1, b1)) arr2 = (zeros(ab2).reshape(a2, b2)) # print(arr1) print("Enter numbers for First Table") for i in range(a1): for j in range(b1): print("Enter the number in Row ", i, " Column ", j, end = " ") arr1[i][j]= int(input()) print("Enter numbers for Second Table") for i in range(a2): for j in range(b2): print("Enter the number in Row ", i, " Column ", j, end = " ") arr2[i][j]= int(input()) print('First Table') print(arr1) print('Second Table') print(arr2) arr3 = (zeros(a1b2).reshape(a1, b2)) for i in range(a1): for j in range(b2): p = 0 for k in range(c): p = (arr1[i][k] * arr2[k][j]) + p arr3[i][j]= p print("Multiplication of Both Tables ") print(arr3) else: print("Number of columns of first arr are not equal to number of rows of second array")
from numpy import * arr1 = array([[1,2,3],[4,5,6],[3,4,5]]) arr2 = array([[1,4,5],[5,4,3],[4,7,8]]) if(arr1.shape[1] == arr2.shape[0]): arr3= [] for i in range(arr1.shape[0]): for j in range(arr2.shape[1]): y = 0 for k in range(arr2.shape[1]): x = arr1[i][k]*arr2[k][j]; y = y+x arr3.append(y) m = matrix(arr3).reshape(1,3,3) print(m) else: print("Array multiplication is not possible")
from numpy import * arr= array([ [1,2,3], [4,5,6], [3,3,1] ]) m1= matrix(arr) m2=m1 m3=matrix('0,0,0;0,0,0;0,0,0') for k in range(len(m1)): for i in range(len(m1)): li=[] for j in range(len(m2)): li.append(m1[k,j]*m2[j,i]) m3[k,i]=sum(li) print(m3)
solution: from numpy import * m1 = matrix('1,2,3;4,5,6') m2 = matrix('2,4;5,3;6,7') m3 = matrix('0,0;0,0') sum = 0 for i in range(2): for j in range(2): for k in range(3): sum = sum +m1[i,k]*m2[k,j] m3[i,j]=sum sum = 0 print(m3)
sir please make a video on this assignment multiply of two matrices its so difficult please make a video sir because my concept of matrices is not clear
from numpy import * arr1=array([]) arr2=array([]) n1=int(input('Enter no. of values')) for i in range(n1): a=int(input('enter a number in array 1: ')) arr1=append(arr1, a) for i in range(n1): a = int(input('enter a number in array 2: ')) arr2=append(arr2, a) arr1=arr1.reshape(2,2) arr2=arr2.reshape(2,2) m1=matrix(arr1) m2=matrix(arr2) m3=m1*m2 print(m3) arr3=array([]) for i in range(2): for j in range(2): b = 0 for t in range(2): a=arr1[i, t]*arr2[t, j] b+=a arr3=append(arr3, b) m3=matrix(arr3) m3=m3.reshape(2, 2) print(m3)
matrix multiplication arr1=[[1,2,3], [3,4,5], [5,6,1]] arr2=[[3,4,5], [6,1,2], [4,5,6]] arr3=[[0,0,0], [0,0,0], [0,0,0]] for b in range(len(arr1)): for c in range(len(arr2[0])): #taking coloumn wise values in arr2 for h in range(len(arr2)):
arr3[b][c] +=arr1[b][h]*arr2[h][c] for j in arr3: print(j) ##hope this helps
from numpy import* m = int(input('1st Matrix:- Enter the no. of rows: ')) n = int(input('1st Matrix:- Enter the no. of columns: ')) x = matrix([0 for i in range(m*n)]) for i in range(m*n): x[0,i] = int(input("Enter the element for Matrix 1: ")) r = int(input('2nd Matrix:- Enter the no. of rows: ')) p = int(input('2nd Matrix:- Enter the no. of columns: ')) y = matrix([0 for i in range(r*p)]) for i in range(r*p): y[0,i] = int(input("Enter the element for Matrix 2: ")) m1 = x.reshape(m,n) m2 = y.reshape(r,p) m3 = m1 * m2 print("First Matrix is below:- ", m1, " ", "Second Matrix is below:- ", m2, " ", "Multiplication of Matrix:- ", m3)
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 .
i am in final year. i will pay you the fees for this when i get my first salary of my first job. THANKS!
Given are 2 similar dimensional numpy arrays, how to get a numpy array output in which every element is an element-wise sum of the 2 numpy arrays? pls help me with this
@@mukulyadav9011 i guess, m3=m1+m2
@@mukulyadav9011 it is shown in last video
@@mukulyadav9011 or you can use for loop to do
I wish all the best for you
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]]
😂
You are the best teacher of python , i have ever seen sir ji .....feeling happy to watch your python series
Yes!!
Given are 2 similar dimensional numpy arrays, how to get a numpy array output in which every element is an element-wise sum of the 2 numpy arrays? pls help
@@mukulyadav9011
Take for Example One Dimensional Numpy Array
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
from numpy import *
arr1 = array([1,4,3,7]) # 1st Array
arr2 = array([5,3,8,1]) # 2nd Array
arr3 = arr1 + arr2 # This will add element-wise as u said
print(arr3)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:-
>>> [6 7 11 8]
This is also Know as "Vectorised Operation"
He Literally Mentioned about this in the Previous Video (#30) .See from 1:35
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 ❤❤❤
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.
So kind you are 😘
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
OMG... you are so young in the matrix multiplication video. Thank you for these python & math videos!
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 .
Getting different results when typing below code
from numpy import *
m1=matrix('1 2')
m2=matrix('3 4')
m3=m1+m2;
print(m3)
Output :-
C:\Users\chauhv1\PycharmProjects\pythonProject1\venv\Scripts\python.exe C:/Users/chauhv1/PycharmProjects/pythonProject1/venv/Scripts/numpy.py
Traceback (most recent call last):
File "C:\Users\chauhv1\PycharmProjects\pythonProject1\venv\Scripts
umpy.py", line 1, in
from numpy import *
File "C:\Users\chauhv1\PycharmProjects\pythonProject1\venv\Scripts
umpy.py", line 3, in
m1=matrix('1 2;')
NameError: name 'matrix' is not defined
Process finished with exit code 1
I tried reinstalling numpy that helped :)
The same happened to me...
Do u get any solution?
@@mantesh08 reinstall numpy that will help
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
you mirrored your video at 8:31 to make us understand for diagnol elements.......WOW, SUCH A WONDERFUL TEACHER U R! 🙌
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)
Thanks brother
Bro what does this( len(m1[:,1]) )means
Awesome teaching skills,
You are one of the best teachers I have ever seen!!!! 👍👍👍
Spot on !!!!
arr2.reshape(2,2,3).........explanation: 2 array that contains 2 sub array of 3 element each
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)
you are indeed an excellent teacher of Python!
import numpy as np
m1 = np.matrix(np.arange(1,11).reshape((2,5)))
m2 = np.matrix(np.arange(1,11).reshape((5,2)))
m3 = np.matrix(np.zeros(4).reshape((2,2)), int)
t1 = m1.shape
t2 = m2.shape
#if shape mismatch display error message
if( t1[0] != t2[1] and t1[1] != t2[0]):
print("multiplication cant be done")
else:
for i in range(t1[0]):
for j in range(t2[1]):
for k in range(t1[1]):
m3[i,j] += m1[i, k] * m2[k, j]
print(m3)
print(m1 * m2)
op:
[[ 95 110]
[220 260]]
[[ 95 110]
[220 260]]
You python tutorial is best of all waiting for further upload
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()
its sure work :)
great!
can be enhanced more by making it more user friendly and removing ambiguity a bit more.
Thank you.
what if the matrix is not uniform i.e one is 3*3 and other is 3*4 how to multiply them??
Iam learning theory from navin sir and code form u great!
Can you explain
m=m+array1[i][t]*array2[t][j]
works for all kinds of matrix, changing the formation of a and b will work too...
from numpy import *
a=array([[2,3],[4,5]])
b=array([[2,4,3],[8,4,2]])
c=zeros(len(a)*len(b[0]),int)
c=c.reshape(len(a),len(b[0]))
i=0
k=0
while i
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))
the best and enthusiastic video explanation
By taking user input and using inbuilt numpy library:
import numpy as np
from numpy import *
import mathematics as ma
print(" .........Enter the 1st matrix.........")
print()
print("Enter the no of rows: ",end=" ")
rows1=int(input())
print("Enter the no of column: ", end=" ")
columns1=int(input())
m=[]
print("Enter all the elements of the matrix following rows to column: ")
for i in range(rows1*columns1):
x=float(input())
m.append(x)
fmatrix1=np.array(m)
m1=fmatrix1.reshape(rows1,columns1)
print()
print("The 1st Matrix is: ")
print(m1)
print()
print(" .........Enter the 2nd matrix.........")
print()
print("Enter the no of rows: ",end=" ")
rows2=int(input())
print("Enter the no of column: ", end=" ")
columns2=int(input())
n=[]
print("Enter all the elements of the matrix following rows to column: ")
for i in range(rows2*columns2):
y=float(input())
n.append(y)
fmatrix2=np.array(n)
m2=fmatrix2.reshape(rows2,columns2)
print()
print("The 2nd Matrix is: ")
print(m2)
print()
print()
print("The multiplication of two matrices is: ")
ma1=matrix(m1)
ma2=matrix(m2)
ma3=ma1*ma2
print()
print(ma3)
print()
print(np.dot(ma1,ma2))
The way u teach is really awesome sir. Thank you sir
Sir, I have become a fan of yours.🙌🙌
Thankyou bro... U r the best teacher of coding☺️.... superb explanation 👌👌
Hi Mr Navin, i got one question and hope that you could address that. For the matrix multiplication what is the scenario that we will actually be using. For statistical calculations?
Well he didn't.
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)
Hey Navin, I think in the latest update numpy has deprecated i.e. it do not recommend the use of matrix and warns that it would be removed in future
Yes bro ur right
Sir m really glad. The way in which you teaches is really very amazing. You put your hard work for making this video, thank you so much for that. ☺️
# Multiplication (R=AxB)
===================
from numpy import *
A = array([
[1, 2, 3],
[4, 5, 6], # 2x3
])
B = array([
[1, 2, 3],
[2, 3, 4],
[4, 5, 6] # 3x3
])
# printing Matrix A
print("A =", end="")
for i in range(2): # A row
for j in range(3): # A column
print(end="\t")
print(A[i][j], end="")
print()
# printing Matrix B
print("
B =", end="")
for i in range(3): # B row
for j in range(3): # B column
print(end="\t")
print(B[i][j], end="")
print()
# creating Matrix R(AxB) blueprint
R = zeros(6, int).reshape(2, 3) # 2x3(A=row x B=column)
# AxB operation
sum = 0
for i in range(2): # R row
for j in range(3): # R column
for k in range(3): # A column
sum += A[i][k] * B[k][j]
R[i][j] = sum
sum = 0
# printing Matrix R(AxB)
print("
R =", end="")
for i in range(2): # R row
for j in range(3): # R column
print(end="\t")
print(R[i][j], end="")
print()
Why don't you print the matrix directly by saying print (R)
This is one of the best platform on the TH-cam to learn programming
Thank you telusko learning
Thank you so much sir !! You teach Python in most easiest & fun way .
Assignment:
from numpy import*
arr1= array([[1,2,3],[4,5,6]])
arr2= array([[7,8],[9,10],[11,12]])
q= array ([[0,0],
[0,0]])
for x in range(2):
for y in range (2):
for i in range(3):
q[x][y]=q[x][y]+ (arr1[x][i]*arr2[i][y])
print(q)
decent video on python
from numpy import *
ar = int(input('Enter no. of rows in matrix a:'))
ac = int(input('Enter no. of columns in matrix a:'))
br = int(input('Enter no. of rows in matrix b:'))
bc = int(input('Enter no. of columns in matrix b:'))
if ac != br:
print('Multiplication NOT possible')
else:
a = zeros((ar, ac), int)
b = zeros((br, bc), int)
c = zeros((ar, bc), int)
for i in range(ar):
for j in range(ac):
a[i, j] = int(input('Enter element row wise:'))
print('matrix A is
', a)
for i in range(br):
for j in range(bc):
b[i, j] = int(input('Enter element row wise:'))
print('matrix B is
', b)
for i in range(ar):
for j in range(bc):
x = 0
for k in range(ac):
x = a[i, k] * b[k, j] + x
c[i, j] = x
print("A*B is
", c)
Multiplication of two matrix:
(r1, c1) = shape(m1)
(r2, c2) = shape(m2)
arr3 = array([],int)
x = 0
for i in range(r1):
for k in range(c2):
for j in range(c1):
x = (arr1[i][j]*arr2[j][k] ) + x
arr3 = append(arr3,x)
x=0
print(arr3)
m3 = matrix(reshape(arr3,(r1,c2)))
print(m3)
from numpy import *
tryarray = array([100,26,3,475,5])
max = tryarray[0]
currentmax = tryarray[0]
for x in tryarray:
print("currentmax", currentmax)
for j in range(1, len(tryarray)):
print("value of x", x)
y = tryarray[j]
print("value of y", y)
if(x>y):
max = x
print("current value",max)
else:
max = y
print("herevalue",max)
if(currentmax< max):
currentmax = max
print("finalvalue",currentmax)
Respected sir, You are very brilliant. You are covering each and every topics on python. I support you.
"program to multiply 2matrix usin 2d array using for loop"
from numpy import*
m1=array([[1,2,3],[4,5,6],[7,8,9]])
m2=array([[9,8,7],[6,5,4],[3,2,1]])
m3=[]
for i in range(0,3):
m3.append([])
for j in range(0,3):
sum=0
for k in range(0,3):
sum=sum+(m1[i][k]*m2[k][j])
m3[i].append(sum)
print(matrix(m3))
Could you please explain your code.. I have been trying to understand it but am not getting the for loops
@@Udayfah First run then debug to understand
Small Correction to your Code : Range of for loop should not be manually defined, it should take from array rows x columns as below.
from numpy import *
M1 = array([[1,3,5],[2,4,6]])
M2 = array([[2,4],[6,1],[3,5]])
M3 = []
print("
Matrix 1 :
", M1, "
Matrix 2 :
", M2)
if M1.shape[0] == M2.shape[1]:
for i in range(M1.shape[0]):
M3.append([])
for j in range(M2.shape[1]):
sum = 0
for k in range(M1.shape[1]):
sum = sum + (M1[i][k] * M2[k][j])
M3[i].append(sum)
print("
Matrix 1 & Matrix 2 Multiplication using for loop :
", matrix(M3))
Output :
Matrix 1 :
[[1 3 5]
[2 4 6]]
Matrix 2 :
[[2 4]
[6 1]
[3 5]]
Matrix 1 & Matrix 2 Multiplication using for loop :
[[35 32]
[46 42]]
@@akashtalla3136 thanks for this correction
You wanted to know did I enjoy this video? ...Absolutely YES from the moment you said "Welcome back Aliens" to "Buh-byeeeee" 😀
Sir similarly can u pls make a series on c++
Your teaching ideology is just great and better than other
Yes sir
Thank u sir u are doing an amazing job.
my code to multiply two matrices
from numpy import *
b = array([[1, 2, 3], [4, 5, 6]])
a = array([[4, 5], [6, 2], [3, 9]])
m = matrix(a)
n = matrix(b)
o = m.tolist()
p = n.tolist()
d = ''
for i in range(m.shape[0]):
for j in range(m.shape[0]):
v = 0
for k in range(m.shape[1]):
v += o[i][k] * p[k][j]
d += str(v) + ' '
if i != (n.shape[1] - 1):
d += '; '
print(matrix(d))
hey buddy, can you explain the if block to me.. I'm not getting it
@@OliFarhaan hmm let me explain it is like, I am concatenating in the string d all the products now to make it matrix I have to add some kind of delimiter i.e. ';' so, normally if we know the rule the size of the matrix will be number of row of first matrix (m)* number of columns of second matrix(n)....... so that's the thing ------> my if block is adding delimiter after getting n number of elements i.e 3 in our case
@VG ARMY hmm let me explain it is like, I am concatenating in the string d all the products now to make it matrix I have to add some kind of delimiter i.e. ';' so, normally if we know the rule the size of the matrix will be number of row of first matrix (m)* number of columns of second matrix(n)....... so that's the thing ------> my if block is adding delimiter after getting n number of elements i.e 3 in our case
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)
Thank you sir for this tutorial
I really like the way you teach
your lectures are so good , iam enjoying the way you teach python.
multiplication of two matrices
m=matrix('1 2 3;4 5 6;7 8 9')
m1=matrix('9 8 7;6 5 4;3 2 1')
m2 =matrix('0 0 0;0 0 0;0 0 0')
for y in range(0,3):
for j in range(0,3):
z=0
for k in range(0,3):
z=z+m[y,k]*m1[k,j]
m2[y,j]=z
print(m2,int)
Thanks 😊
Was looking for it here
Your videos all are awesome. It will help all people who try to learn python
Thank you so much sir
Hei , sir, you can see mine too. The channel has both Python Crash course tutorials, and R beginning course tutorials. All the videos have source files downloadable, you can check in the description of the video. Hope they will be useful.
Amazing sir. Could you please explain how to include an unknown variable like x or y in a matrix?
you are explaining every concept with good examples and ideas
really thanks a lot.
from numpy import *
arr1 = array([1,6,3,9])
b = 0
for i in range(len(arr1)):
if arr1[i] >= b:
b = arr1[i]
print(b)
# multiply 2 matrices using loop
import numpy as np
a = np.array([[1, 2, 12], [4, 9, 6]])
b = np.array([[7, 8], [9, 10], [11, 12]])
ma, na = a.shape
mb, nb = b.shape
c = np.full((ma, nb), 0)
for i in range(ma):
for j in range(nb):
for k in range(na):
c[i][j] += a[i][k] * b[k][j]
print(c)
from numpy import *
a1 = array([[1,2,3],[4,5,6]])
a2 = array([[1,2,7],[3,4,8],[5,6,9]])
if a1.shape[1] == a2.shape[0]:
r,c = a1.shape
# c = a2.shape[0] -> Second Matrix Rows
# r -> First Matrix Rows
prod = []
for i in range(0,r):
row = a1[i]
pr = []
for k in range(a2.shape[1]):
vals = sum([row[j] * a2[j][k] for j in range(c)])
pr.append(vals)
prod.append(pr)
print(prod)
else:
print('Multiplication Not possible!!')
Ur one the best attribute having person for python sir tq sir..... 🙏🙏🙏
from numpy import *
m1 = int(input('enter rows of the metrix '))
n1= int(input('eter rows of the metrix '))
A = zeros((m1,n1),int)
for i in range(m1):
for j in range(n1):
x=int(input('enter next element of the metrix'))
A[i][j]=x
print(A)
m2 = int(input('enter rows of the metrix '))
n2 = int(input('eter rows of the metrix '))
B = zeros((m2,n2),int)
for i in range(m2):
for j in range(n2):
x=int(input('enter next element of the metrix'))
B[i][j]=x
print(B)
C = zeros((m1,n2),int)
for i in range(m1):
for j in range(n2):
x=0
for k in range(m2):
x=x+A[i][k]*B[k][i]
C[i][j] = x
print(C)
from numpy import *
a=ones((3,3),int)
b=ones((3,3),int)
result=zeros((3,3),int)
print(a)
print(b)
for i in range(len(a)):
for j in range(len(b)):
for k in range(len(b)):
result[i][j]+=+(a[i][k]*b[k][j])
for r in result:
print(r)
from numpy import *
arr1=array([
[1,2,3],
[4,5,6]
])
arr2=array([
[7,8,9],
[10,11,12]
])
arr3=arr1*arr2
print(arr3)
solution for matrix multi[lication
# Multiplying 2 matrices of any order
m1 = matrix(input('Type the elements of the first matrix'))
m2 = matrix(input('Type the elements of the second matrix'))
c = matrix(array(zeros(m1.shape[0]*m2.shape[1], int).reshape(m1.shape[0],m2.shape[1])))
if m1.shape[1] != m2.shape[0]:
print('The matrices cannot be multiplied')
else:
print('The matrices can be multiplied')
for i in range(m1.shape[0]):
for j in range(m2.shape[1]):
for k in range(m2.shape[0]):
c[i,j] += m1[i,k] * m2[k,j]
for r in c:
print(r)
from numpy import *
m1 = array([
[1,2],
[3,4]
])
m2 = array([
[2,1],
[4,3]
])
m3=array([[0,0],
[0,0]])
for i in range(2):
for j in range(2):
for k in range(2):
m3[i,j] = m1[i,k] * m2[k,j] + m3[i,j]
print(m1)
print(m2)
print(m3)
Hi sir
Ur teaching is awesome and I have a question after watching all ur sessions I used to think that I need memorize certain functions what u have thought during the class
x1 = array(range(0,9)).reshape(3,3)
print(x1)
x2 = array(range(1,10)).reshape(3,3)
print(x2)
y=x1.dot(x2)
print(y)
from numpy import*
m1=int(input("enter the number of rows of 1 matrix"))
n1=int(input("enter the number of colmn of 1 matrix"))
mat1 =zeros((m1,n1),dtype=int)
u1=len(mat1)
for i in range(u1):
for j in range(len(mat1[i])):
x=int(input("enter the value"))
mat1[i][j]=x
print(mat1)
m2=int(input("enter the number of rows of 2 matrix"))
n2=int(input("enter the number of colmn of 2 matrix"))
mat2 =zeros((m2,n2),dtype=int)
u2=len(mat2)
for i in range(u2):
for j in range(len(mat2[i])):
x=int(input("enter the value"))
mat2[i][j]=x
print(mat2)
new=zeros((m1,n2),dtype=int)
for i in range(u1):
for j in range(len(mat2[0])):
for k in range(u2):
new[i][j]=new[i][j] + mat1[i][k] * mat2[k][j]
print("multiplaction of mat1 and mat2 is")
matr=matrix(new)
print(type(matr))
print(matr)
What a casual explanation sir
Q1.Solution :
# I will just try 2D matrix multiplication
from numpy import *
row1=2
col1=row2=3
col2=2
arr1=array([[1,2,4],
[3,5,6]])
arr2=array([[0,0],
[0,2],
[1,1]])
# row1,col1=row2,col2 and arr1,arr2 can be arranged a/c to user i/p
arr1f=arr1.flatten()
arr2f=arr2.flatten()
arr3=zeros(4)
for i1 in range(0,row1):
for j2 in range(0,col2):
store=0
for j1 in range(0, col1):
store = store + arr1[i1][j1] * arr2[j1][j2]
arr3[i1*col2+j2]=store
arr4=arr3.reshape(row1,col2)
print(arr4)
7:55 I like the way of correction of mistake, in mind something and uttering something
You are really a very good teacher , sir 👍👌👌👌👌👌💐
from numpy import *
a = array([[4, 5, 7, 8, 0],
[1, 2, 3, 4, 5, 6, 10]
])
b = a.flatten()
print(b)
o/p:
[list([4, 5, 7, 8, 0]) list([1, 2, 3, 4, 5, 6, 10])]
from numpy import *
m1=matrix('1 2 3;4 5 6;7 8 9')
m2=matrix('4 2 1;7 5 2;6 5 3')
sum=0
for i in range(len(m1)):
for j in range(len(m1)):
for k in range(len(m1)):
sum=m1[i,k]*m2[k,j]+sum
m3[i,j]=sum
sum=0
print(m3)
from numpy import *
arr1=array([[2,4,6],
[3,6,9],
[8,12,16]])
arr2=array([[4,8,12],
[5,10,15],
[16,20,24]])
print("Matrix Multiplication using inbuilt method:",arr1 * arr2)
print("Matrix Multiplication using for loop")
for i in range (3):
ctr=0
for j in range (3):
ctr=arr1[i][j]*arr2[i][j]
print(ctr,end=",")
print()
mission completed: final problem of solution
from numpy import *
R = int(input("Enter the size of rows:"))
C =int(input("Enter the size of columns:"))
m1 = zeros((R,C),int)
for i in range(R):
for j in range(C):
x =int(input("Enter the Element"))
m1[i][j] =x
R1 = int(input("Enter the size of rows:"))
C1 =int(input("Enter the size of columns:"))
m2 = zeros((R1,C1),int)
for i in range(R1):
for j in range(C1):
x =int(input("Enter the Element"))
m2[i][j] =x
print(m1)
print(m2)
R2 = int(input("Enter the size of rows:"))
C2 =int(input("Enter the size of columns:"))
m3 = zeros((R2,C2),int)
#itrate through row of m1
for i in range(len(m1)):
#itrate through colomns of m2
for j in range(len(m2[0])):
#itrate through row of m2
for k in range(len(m2)):
m3[i][j]+=m1[i][k]*m2[k][j]
print(m3)
output:Enter the size of rows:2
Enter the size of columns:2
Enter the Element1
Enter the Element2
Enter the Element3
Enter the Element4
Enter the size of rows:2
Enter the size of columns:2
Enter the Element1
Enter the Element2
Enter the Element3
Enter the Element4
[[1 2]
[3 4]]
[[1 2]
[3 4]]
Enter the size of rows:2
Enter the size of columns:2
[[ 7 10]
[15 22]]
there is a mistake
C should be equal to R1 for matrices to be multiplicable
Can you help with python interview questions
finally got output :)
from numpy import *
from array import *
a1 = array('i',[1,2,3,4])
print(a1)
a2 = array('i',[5,6,7,8])
print(a2)
a3 = array('i',[])
m1 = matrix(a1)
m2 = matrix(a2)
print(m1)
print(m2)
for j in range(1):
for k in range(2):
x = (a1[j]*a2[k])+(a1[j+1]*a2[k+2])
a3.append(x)
for i in range(2):
for e in range(1,2,1):
y = (a1[e+1]*a2[i])+(a1[e+2]*a2[i+2])
a3.append(y)
print(a3)
m3 = matrix(a3)
print(m3)
Excellent. Provided explanation is spot on and very clear
The answer of the assignment is:
import numpy as np
R = int(input("Enter the number of rows:"))
C = int(input("Enter the number of columns:"))
print("Enter {}entries in a single line (separated by space): ".format(R*C))
#The format() is a method which replaces {} in print function and put the values there.
# User input of entries in a
# single line separated by space
entries = list(map(int, input().split()))
#The split() method splits a string into a list.You can specify the separator, default separator is any whitespace.
#string.split(separator, maxsplit)
if (len(entries))
copy paste
Ain't No way , You are replying to a 3 year old comment with that bot ass name.
@@GowthamChandmanikanta
your explanation was so good through practically................
easy to understand keep going on ahead
#Matrix multiplication 3*2 with 2*3 resulting 3*3
from numpy import *
M1=array([
[7,8],
[9,10],
[11,12]
])
M2=array([
[1,2,3],
[4,5,6]
])
M_res=array([[0,0,0],[0,0,0],[0,0,0]])
for i in range(3):
for j in range(3):
for k in range(2):
M_res[i][j]+=M1[i][k]*M2[k][j]
print(M_res)
Thank you always for your tutorial videos! They're very helpful!
Multiplication Code:
from numpy import *
m1 = matrix('4 -3 2; -3 2 9; 9 2 -1')
m2 = matrix('1 -4 7; 1 1 -1; 10 -8 -2')
shapem1 = m1.shape
shapem2 = m2.shape
if shapem1[1] != shapem2[0]:
print("Condition for multiplication does not match")
else:
m3 = m1 * m2
print('result with in-built multiplication: ')
print(m3)
print()
resultm = zeros((shapem1[0],shapem2[1]))
for i in range(shapem1[0]):
for n in range(shapem2[1]):
for j in range(shapem1[1]):
m = j
resultm[i, n] = resultm[i, n] + m1[i, j] * m2[m, n]
print('result with self-made multiplication: ')
print(resultm)
print('End')
from numpy import *
m1 = matrix(' 1 2 3; 4 5 6; 7 8 9')
m2 = matrix('9 8 7; 6 5 4; 3 2 1')
print('m1=',m1,'
','m2=',m2)
m3=zeros((m1.shape[0],m2.shape[1]),int) # blank matrix which will be 3*3 (Row of m1 and Col of m2)
print(m3)
for i in range(m1.shape[0]): # row in 1st matrix
for j in range(m2.shape[1]): #col in 2nd matrix
for k in range(m1.shape[1]): #col in 1st matrix
m3[i,j]= m3[i,j]+ (m1[i,k]* m2[k,j])
print((m3))
For 3 x 3 matrix (eagerly waiting for reply)
m1 = np.array([ [1,2,3],[6,4,5],[1,6,7]
])
m2 = np.array([ [1,2,3],[6,8,5],[2,6,7]
])
k = []
for i in range(len(m1)):
for j in range(len(m2)):
x = sum(m1[i]*m2[:,j])
k.append(x)
m3 = np.array((k[0:3],k[3:6],k[6:9]))
print(m3)
multiplication code by taking user input
from numpy import *
a1=array([ [1,2,3], [4,5,6],[5,6,8],[3,7,1] ])
r1=int(input("enter no. of rows of mat 1 : "))
c1= int(input("enter no. of columns of mat 1 : "))
r2=c1
c2=int(input("enter no. of columns of mat 2 : "))
a1=zeros(r1*c1,int).reshape(r1,c1)
a2=zeros(r2*c2,int).reshape(r2,c2)
a3=zeros(r1*c2,int).reshape(r1,c2)
for i in range(r1):
for j in range(c1):
a1[i][j]=int(input("enter a1 element : "))
for i in range(r2):
for j in range(c2):
a2[i][j]=int(input("enter a2 element : "))
m1=matrix(a1);
print("m1 : ",m1)
m2=matrix(a2);
print("m2 : ",m2)
for i in range(r1):
for j in range(c2):
for k in range(r2):
a3[i][j]=a1[i][k]*a2[k][j] + a3[i][j]
m3=matrix(a3);
print("m3 : ",m3)
Hi navin
Can you please make videos on graphs and implementation using python
I really like your approach and way of teaching
please make video on graphs
Sir please continue making videos on different types of modules libraries and packages by different types of numerical examples in python
Hello Sir! i tried an assignment problem myself. Whether i am not sure that code i had done is correct answer for your question or not. plz let me know if it is wrong.
Que:Write a code to multiply 2 matrices using 2D array and using loop.
Ans:
from numpy import *
arr1 = array ([
[1,2,3],
[5,6,8],
[3,4,6]
])
m1 = matrix(arr1)
arr2 = array ([
[4,2,1],
[8,6,7],
[3,4,7]
])
m2 = matrix(arr2)
print(m1)
print(m2)
for i in range(1):
m1 = m1 * m2
m3 = m1
print(m3)
from numpy import *
m1 = [[1,2,3],
[9,5,6],
[7,8,10]]
m2 = [[2,9,6],
[1,7,2],
[1,5,9]]
res =[[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(3):
for j in range(3):
for k in range(3):
res[i][j] += m1[i][k] * m2[k][j]
for r in res:
print(r)
ans:
import numpy as np
# input by uesers
a1 = np.array([[1, 2, 3], [6, 5, 4]])
a2 = np.array([[6, 5], [9, 5], [6, 2]])
def matrixMul(arr1, arr2):
shape1 = np.shape(arr1)
shape2 = np.shape(arr2)
if shape1[1] != shape2[0]:
print("can not be multiplied.")
else:
arrMul = np.zeros((shape1[0], shape2[1]))
for i in range(shape1[0]): # shape1[0] is 2
for j in range(shape2[1]): # shape2[1] is 2
for k in range(shape1[1]): # either shape1[1] or shape2[0] is ok
arrMul[i][j] += arr1[i][k] * arr2[k][j]
return arrMul
res = matrixMul(a1, a2)
print(res)
so precious and thanks for your power tutorial......hats off to you and your team!
Multiplication of Two Matrix by taking Input From User
from numpy import *
a1 = int(input("Enter number of rows "))
b1 = int(input("Enter number of Columns "))
a2 = int(input("Enter number of rows "))
b2 = int(input("Enter number of Columns "))
if b1 == a2:
c = b1
ab1 = a1 * b1
ab2 = a2 * b2
a1b2 = a1 * b2
arr1 = (zeros(ab1).reshape(a1, b1))
arr2 = (zeros(ab2).reshape(a2, b2))
# print(arr1)
print("Enter numbers for First Table")
for i in range(a1):
for j in range(b1):
print("Enter the number in Row ", i, " Column ", j, end = " ")
arr1[i][j]= int(input())
print("Enter numbers for Second Table")
for i in range(a2):
for j in range(b2):
print("Enter the number in Row ", i, " Column ", j, end = " ")
arr2[i][j]= int(input())
print('First Table')
print(arr1)
print('Second Table')
print(arr2)
arr3 = (zeros(a1b2).reshape(a1, b2))
for i in range(a1):
for j in range(b2):
p = 0
for k in range(c):
p = (arr1[i][k] * arr2[k][j]) + p
arr3[i][j]= p
print("Multiplication of Both Tables ")
print(arr3)
else:
print("Number of columns of first arr are not equal to number of rows of second array")
from numpy import *
arr1 = array([[1,2,3],[4,5,6],[3,4,5]])
arr2 = array([[1,4,5],[5,4,3],[4,7,8]])
if(arr1.shape[1] == arr2.shape[0]):
arr3= []
for i in range(arr1.shape[0]):
for j in range(arr2.shape[1]):
y = 0
for k in range(arr2.shape[1]):
x = arr1[i][k]*arr2[k][j];
y = y+x
arr3.append(y)
m = matrix(arr3).reshape(1,3,3)
print(m)
else:
print("Array multiplication is not possible")
could you explain to me:
shape[1] : mean? how it runs?
your code is hard to understand, could you explain all to me?
How append is working in numpy ?
from numpy import *
arr= array([
[1,2,3],
[4,5,6],
[3,3,1]
])
m1= matrix(arr)
m2=m1
m3=matrix('0,0,0;0,0,0;0,0,0')
for k in range(len(m1)):
for i in range(len(m1)):
li=[]
for j in range(len(m2)):
li.append(m1[k,j]*m2[j,i])
m3[k,i]=sum(li)
print(m3)
solution:
from numpy import *
m1 = matrix('1,2,3;4,5,6')
m2 = matrix('2,4;5,3;6,7')
m3 = matrix('0,0;0,0')
sum = 0
for i in range(2):
for j in range(2):
for k in range(3):
sum = sum +m1[i,k]*m2[k,j]
m3[i,j]=sum
sum = 0
print(m3)
It says error shapes (1,2)and (1,2) not aligned :2 (dim 1)! =1 (dim 0)
sir please make a video on this assignment multiply of two matrices its so difficult please make a video sir because my concept of matrices is not clear
Class 12th maths first chapter...watch neha mam video, you'll get it
from numpy import *
arr1=array([])
arr2=array([])
n1=int(input('Enter no. of values'))
for i in range(n1):
a=int(input('enter a number in array 1: '))
arr1=append(arr1, a)
for i in range(n1):
a = int(input('enter a number in array 2: '))
arr2=append(arr2, a)
arr1=arr1.reshape(2,2)
arr2=arr2.reshape(2,2)
m1=matrix(arr1)
m2=matrix(arr2)
m3=m1*m2
print(m3)
arr3=array([])
for i in range(2):
for j in range(2):
b = 0
for t in range(2):
a=arr1[i, t]*arr2[t, j]
b+=a
arr3=append(arr3, b)
m3=matrix(arr3)
m3=m3.reshape(2, 2)
print(m3)
Thank you very much. You are a genius .
Matrix multiplication:
from numpy import *
def mat_mul():
mat1 = array([
[1,2,3],
[4,5,6],
[7,8,9]
])
mat2 = array([
[4,5,6],
[1,2,3],
[7,8,9]
])
m1,n1 = mat1.shape
m2,n2 = mat2.shape
if n1 == m2:
l1, temp1, temp2 = [], [], []
for i in range(m1):
k = 0
while k
This is so helpful!!!
matrix multiplication
arr1=[[1,2,3],
[3,4,5],
[5,6,1]]
arr2=[[3,4,5],
[6,1,2],
[4,5,6]]
arr3=[[0,0,0],
[0,0,0],
[0,0,0]]
for b in range(len(arr1)):
for c in range(len(arr2[0])): #taking coloumn wise values in arr2
for h in range(len(arr2)):
arr3[b][c] +=arr1[b][h]*arr2[h][c]
for j in arr3:
print(j)
##hope this helps
from numpy import*
m = int(input('1st Matrix:- Enter the no. of rows: '))
n = int(input('1st Matrix:- Enter the no. of columns: '))
x = matrix([0 for i in range(m*n)])
for i in range(m*n):
x[0,i] = int(input("Enter the element for Matrix 1: "))
r = int(input('2nd Matrix:- Enter the no. of rows: '))
p = int(input('2nd Matrix:- Enter the no. of columns: '))
y = matrix([0 for i in range(r*p)])
for i in range(r*p):
y[0,i] = int(input("Enter the element for Matrix 2: "))
m1 = x.reshape(m,n)
m2 = y.reshape(r,p)
m3 = m1 * m2
print("First Matrix is below:-
", m1, "
", "Second Matrix is below:-
", m2, "
", "Multiplication of Matrix:-
", m3)