<jupyter_start><jupyter_text>Recommendations with MovieTweetings: Most Popular RecommendationNow that you have created the necessary columns we will be using throughout the rest of the lesson on creating recommendations, let's get started with the first of our recommendations.To get started, read in the libraries and the two datasets you will be using throughout the lesson using the code below.<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tests as t

%matplotlib inline

# Read in the datasets
movies = pd.read_csv('movies_clean.csv')
reviews = pd.read_csv('reviews_clean.csv')
del movies['Unnamed: 0']
del reviews['Unnamed: 0']
reviews.columns
reviews.head(5)<jupyter_output><empty_output><jupyter_text>1. How To Find The Most Popular MoviesFor this notebook, we have a single task.  The task is that no matter the user, we need to provide a list of the recommendations based on simply the most popular items.For this task, we will consider what is "most popular" based on the following criteria:* A movie with the highest average rating is considered best* With ties, movies that have more ratings are better* A movie must have a minimum of 5 ratings to be considered among the best movies* If movies are tied in their average rating and number of ratings, the ranking is determined by the movie that is the most recent ratingWith these criteria, the goal for this notebook is to take a **user_id** and provide back the **n_top** recommendations.  Use the function below as the scaffolding that will be used for all the future recommendations as well.<jupyter_code>df_movie_avgratings = reviews.groupby('movie_id', sort=False)['rating'].mean()
df_sorted_avgratings = df_movie_avgratings.sort_values(ascending=False)

df_movie_countratings = reviews.groupby('movie_id', sort=False)['rating'].count()
df_sorted_countratings = df_movie_countratings.sort_values(ascending=False)

df_movie_dateRecentDateRating = reviews.groupby('movie_id', sort=False)['date'].max()
df_sorted_dateRecentDateRating = df_movie_dateRecentDateRating.sort_values(ascending=False)
df_sorted_avgratings.head(5)
df_sorted_countratings.loc[89984]
df_sorted_countratings.loc[df_sorted_avgratings.index[1]]
def popular_recommendations(user_id, n_top):
    '''
    INPUT:
    user_id - the user_id of the individual you are making recommendations for
    n_top - an integer of the number recommendations you want back
    OUTPUT:
    top_movies - a list of the n_top recommended movies by movie title in order best to worst
    '''
    # Do stuff
    # retrieve first n_top movie_id elements from df_sorted_avgratings
    # find ties and separate them in groups
    # sort them based on number of ratings
    # find ties and sort based on most recent date of ratings
    df_topn_avgrat = df_sorted_avgratings.head(n_top)
    
    
    # find number of movies with minimum rating 5. if that is less than
    # n_top, then response is not possible
    
    return top_movies # a list of the n_top movies as recommended<jupyter_output><empty_output><jupyter_text>Usint the three criteria above, you should be able to put together the above function.  If you feel confident in your solution, check the results of your function against our solution. On the next page, you can see a walkthrough and you can of course get the solution by looking at the solution notebook available in this workspace.<jupyter_code># Put your solutions for each of the cases here

# Top 20 movies recommended for id 1

recs_20_for_1 = # Your solution list here

# Top 5 movies recommended for id 53968
recs_5_for_53968 = # Your solution list here

# Top 100 movies recommended for id 70000
recs_100_for_70000 = # Your solution list here

# Top 35 movies recommended for id 43
recs_35_for_43 = # Your solution list here


### You Should Not Need To Modify Anything In This Cell
ranked_movies = t.create_ranked_df(movies, reviews) # only run this once - it is not fast

# check 1 
assert t.popular_recommendations('1', 20, ranked_movies) == recs_20_for_1,  "The first check failed..."
# check 2
assert t.popular_recommendations('53968', 5, ranked_movies) == recs_5_for_53968,  "The second check failed..."
# check 3
assert t.popular_recommendations('70000', 100, ranked_movies) == recs_100_for_70000,  "The third check failed..."
# check 4
assert t.popular_recommendations('43', 35, ranked_movies) == recs_35_for_43,  "The fourth check failed..."

print("If you got here, looks like you are good to go!  Nice job!")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06393298059964726, 'fraction_numerical': 0.0222663139329806, 'mean_word_length': 5.017241379310345, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '131576', 'license': 'MIT', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1300, 'n_words': 553, 'path': 'lessons/Recommendations/1_Intro_to_Recommendations/.ipynb_checkpoints/2_Most_Popular_Recommendations-checkpoint.ipynb', 'repo_name': 's-arora-1987/DSND_Term2', 'size': '4536'}