<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Non-Personalized Recommenders Assignment
Overview
This assignment will explore non-personalized recommendations. You will be given a 20x20 matrix where columns represent movies, rows represent users, and each cell represents a user-movie rating.
Deliverables
There are 4 deliverables for this assignment. Each deliverable represents a different analysis of the data provided to you. For each deliverable, you will submit a list of the top 5 movies as ranked by a particular metric. The 4 metrics are
Step1:  Loading the Data
Step2:  Non-Personalized Recommenders for Raiders of the Lost Ark
Step3:  Mean rating for Raiders of the Lost Ark (1981)
Step4:  Number of non-NA ratings for Raiders of the Lost Ark (1981)
Step5:  Percentage of ratings >=4 for Raiders of the Lost Ark (1981)
Step6:  Finding Association of Raiders of the Lost Ark (1981) with Star Wars Episode IV. The association with Star Wars Episode IV is defined as the number of users that rated BOTH Raiders of the Lost Ark (1981) and Star Wars Episode IV divided by the number of users that rated Star Wars Episode IV.
Step7:  Printing the Association of Raiders of the Lost Ark (1981) and Star Wars Episode IV
Step8:  Finding top 5 movies with the highest ratings
Making a Pandas Series with the index name equal to the movie and the entry equal to the mean rating for each movie. Sliced the column names from of the movie_data dataframe from [1
Step9:  Printing the top 5 rated movies
Step10:  Finding top 5 movies with the most ratings
Making a Pandas Series with the index name equal to the movie and the entry equal to the number of non-Na ratings for each movie. Sliced the column names from of the movie_data dataframe from [1
Step11:  Printing the top 5 movies with the most ratings
Step12:  Top 5 movies with Percentage of ratings >=4
Making a Pandas Series with the index name equal to the movie and the entry equal to the number of non-Na ratings for each movie. Sliced the column names from of the movie_data dataframe from [1
Step13:  Printing Top 5 movies with Percentage of ratings >=4
Step14:  Top 5 movies most similar to Star Wars (movie id =260)
Step15:  Finding Association of all movies with Star Wars Episode IV. The association with Star Wars Episode IV is defined as the number of users that rated BOTH movie i and Star Wars Episode IV divided by the number of users that rated Star Wars Episode IV. Below, we are looping over [2
Step16:  Printing Top 5 movies most similar to Star Wars (movie id =260)
<ASSISTANT_TASK:>
Python Code:
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('darkgrid')
%matplotlib inline
Explanation: Non-Personalized Recommenders Assignment
Overview
This assignment will explore non-personalized recommendations. You will be given a 20x20 matrix where columns represent movies, rows represent users, and each cell represents a user-movie rating.
Deliverables
There are 4 deliverables for this assignment. Each deliverable represents a different analysis of the data provided to you. For each deliverable, you will submit a list of the top 5 movies as ranked by a particular metric. The 4 metrics are:
Mean Rating: Calculate the mean rating for each movie, order with the highest rating listed first, and submit the top 5.
% of ratings 4+: Calculate the percentage of ratings for each movie that are 4 or higher. Order with the highest percentage first, and submit the top 5.
Rating Count: Count the number of ratings for each movie, order with the most number of ratings first, and submit the top 5.
Top 5 Star Wars: Calculate movies that most often occur with Star Wars: Episode IV - A New Hope (1977) using the (x+y)/x method described in class. In other words, for each movie, calculate the percentage of Star Wars raters who also rated that movie. Order with the highest percentage first, and submit the top 5.
Importing Libraries
End of explanation
# Loading the data into a Pandas dataframe
movie_data = pd.read_csv('A1Ratings.csv')
# Looking at the first 5 rows of the dataframe
movie_data.head()
#printing the column names of the dataframe
movie_data.columns
# Summarizing the data in the movie_data dataframe
movie_data.describe()
Explanation: Loading the Data
End of explanation
# Storing the "1198: Raiders of the Lost Ark (1981)" data into an array
raid_lost_arc = movie_data["1198: Raiders of the Lost Ark (1981)"]
raid_lost_arc
Explanation: Non-Personalized Recommenders for Raiders of the Lost Ark
End of explanation
print '%.2f' % ( raid_lost_arc.mean() )
Explanation: Mean rating for Raiders of the Lost Ark (1981)
End of explanation
raid_lost_arc.count()
Explanation: Number of non-NA ratings for Raiders of the Lost Ark (1981)
End of explanation
print '%.1f' % ( (len(raid_lost_arc[raid_lost_arc>=4])/float(raid_lost_arc.count()))*100.0 )
Explanation: Percentage of ratings >=4 for Raiders of the Lost Ark (1981)
End of explanation
# First, storing the Star Wars count
star_wars_count = movie_data["260: Star Wars: Episode IV - A New Hope (1977)"].count()
# Then multiply the Raiders of the Lost Ark and Star Wars data.
# non-NA values will be the ones where both entries do not have NA. Then, count these entries
rad_arc_star_wars_count = (movie_data["1198: Raiders of the Lost Ark (1981)"]*movie_data["260: Star Wars: Episode IV - A New Hope (1977)"]).count()
Explanation: Finding Association of Raiders of the Lost Ark (1981) with Star Wars Episode IV. The association with Star Wars Episode IV is defined as the number of users that rated BOTH Raiders of the Lost Ark (1981) and Star Wars Episode IV divided by the number of users that rated Star Wars Episode IV.
End of explanation
print '%.1f' % ( (rad_arc_star_wars_count/float(star_wars_count))*100.0 )
Explanation: Printing the Association of Raiders of the Lost Ark (1981) and Star Wars Episode IV
End of explanation
rating_means = pd.Series([movie_data[col_name].mean() for col_name in movie_data.columns[1:]], 
                         index=movie_data.columns[1:])
Explanation: Finding top 5 movies with the highest ratings
Making a Pandas Series with the index name equal to the movie and the entry equal to the mean rating for each movie. Sliced the column names from of the movie_data dataframe from [1:] since the first column is the user id.
End of explanation
rating_means.sort_values(ascending=False)[0:5]
Explanation: Printing the top 5 rated movies
End of explanation
rating_count = pd.Series([movie_data[col_name].count() for col_name in movie_data.columns[1:]], 
                         index=movie_data.columns[1:])
Explanation: Finding top 5 movies with the most ratings
Making a Pandas Series with the index name equal to the movie and the entry equal to the number of non-Na ratings for each movie. Sliced the column names from of the movie_data dataframe from [1:] since the first column is the user id.
End of explanation
rating_count.sort_values(ascending=False)[0:5]
Explanation: Printing the top 5 movies with the most ratings
End of explanation
rating_positive = pd.Series([sum(movie_data[col_name]>=4)/float(movie_data[col_name].count()) for col_name in movie_data.columns[1:]], 
                             index=movie_data.columns[1:])
Explanation: Top 5 movies with Percentage of ratings >=4
Making a Pandas Series with the index name equal to the movie and the entry equal to the number of non-Na ratings for each movie. Sliced the column names from of the movie_data dataframe from [1:] since the first column is the user id.
End of explanation
rating_positive.sort_values(ascending=False)[0:5]
Explanation: Printing Top 5 movies with Percentage of ratings >=4
End of explanation
# First, storing the Star Wars ratings and the count of non-NA Star Wars ratings
star_wars_rat = movie_data["260: Star Wars: Episode IV - A New Hope (1977)"]
star_wars_count = float(movie_data["260: Star Wars: Episode IV - A New Hope (1977)"].count())
print star_wars_count
Explanation: Top 5 movies most similar to Star Wars (movie id =260)
End of explanation
sim_val = pd.Series( [ (movie_data[col_name]*star_wars_rat).count()/star_wars_count 
                     for col_name in movie_data.columns[2:] ], index=movie_data.columns[2:] )
Explanation: Finding Association of all movies with Star Wars Episode IV. The association with Star Wars Episode IV is defined as the number of users that rated BOTH movie i and Star Wars Episode IV divided by the number of users that rated Star Wars Episode IV. Below, we are looping over [2:] to not include Star Wars Episode IV in the Association calculation.
End of explanation
sim_val.sort_values(ascending=False)[0:5]
Explanation: Printing Top 5 movies most similar to Star Wars (movie id =260)
End of explanation
<END_TASK>