#!/usr/bin/env python # coding: utf-8 # In[14]: import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt import warnings warnings.simplefilter('ignore') # In[15]: fandango = pd.read_csv(r"C:\Users\Teni\Desktop\Git-Github\06-Capstone-Project\fandango_scrape.csv") # In[16]: fandango # #### Understand the Data # In[17]: fandango.info() # In[18]: fandango.describe() # #### Explore the relationship between the Rating and Votes # In[19]: plt.figure(figsize=(10, 4), dpi=200) sns.scatterplot(data=fandango, x='RATING', y='VOTES'); # #### Calculating the correlation between the columns. # # This helps undersyand how closely knitted each column is to each other # In[20]: fandango.corr() # In[21]: fandango['YEAR'] = fandango['FILM'].str.extract(r'\((\d+)\)') # pd.to_datetime(fandango['YEAR']) # In[22]: fandango # In[24]: # fandango.drop('Numbers_in_brackets', axis=1, inplace=True) # In[25]: fandango # #### How many movies are in the Fandango Dataframe per year? # In[26]: fandango['YEAR'].value_counts() # #### TASK: Visualize the count of movies per year with a plot: # In[29]: # Not using the barplot because it'd require a Y variable- which we don't have or do not need. The countlot allows for only counting plt.figure(figsize=(10, 6), dpi=200) sns.countplot(data=fandango, x='YEAR'); # #### What are the 10 movies with the highest number of votes? # In[30]: fandango.loc[fandango['VOTES'].nlargest(10).index] # In[31]: # OR fandango.sort_values(by='VOTES', ascending=False).head(10) # #### How many movies have zero votes # In[104]: fandango[fandango['VOTES']==0] # In[105]: (fandango['VOTES']==0).value_counts() # In[106]: (fandango['VOTES']==0).sum() # #### Create DataFrame of only reviewed films by removing any films that have zero votes. # In[107]: fandango # In[108]: reviewed_films = fandango[fandango['VOTES']!=0] reviewed_films # #### Create a KDE plot (or multiple kdeplots) that displays the distribution of ratings that are displayed (STARS) versus what the true rating was from votes (RATING). Clip the KDEs to 0-5.** # In[109]: from matplotlib.ticker import MaxNLocator # Ensure MaxNLocator is imported plt.figure(figsize=[10,4], dpi=500) sns.kdeplot(reviewed_films['RATING'], clip=[0, 5], shade=True, label='True Rating') sns.kdeplot(reviewed_films['STARS'], clip=[0, 5], shade=True, label='Stars Displayed') plt.title('KDE Plot for True Rating & the Stars Displayed Comparism') # plt.xlim(0, 5) The clip already catered to the ranges # plt.ylim(0.0, 0.6) The clip already catered to the ranges # plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True)) # Display integers on x-axis # plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True)) # Display integers on y-axis plt.legend(loc=(1.05, 0.5)) plt.show() # #### TASK: Let's now actually quantify this discrepancy. Create a new column of the different between STARS displayed versus true RATING. Calculate this difference with STARS-RATING and round these differences to the nearest decimal point. # In[110]: reviewed_films['STARS_DIFF'] = round(reviewed_films['STARS'] - reviewed_films['RATING'], 2) # In[111]: reviewed_films # #### TASK: Create a count plot to display the number of times a certain difference occurs # In[112]: plt.figure(figsize=[12, 8], dpi=200) sns.countplot(data = reviewed_films, x='STARS_DIFF'); # #### TASK: We can see from the plot that one movie was displaying over a 1 star difference than its true rating! What movie had this close to 1 star differential? # In[115]: reviewed_films[reviewed_films['STARS_DIFF']>=1.0] # #### New Data # In[116]: all_sites = pd.read_csv(r"C:\Users\Teni\Desktop\Git-Github\06-Capstone-Project\all_sites_scores.csv") # In[117]: all_sites # In[121]: all_sites.head(5) # In[120]: all_sites.info() # In[119]: all_sites.describe() # #### Rotten Tomatoes # # Let's first take a look at Rotten Tomatoes. RT has two sets of reviews, their critics reviews (ratings published by official critics) and user reviews. # # **TASK: Create a scatterplot exploring the relationship between RT Critic reviews and RT User reviews.** # In[132]: plt.figure(figsize=[12, 8], dpi=200) sns.scatterplot(data=all_sites, x='RottenTomatoes', y= 'RottenTomatoes_User', s=80) plt.xlim(0,100) plt.ylim(0, 100); # #### Rotten Tomatoes # # Let's quantify this difference by comparing the critics ratings and the RT User ratings. We will calculate this with RottenTomatoes-RottenTomatoes_User. Note: diff here is Critics - User Score. So values closer to 0 means aggrement between Critics and Users. Larger positive values means critics rated much higher than users. Larger negative values means users rated much higher than critics. # # **TASK: Create a new column based off the difference between critics ratings and users ratings for Rotten Tomatoes. Calculate this with RottenTomatoes-RottenTomatoes_User** # In[133]: all_sites['diff'] = round(all_sites['RottenTomatoes'] - all_sites['RottenTomatoes_User'], 2) # In[134]: all_sites # #### Rotten Tomatoes # # Let's now compare the overall mean difference. Since we're dealing with differences that could be negative or positive, first take the absolute value of all the differences, then take the mean. This would report back on average to absolute difference between the critics rating versus the user rating. # # **TASK: Calculate the Mean Absolute Difference between RT scores and RT User scores as described above.** # In[136]: all_sites['diff'].apply(abs).mean() # #### Rotten Tomatoes # # # **TASK: Plot the distribution of the differences between RT Critics Score and RT User Score. There should be negative values in this distribution plot. Feel free to use KDE or Histograms to display this distribution.** # In[150]: plt.figure(figsize=[12, 6], dpi=200) sns.displot(data=all_sites,x='diff', kde=True, bins=25) plot.set_axis_labels("Rotten_Diff") plot.fig.suptitle('RT Critics Score minus RT User Score', y=1.02) # Title for the plot plt.show() # sns.displot(data=sample_ages, x='age', rug=True, bins=30, kde=True) # #### Rotten Tomatoes # # # **TASK: Now create a distribution showing the *absolute value* difference between Critics and Users on Rotten Tomatoes.** # In[158]: abs_mean = all_sites['diff'].apply(abs).mean() all_sites['task_need'] = round(abs_mean-all_sites['diff'], 2) all_sites # In[176]: plt.figure(figsize=[12, 8], dpi=200) sns.displot(x=all_sites['diff'].apply(abs), bins=25, kde=True) plt.title('Abs Difference between RT Critics Score and RT User Score') plt.show() # In[178]: plt.figure(figsize=[12, 8], dpi=200) sns.histplot(x=all_sites['diff'].apply(abs), bins=25, kde=True) plt.title('Abs Difference between RT Critics Score and RT User Score') plt.show() # Apparently the Histplot is neater and more srisp than the displot # #### Rotten Tomatoes # # # **TASK: What are the top 5 movies users rated higher than critics on average:** # In[216]: all_sites.info() # In[185]: print('Users Love but Critics Hate') all_sites.nsmallest(5, 'diff')[['FILM', 'diff']] # #### **OR** # In[194]: print('Users Love but Critics Hate') all_sites.sort_values('diff', ascending=True)[['FILM', 'diff']].head(5) # #### **OR** # In[199]: print('Users Love but Critics Hate') all_sites.sort_values('diff', ascending=False)[['FILM', 'diff']].tail(5) # #### Rotten Tomatoes # # # **TASK: Now show the top 5 movies critics scores higher than users on average.** # In[190]: print('Critics love, but Users Hate') all_sites.nlargest(5, 'diff')[['FILM', 'diff']] # #### **OR** # In[192]: print('Critics love, but Users Hate') all_sites.sort_values('diff', ascending=False)[['FILM', 'diff']].head(5) # ## MetaCritic # # Now let's take a quick look at the ratings from MetaCritic. Metacritic also shows an average user rating versus their official displayed rating. # # **TASK: Display a scatterplot of the Metacritic Rating versus the Metacritic User rating.** # In[201]: all_sites.info() # In[215]: plt.figure(figsize=[12, 8], dpi=200) sns.scatterplot(data= all_sites, x= 'Metacritic',y= 'Metacritic_User', s=80) plt.xlim(0, 100) plt.ylim(0, 10) plt.show() # #### IMDB # # # Finally let's explore IMDB. Notice that both Metacritic and IMDB report back vote counts. Let's analyze the most popular movies. # # **TASK: Create a scatterplot for the relationship between vote counts on MetaCritic versus vote counts on IMDB.** # In[217]: all_sites.info() # In[220]: plt.figure(figsize=[12, 8], dpi=200) sns.scatterplot(data=all_sites, x= 'Metacritic_user_vote_count', y='IMDB_user_vote_count',s=70) plt.show() # #### IMDB # # # **Notice there are two outliers here. The movie with the highest vote count on IMDB only has about 500 Metacritic ratings. What is this movie?** # # **TASK: What movie has the highest IMDB user vote count?** # In[238]: largest = all_sites[all_sites['Metacritic'] <=500] largest.nlargest(1, 'IMDB_user_vote_count') # #### **OR** # In[ ]: largest = all_sites[all_sites['Metacritic'] <=500] largest.sort_values('IMDB_user_vote_count', ascending=False).head(1) # #### IMDB # # # **TASK: What movie has the highest Metacritic User Vote count?** # In[240]: all_sites.nlargest(1, 'Metacritic_user_vote_count') # #### **OR** # In[242]: all_sites.sort_values( 'Metacritic_user_vote_count', ascending=False).head(1) # ### Fandago Scores vs. All Sites # # Finally let's begin to explore whether or not Fandango artificially displays higher ratings than warranted to boost ticket sales. # # # **TASK: Combine the Fandango Table with the All Sites table. Not every movie in the Fandango table is in the All Sites table, since some Fandango movies have very little or no reviews. We only want to compare movies that are in both DataFrames, so do an *inner* merge to merge together both DataFrames based on the FILM columns.** # In[245]: fandango.info() # In[246]: all_sites.info() # In[256]: df = pd.merge(fandango, all_sites,how='inner', on='FILM') # In[257]: df.info() # In[259]: df.head(5) # ### Normalize columns to Fandango STARS and RATINGS 0-5 # # Notice that RT,Metacritic, and IMDB don't use a score between 0-5 stars like Fandango does. In order to do a fair comparison, we need to *normalize* these values so they all fall between 0-5 stars and the relationship between reviews stays the same. # # **TASK: Create new normalized columns for all ratings so they match up within the 0-5 star range shown on Fandango. There are many ways to do this.** # # # In[263]: df['RT_norm']=np.round(df['RottenTomatoes']/20, 1) df['RTU_norm']=np.round(df['RottenTomatoes_User']/20, 1) # In[264]: df['Meta_norm']=np.round(df['Metacritic']/20, 1) df['MetaU_norm']=np.round(df['Metacritic_User']/2, 1) # In[265]: df['IMDB_norm']=np.round(df['IMDB']/2, 1) # In[266]: df.head() # **TASK: Now create a norm_scores DataFrame that only contains the normalizes ratings. Include both STARS and RATING from the original Fandango table.** # In[267]: norm_scores = df[['STARS', 'RATING', 'RT_norm', 'RTU_norm', 'Meta_norm', 'MetaU_norm', 'IMDB_norm']] # In[268]: norm_scores # In[269]: norm_scores.head(5) # ### Comparing Distribution of Scores Across Sites # # # Now the moment of truth! Does Fandango display abnormally high ratings? We already know it pushs displayed RATING higher than STARS, but are the ratings themselves higher than average? # # # **TASK: Create a plot comparing the distributions of normalized ratings across all sites. There are many ways to do this, but explore the Seaborn KDEplot docs for some simple ways to quickly show this. Don't worry if your plot format does not look exactly the same as ours, as long as the differences in distribution are clear.** # # In[277]: def move_legend(ax, new_loc, **kws): old_legend = ax.legend_ handles = old_legend.legendHandles labels = [t.get_text() for t in old_legend.get_texts()] title = old_legend.get_title().get_text() ax.legend(handles, labels, loc=new_loc, title=title, **kws) # In[284]: fig, ax = plt.subplots(figsize=(15,6),dpi=150) sns.kdeplot(data=norm_scores,clip=[0,5],shade=True,palette='Set1', ax=ax) move_legend(ax, "upper left") # **Clearly Fandango has an uneven distribution. We can also see that RT critics have the most uniform distribution. Let's directly compare these two.** # # **TASK: Create a KDE plot that compare the distribution of RT critic ratings against the STARS displayed by Fandango.** # In[285]: norm_scores.info() # In[297]: fig, ax = plt.subplots(figsize=[12, 8], dpi=200) sns.kdeplot(data=norm_scores[['RT_norm', 'STARS']], clip=[0, 5], shade=True, ax=ax) move_legend(ax, 'upper left') # #### **OR** # In[310]: fig = plt.figure(figsize=[12, 8], dpi=200) sns.kdeplot(norm_scores['RT_norm'], clip=[0, 5], shade=True, label='RT_norm') sns.kdeplot(norm_scores[ 'STARS'], clip=[0, 5], shade=True, label='Stars') plt.legend(loc=(0.01, 0.9)) plt.show() # **OPTIONAL TASK: Create a histplot comparing all normalized scores.** # In[311]: norm_scores.info() # In[328]: fig, ax= plt.subplots(figsize=[12, 8], dpi=200) sns.histplot(data=norm_scores, ax=ax, bins=50) move_legend(ax, "center left") plt.show() # # ### How are the worst movies rated across all platforms? # # **TASK: Create a clustermap visualization of all normalized scores. Note the differences in ratings, highly rated movies should be clustered together versus poorly rated movies. Note: This clustermap does not need to have the FILM titles as the index, feel free to drop it for the clustermap.** # In[331]: sns.clustermap(norm_scores); # In[336]: sns.clustermap(norm_scores, col_cluster=False, cmap='magma'); # **TASK: Clearly Fandango is rating movies much higher than other sites, especially considering that it is then displaying a rounded up version of the rating. Let's examine the top 10 worst movies. Based off the Rotten Tomatoes Critic Ratings, what are the top 10 lowest rated movies? What are the normalized scores across all platforms for these movies? You may need to add the FILM column back in to your DataFrame of normalized scores to see the results.** # In[342]: norm_scores # In[346]: norm_scores = df[['STARS','RATING','RT_norm','RTU_norm','Meta_norm','MetaU_norm','IMDB_norm','FILM']] # In[347]: norm_scores # In[350]: norm_scores.nsmallest(10, 'RT_norm') # ### **OR** # In[353]: norm_scores.sort_values('RT_norm', ascending = True).head(10) # norm_scores.sort_values('RT_norm', ascending = False).tail(10) # **FINAL TASK: Visualize the distribution of ratings across all sites for the top 10 worst movies.** # In[355]: norm_scores # In[368]: print('\n\n\n\n\n') # This signmifies some space before the plot or whatever comes next plt.figure(figsize=[12, 8], dpi=200) worst_films= norm_scores.nsmallest(10, 'RT_norm').drop('FILM', axis=1) sns.kdeplot(data= worst_films, shade=True, clip=[0, 5]) plt.title("Ratings for RT Critics' Worst Reviewed Films") plt.show() # --- # ---- # # # # **Final thoughts: Wow! Fandango is showing around 3-4 star ratings for films that are clearly bad! Notice the biggest offender, [Taken 3!](https://www.youtube.com/watch?v=tJrfImRCHJ0). Fandango is displaying 4.5 stars on their site for a film with an [average rating of 1.86](https://en.wikipedia.org/wiki/Taken_3#Critical_response) across the other platforms!** # In[371]: norm_scores.iloc[25] # In[ ]: