9.2 Collaborative Filtering | Using per-item features-- [Machine Learning| Andrew Ng]
 So, let's take a look at how we can develop a recommender system if we had features of each item or features of each movie. So here's the same data set that we had previously with the four users having rated some but not all of the five movies. What if we additionally have features of the movies? So here I've added two features, X1 and X2, that tell us how much each of these is a romance movie and how much each of these is an action movie. So for example, Love at Last is a very romantic movie, so this feature takes on 0.9, but it's not a lot of action movies, so this feature takes on 0. But it turns out Non-Stop Car Chases has just a little bit of romance in it, so it's 0.1, but it has a ton of action, so that feature takes on the value of 1.0. So you recall that I had used the notation NU to denote the number of users, which is 4, and M to denote the number of movies, which is 5. I'm going to also introduce N to denote the number of features we have here, and so N is equal to 2, because we have two features, X1 and X2, for each movie. With these features, we have, for example, that the features for movie 1, that is the movie Love at Last, would be 0.9, 0, and the features for the third movie, Cute Puppies of Love, would be 0.99, and 0. And let's start by taking a look at how we might make predictions for Alice's movie ratings. So for user 1, that is Alice, let's say we predict the rating for movie i as w dot x of feature i plus b. So this is just a lot like linear regression. For example, if we end up choosing the parameter w1 equal 5, 0, and say b1 is equal to 0, then at our prediction for movie 3, where the features are 0.99 and 0, which we just copied from here, first feature is 0.99, second feature is 0, our prediction would be w dot x3 plus b equals 0.99 times 5 plus 0 times 0, which turns out to be equal to 4.95. And this rating seems pretty plausible. It looks like Alice has given high ratings to Love at Last and Romance Forever to two highly romantic movies, but given low ratings to the action movies Nonstop Conchases and Souls vs Karate. So if we look at Cute Puppies of Love, well predicting that she might rate that 4.95 seems quite plausible. And so these parameters w and b for Alice seems like a reasonable model for predicting her movie ratings. Just to add a little bit of notation, because we have not just one user but multiple users, or really nu equals four users, I'm going to add a superscript 1 here to denote that this is the parameter w1 for user 1 and add a superscript 1 there as well. And similarly here and here as well, so that we would actually have different parameters for each of the four users of our data set. And more generally, in this model, we can for user j, not just user 1 now, we can predict user j's rating for movie i as wj dot product xi plus bj. So here the parameters wj and bj are the parameters used to predict user j's rating for movie i, which is a function of xi, which is a features of movie i. And this is a lot like linear regression, except that we're fitting a different linear regression model for each of the four users in the data set. So let's take a look at how we can formulate the cost function for this algorithm. As a reminder, our notation is that rij is equal to 1 if user j has rated movie i or zero otherwise, and yij is the rating given by user j on movie i. And on the previous slide, we defined wj, bj as a parameters for user j, and xi as the feature vector for movie i. So the model we have is for user j and movie i predict the rating to be wj dot product xi plus bj. I'm going to introduce just one new piece of notation, which is I'm going to use mj to denote the number of movies rated by user j. So if a user has rated four movies, then mj would be equal to four. And if a user has rated three movies, then mj would be equal to three. So what we'd like to do is to learn the parameters wj and bj given the data that we have, that is given the ratings a user has given of a set of movies. So the algorithm we're going to use is very similar to linear regression. So let's write out the cost function for learning the parameters wj and bj for a given user j. This focus on one user on user j for now, I'm going to use the mean squared error criteria. So the cost will be the prediction, which is wj dot xi plus bj minus the actual rating that the user had given so minus y i j squared. And we're trying to choose parameters w and b to minimize the squared error between the predicted rating and the actual rating that was observed. But the user hasn't rated all the movies. So if we're going to sum over this, we're going to sum over only over the values of i where r i j is equal to one. So we're going to sum only over the movies i that user j has actually rated. So that's what this is. Sum over all values of i where r i j is equal to one, meaning that user j has rated that movie i. And then finally, we can take the usual normalization one over two mj. And this is very much like the cost function we had for linear regression with m or really mj training examples where you're summing over the mj movies for which you have a rating taken a squared error and then normalizing by this one over two mj. And this is going to be a cost function j of wj bj. And if we minimize this as a function of wj and bj, then you should come up with a pretty good choice of parameters wj and bj for making predictions for user j's ratings. Let me add just one more term to this cost function, which is the regularization term to prevent overfitting. And so here's our usual regularization parameter lambda divided by two mj and then times the sum of the squared values of the parameters w. And so n is a number of numbers in xi and that's the same as the number of numbers in wj. If you were to minimize this cost function j as a function of w and b, you should get a pretty good set of parameters for predicting user j's ratings for other movies. Now before moving on, it turns out that for recommender systems, it would be convenient to actually eliminate this division by mj term. mj is just a constant in this expression and so even if you take it out, you should end up with the same value of w and b. Now let me take this cost function down here at the bottom and copy it to the next slide. So we have that to learn the parameters wj, bj for user j, we would minimize this cost function as a function of wj and bj. But instead of focusing on a single user, let's look at how we learn the parameters for all of the users. To learn the parameters w1, b1, w2, b2 through wnu, bnu for all of our n subscript u users, we would take this cost function on top and sum it over all the nu users. So we would have sum from j equals 1 to nu of the same cost function that we had written up above and this becomes the cost for learning all the parameters for all of the users. Even if we use gradient descent or any other optimization algorithm to minimize this as a function of w1, b1 all the way through wnu, bnu, then you have a pretty good set of parameters for predicting movie ratings for all the users. And you may notice that this algorithm is a lot like linear regression where that plays a role similar to the output f of x of linear regression, only now we're training a different linear regression model for each of the n subscript u users. So that's how you can learn parameters and predict movie ratings if you had access to these features x1 and x2 that tell you how much is each of the movies a romance movie and how much is each of the movies an action movie. But where did these features come from and what if you don't have access to such features that give you enough detail about the movies with which to make these predictions? In the next video we'll look at a modification of this algorithm that will let you make predictions, let you make recommendations even if you don't have in advance features that describe the items of the movies in sufficient detail to run the algorithm that we just saw. Let's go on and take a look at that in the next video.
