In this video I show you how to solve Deep-ML Problem 1: Matrix-Vector Dot Product in two ways: using explicit native python, and a faster way using numpy. Hi everybody today we're gonna do some more ML coding so we're here at Deep ML com and we're going to start with the first problem matrix vector dot product so this problem what we need to do oops so reset this over here so we didn't see what happened what we're gonna do is we're gonna write a Python function that computes the dot products between the matrix and vector the function should return a list result with the resulting vector if the operation is valid we'll talk about what that means our minus 1 if the matrix dimensions don't match a matrix can be dotted with a vector these are also the matrix would be a list of lists over here only if the number of columns is equal to the length of the vector so we'll see how to check that for example if an n by m matrix requires an m length vector and here we have kind of an explanation of what that is so what we want to do we got a list of lists over here and the Matrix B so there's actually two ways to do this I'll show you the more explicit Python way and I'll show you how to solve this also using non pie which makes everything much much easier so let's first solve this using the more explicit way cause I think that's a little learn a little more from that so we'll have our output list and now we're gonna start to populate it with the resulting dot products so we're gonna iterate through the matrix a and now what we're gonna do is try to try to keep track of the sum so this would be the resulting the resulting output and we're gonna start set it to zero cause we're gonna see we're gonna start adding elements to it and now I'm gonna do the length of the vector B we can't multiply them cause we will just have a mismatch of the shape this is exactly what they want so if if any of these rows is not equal to the length of the beam they want us to return minus 1 as you can see over here turn minus 1 otherwise let's start doing this multiplication so I'm gonna use a cool thing that I like called zip and python so what we wanna do is we'll take the matrix element and the row element and will zip which means we're iterating across both the row and the vector d at the same time and we're making sure that these two are matched so it will take the corresponding bro element and the vector element and now what we want to do is multiply them and add them to our sum so this is how we do a matrix multiplication take each element from the row vector of the matrix and from sorry this is my bad I have typo here from the row element and from the vector element got him having steak over there now we fixed it and what we're gonna do is we add them to the sum and then we add this output of the sum to our to our output vector and then we just return it so as we if we recap we started with an empty list now we go over each row and then we start with the sum if the shape is there's a shape mismatch we turn minus 1 as we needed to we iterate through elements from both the row and our vector B and then this is how we do the dock product we sum up all the multiplications and then we add this to our output ring and that's that so let's see if this runs properly and it takes a little bit of time and yes it does this also includes a test case where the shapes don't match for example this one now let's see how we can do this in Python in Nun Pi which is a little more efficient because here we're doing double for loop in Python which is very slow so what we're gonna do is we're gonna import Nun Pi as and P and what we're gonna do is we're gonna try to convert both of these into an umpi array so we'll convert a into an Pi array and will convert the into an empire now if we have a very special situation where there's a list of list that one of them is a different size this line will fail in this case we would need to handle this with a certain handling of exceptions but it's not gonna happen over here but just so you know this might fail if there's a list of lists that isn't the same shape shape what they designed this for is to make sure that the matrix is valid so what we're gonna do is check the shapes so we want the shape of the the number of columns which is multiplied by B so this is the shape of each row and the shape of B and this is what we want to be compatible if it's not we need to take return minus 1 otherwise what we do is we need to just return a a times B when you do this with with the at sign this is how you do matrix multiplication with non pine and we actually need to convert this to list so we're going to return it as a list cause we see we need to return has a list this is a little more compact and it's more explicit also what this is checking and usually this is gonna be way way faster than a 44 looking python









