nowwe iterate over all the moviesfor in range(nmovies)movie_likeness[iall_correlations(reviews[:, ]reviews tmovie_likeness[ , - we set the diagonal to - otherwisethe most similar movie to any movie is itselfwhich is truebut very unhelpful this is the same trick we used in learning how to classify with real-world exampleswhen we first introduced the nearest neighbor classification based on this matrixwe can easily write function that estimates ratingdef nn_movie(movie_likenessreviewsuidmid)likes movie_likeness[midargsort(reverse the sorting so that most alike are in beginning likes likes[::- returns the rating for the most similar movie available for ell in likesif reviews[ ,ell return reviews[ ,ellhow well does the preceding function dofairly wellits rmse is only the preceding code does not show you all of the details of the crossvalidation while it would work well in production as it is writtenfor testingwe need to make sure we have recomputed the likeness matrix afresh without using the user that we are currently testing on (otherwisewe contaminate the test set and we have an inflated estimate of generalizationunfortunatelythis takes long timeand we do not need the full matrix for each user you should compute only what you need this makes the code slightly more complex than the preceding examples on the companion website for this bookyou will find code with all the hairy details there you will also find much faster implementation of the all_correlations function combining multiple methods we can now combine the methods given in the earlier section into single prediction for examplewe could average the predictions this is normally good enoughbut there is no reason to think that both predictions are similarly good and should thus have the exact same weight of it might be that one is better