[{"start": 0.0, "end": 7.5200000000000005, "text": " So, let's take a look at how we can develop a recommender system if we had features of"}, {"start": 7.5200000000000005, "end": 10.72, "text": " each item or features of each movie."}, {"start": 10.72, "end": 16.22, "text": " So here's the same data set that we had previously with the four users having rated some but"}, {"start": 16.22, "end": 19.240000000000002, "text": " not all of the five movies."}, {"start": 19.240000000000002, "end": 22.84, "text": " What if we additionally have features of the movies?"}, {"start": 22.84, "end": 29.88, "text": " So here I've added two features, X1 and X2, that tell us how much each of these is a romance"}, {"start": 29.88, "end": 34.24, "text": " movie and how much each of these is an action movie."}, {"start": 34.24, "end": 41.04, "text": " So for example, Love at Last is a very romantic movie, so this feature takes on 0.9, but it's"}, {"start": 41.04, "end": 44.879999999999995, "text": " not a lot of action movies, so this feature takes on 0."}, {"start": 44.879999999999995, "end": 52.28, "text": " But it turns out Non-Stop Car Chases has just a little bit of romance in it, so it's 0.1,"}, {"start": 52.28, "end": 58.16, "text": " but it has a ton of action, so that feature takes on the value of 1.0."}, {"start": 58.16, "end": 65.39999999999999, "text": " So you recall that I had used the notation NU to denote the number of users, which is"}, {"start": 65.39999999999999, "end": 69.2, "text": " 4, and M to denote the number of movies, which is 5."}, {"start": 69.2, "end": 74.92, "text": " I'm going to also introduce N to denote the number of features we have here, and so N"}, {"start": 74.92, "end": 80.19999999999999, "text": " is equal to 2, because we have two features, X1 and X2, for each movie."}, {"start": 80.19999999999999, "end": 86.56, "text": " With these features, we have, for example, that the features for movie 1, that is the"}, {"start": 86.56, "end": 94.2, "text": " movie Love at Last, would be 0.9, 0, and the features for the third movie, Cute Puppies"}, {"start": 94.2, "end": 101.08, "text": " of Love, would be 0.99, and 0."}, {"start": 101.08, "end": 108.82000000000001, "text": " And let's start by taking a look at how we might make predictions for Alice's movie ratings."}, {"start": 108.82, "end": 118.33999999999999, "text": " So for user 1, that is Alice, let's say we predict the rating for movie i as w dot x"}, {"start": 118.33999999999999, "end": 121.32, "text": " of feature i plus b."}, {"start": 121.32, "end": 125.72, "text": " So this is just a lot like linear regression."}, {"start": 125.72, "end": 136.16, "text": " For example, if we end up choosing the parameter w1 equal 5, 0, and say b1 is equal to 0, then"}, {"start": 136.16, "end": 144.64, "text": " at our prediction for movie 3, where the features are 0.99 and 0, which we just copied from"}, {"start": 144.64, "end": 154.04, "text": " here, first feature is 0.99, second feature is 0, our prediction would be w dot x3 plus"}, {"start": 154.04, "end": 164.76, "text": " b equals 0.99 times 5 plus 0 times 0, which turns out to be equal to 4.95."}, {"start": 164.76, "end": 166.92, "text": " And this rating seems pretty plausible."}, {"start": 166.92, "end": 172.72, "text": " It looks like Alice has given high ratings to Love at Last and Romance Forever to two"}, {"start": 172.72, "end": 178.44, "text": " highly romantic movies, but given low ratings to the action movies Nonstop Conchases and"}, {"start": 178.44, "end": 180.32, "text": " Souls vs Karate."}, {"start": 180.32, "end": 186.23999999999998, "text": " So if we look at Cute Puppies of Love, well predicting that she might rate that 4.95 seems"}, {"start": 186.23999999999998, "end": 188.23999999999998, "text": " quite plausible."}, {"start": 188.24, "end": 194.82000000000002, "text": " And so these parameters w and b for Alice seems like a reasonable model for predicting"}, {"start": 194.82000000000002, "end": 197.28, "text": " her movie ratings."}, {"start": 197.28, "end": 202.44, "text": " Just to add a little bit of notation, because we have not just one user but multiple users,"}, {"start": 202.44, "end": 208.64000000000001, "text": " or really nu equals four users, I'm going to add a superscript 1 here to denote that"}, {"start": 208.64000000000001, "end": 215.48000000000002, "text": " this is the parameter w1 for user 1 and add a superscript 1 there as well."}, {"start": 215.48, "end": 223.48, "text": " And similarly here and here as well, so that we would actually have different parameters"}, {"start": 223.48, "end": 226.56, "text": " for each of the four users of our data set."}, {"start": 226.56, "end": 234.73999999999998, "text": " And more generally, in this model, we can for user j, not just user 1 now, we can predict"}, {"start": 234.73999999999998, "end": 242.76, "text": " user j's rating for movie i as wj dot product xi plus bj."}, {"start": 242.76, "end": 251.76, "text": " So here the parameters wj and bj are the parameters used to predict user j's rating for movie"}, {"start": 251.76, "end": 256.68, "text": " i, which is a function of xi, which is a features of movie i."}, {"start": 256.68, "end": 261.56, "text": " And this is a lot like linear regression, except that we're fitting a different linear"}, {"start": 261.56, "end": 265.44, "text": " regression model for each of the four users in the data set."}, {"start": 265.44, "end": 272.32, "text": " So let's take a look at how we can formulate the cost function for this algorithm."}, {"start": 272.32, "end": 279.12, "text": " As a reminder, our notation is that rij is equal to 1 if user j has rated movie i or"}, {"start": 279.12, "end": 286.44, "text": " zero otherwise, and yij is the rating given by user j on movie i."}, {"start": 286.44, "end": 293.92, "text": " And on the previous slide, we defined wj, bj as a parameters for user j, and xi as the"}, {"start": 293.92, "end": 296.88, "text": " feature vector for movie i."}, {"start": 296.88, "end": 303.4, "text": " So the model we have is for user j and movie i predict the rating to be wj dot product"}, {"start": 303.4, "end": 307.08, "text": " xi plus bj."}, {"start": 307.08, "end": 312.76, "text": " I'm going to introduce just one new piece of notation, which is I'm going to use mj"}, {"start": 312.76, "end": 316.32, "text": " to denote the number of movies rated by user j."}, {"start": 316.32, "end": 321.56, "text": " So if a user has rated four movies, then mj would be equal to four."}, {"start": 321.56, "end": 326.46, "text": " And if a user has rated three movies, then mj would be equal to three."}, {"start": 326.46, "end": 335.76, "text": " So what we'd like to do is to learn the parameters wj and bj given the data that we have, that"}, {"start": 335.76, "end": 341.38, "text": " is given the ratings a user has given of a set of movies."}, {"start": 341.38, "end": 347.2, "text": " So the algorithm we're going to use is very similar to linear regression."}, {"start": 347.2, "end": 352.56, "text": " So let's write out the cost function for learning the parameters wj and bj for a given user"}, {"start": 352.56, "end": 353.56, "text": " j."}, {"start": 353.56, "end": 361.72, "text": " This focus on one user on user j for now, I'm going to use the mean squared error criteria."}, {"start": 361.72, "end": 372.0, "text": " So the cost will be the prediction, which is wj dot xi plus bj minus the actual rating"}, {"start": 372.0, "end": 378.26, "text": " that the user had given so minus y i j squared."}, {"start": 378.26, "end": 384.68, "text": " And we're trying to choose parameters w and b to minimize the squared error between the"}, {"start": 384.68, "end": 390.56, "text": " predicted rating and the actual rating that was observed."}, {"start": 390.56, "end": 393.76, "text": " But the user hasn't rated all the movies."}, {"start": 393.76, "end": 400.36, "text": " So if we're going to sum over this, we're going to sum over only over the values of"}, {"start": 400.36, "end": 406.68, "text": " i where r i j is equal to one."}, {"start": 406.68, "end": 414.34000000000003, "text": " So we're going to sum only over the movies i that user j has actually rated."}, {"start": 414.34000000000003, "end": 415.72, "text": " So that's what this is."}, {"start": 415.72, "end": 422.06, "text": " Sum over all values of i where r i j is equal to one, meaning that user j has rated that"}, {"start": 422.06, "end": 424.0, "text": " movie i."}, {"start": 424.0, "end": 432.3, "text": " And then finally, we can take the usual normalization one over two mj."}, {"start": 432.3, "end": 438.24, "text": " And this is very much like the cost function we had for linear regression with m or really"}, {"start": 438.24, "end": 444.12, "text": " mj training examples where you're summing over the mj movies for which you have a rating"}, {"start": 444.12, "end": 448.76, "text": " taken a squared error and then normalizing by this one over two mj."}, {"start": 448.76, "end": 458.82, "text": " And this is going to be a cost function j of wj bj."}, {"start": 458.82, "end": 466.52, "text": " And if we minimize this as a function of wj and bj, then you should come up with a pretty"}, {"start": 466.52, "end": 472.2, "text": " good choice of parameters wj and bj for making predictions for user j's ratings."}, {"start": 472.2, "end": 476.03999999999996, "text": " Let me add just one more term to this cost function, which is the regularization term"}, {"start": 476.03999999999996, "end": 478.28, "text": " to prevent overfitting."}, {"start": 478.28, "end": 485.12, "text": " And so here's our usual regularization parameter lambda divided by two mj and then times the"}, {"start": 485.12, "end": 492.88, "text": " sum of the squared values of the parameters w."}, {"start": 492.88, "end": 498.36, "text": " And so n is a number of numbers in xi and that's the same as the number of numbers in"}, {"start": 498.36, "end": 500.64, "text": " wj."}, {"start": 500.64, "end": 506.9, "text": " If you were to minimize this cost function j as a function of w and b, you should get"}, {"start": 506.9, "end": 513.12, "text": " a pretty good set of parameters for predicting user j's ratings for other movies."}, {"start": 513.12, "end": 519.4, "text": " Now before moving on, it turns out that for recommender systems, it would be convenient"}, {"start": 519.4, "end": 525.52, "text": " to actually eliminate this division by mj term."}, {"start": 525.52, "end": 530.4, "text": " mj is just a constant in this expression and so even if you take it out, you should end"}, {"start": 530.4, "end": 533.68, "text": " up with the same value of w and b."}, {"start": 533.68, "end": 540.86, "text": " Now let me take this cost function down here at the bottom and copy it to the next slide."}, {"start": 540.86, "end": 547.0, "text": " So we have that to learn the parameters wj, bj for user j, we would minimize this cost"}, {"start": 547.0, "end": 551.98, "text": " function as a function of wj and bj."}, {"start": 551.98, "end": 556.7, "text": " But instead of focusing on a single user, let's look at how we learn the parameters"}, {"start": 556.7, "end": 559.4, "text": " for all of the users."}, {"start": 559.4, "end": 569.54, "text": " To learn the parameters w1, b1, w2, b2 through wnu, bnu for all of our n subscript u users,"}, {"start": 569.54, "end": 576.06, "text": " we would take this cost function on top and sum it over all the nu users."}, {"start": 576.06, "end": 587.18, "text": " So we would have sum from j equals 1 to nu of the same cost function that we had written"}, {"start": 587.18, "end": 598.64, "text": " up above and this becomes the cost for learning all the parameters for all of the users."}, {"start": 598.64, "end": 604.84, "text": " Even if we use gradient descent or any other optimization algorithm to minimize this as"}, {"start": 604.84, "end": 612.84, "text": " a function of w1, b1 all the way through wnu, bnu, then you have a pretty good set of parameters"}, {"start": 612.84, "end": 616.4399999999999, "text": " for predicting movie ratings for all the users."}, {"start": 616.4399999999999, "end": 621.72, "text": " And you may notice that this algorithm is a lot like linear regression where that plays"}, {"start": 621.72, "end": 629.94, "text": " a role similar to the output f of x of linear regression, only now we're training a different"}, {"start": 629.94, "end": 636.0600000000001, "text": " linear regression model for each of the n subscript u users."}, {"start": 636.0600000000001, "end": 642.14, "text": " So that's how you can learn parameters and predict movie ratings if you had access to"}, {"start": 642.14, "end": 648.1600000000001, "text": " these features x1 and x2 that tell you how much is each of the movies a romance movie"}, {"start": 648.1600000000001, "end": 651.6600000000001, "text": " and how much is each of the movies an action movie."}, {"start": 651.66, "end": 657.18, "text": " But where did these features come from and what if you don't have access to such features"}, {"start": 657.18, "end": 662.06, "text": " that give you enough detail about the movies with which to make these predictions?"}, {"start": 662.06, "end": 668.1, "text": " In the next video we'll look at a modification of this algorithm that will let you make predictions,"}, {"start": 668.1, "end": 673.86, "text": " let you make recommendations even if you don't have in advance features that describe the"}, {"start": 673.86, "end": 679.06, "text": " items of the movies in sufficient detail to run the algorithm that we just saw."}, {"start": 679.06, "end": 681.9399999999999, "text": " Let's go on and take a look at that in the next video."}]