{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/dv/107570dd01b_m3wvmvv0g9lm0000gn/T/ipykernel_18673/1882081200.py:4: DtypeWarning: Columns (10) have mixed types. Specify dtype option on import or set low_memory=False.\n", " df = pd.read_csv('../data/movies_metadata.csv')\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
adultbelongs_to_collectionbudgetgenreshomepageidimdb_idoriginal_languageoriginal_titleoverview...release_daterevenueruntimespoken_languagesstatustaglinetitlevideovote_averagevote_count
0False{'id': 10194, 'name': 'Toy Story Collection', ...30000000[{'id': 16, 'name': 'Animation'}, {'id': 35, '...http://toystory.disney.com/toy-story862tt0114709enToy StoryLed by Woody, Andy's toys live happily in his ......1995-10-30373554033.081.0[{'iso_639_1': 'en', 'name': 'English'}]ReleasedNaNToy StoryFalse7.75415.0
1FalseNaN65000000[{'id': 12, 'name': 'Adventure'}, {'id': 14, '...NaN8844tt0113497enJumanjiWhen siblings Judy and Peter discover an encha......1995-12-15262797249.0104.0[{'iso_639_1': 'en', 'name': 'English'}, {'iso...ReleasedRoll the dice and unleash the excitement!JumanjiFalse6.92413.0
2False{'id': 119050, 'name': 'Grumpy Old Men Collect...0[{'id': 10749, 'name': 'Romance'}, {'id': 35, ...NaN15602tt0113228enGrumpier Old MenA family wedding reignites the ancient feud be......1995-12-220.0101.0[{'iso_639_1': 'en', 'name': 'English'}]ReleasedStill Yelling. Still Fighting. Still Ready for...Grumpier Old MenFalse6.592.0
3FalseNaN16000000[{'id': 35, 'name': 'Comedy'}, {'id': 18, 'nam...NaN31357tt0114885enWaiting to ExhaleCheated on, mistreated and stepped on, the wom......1995-12-2281452156.0127.0[{'iso_639_1': 'en', 'name': 'English'}]ReleasedFriends are the people who let you be yourself...Waiting to ExhaleFalse6.134.0
4False{'id': 96871, 'name': 'Father of the Bride Col...0[{'id': 35, 'name': 'Comedy'}]NaN11862tt0113041enFather of the Bride Part IIJust when George Banks has recovered from his ......1995-02-1076578911.0106.0[{'iso_639_1': 'en', 'name': 'English'}]ReleasedJust When His World Is Back To Normal... He's ...Father of the Bride Part IIFalse5.7173.0
\n", "

5 rows × 24 columns

\n", "
" ], "text/plain": [ " adult belongs_to_collection budget \\\n", "0 False {'id': 10194, 'name': 'Toy Story Collection', ... 30000000 \n", "1 False NaN 65000000 \n", "2 False {'id': 119050, 'name': 'Grumpy Old Men Collect... 0 \n", "3 False NaN 16000000 \n", "4 False {'id': 96871, 'name': 'Father of the Bride Col... 0 \n", "\n", " genres \\\n", "0 [{'id': 16, 'name': 'Animation'}, {'id': 35, '... \n", "1 [{'id': 12, 'name': 'Adventure'}, {'id': 14, '... \n", "2 [{'id': 10749, 'name': 'Romance'}, {'id': 35, ... \n", "3 [{'id': 35, 'name': 'Comedy'}, {'id': 18, 'nam... \n", "4 [{'id': 35, 'name': 'Comedy'}] \n", "\n", " homepage id imdb_id original_language \\\n", "0 http://toystory.disney.com/toy-story 862 tt0114709 en \n", "1 NaN 8844 tt0113497 en \n", "2 NaN 15602 tt0113228 en \n", "3 NaN 31357 tt0114885 en \n", "4 NaN 11862 tt0113041 en \n", "\n", " original_title \\\n", "0 Toy Story \n", "1 Jumanji \n", "2 Grumpier Old Men \n", "3 Waiting to Exhale \n", "4 Father of the Bride Part II \n", "\n", " overview ... release_date \\\n", "0 Led by Woody, Andy's toys live happily in his ... ... 1995-10-30 \n", "1 When siblings Judy and Peter discover an encha... ... 1995-12-15 \n", "2 A family wedding reignites the ancient feud be... ... 1995-12-22 \n", "3 Cheated on, mistreated and stepped on, the wom... ... 1995-12-22 \n", "4 Just when George Banks has recovered from his ... ... 1995-02-10 \n", "\n", " revenue runtime spoken_languages \\\n", "0 373554033.0 81.0 [{'iso_639_1': 'en', 'name': 'English'}] \n", "1 262797249.0 104.0 [{'iso_639_1': 'en', 'name': 'English'}, {'iso... \n", "2 0.0 101.0 [{'iso_639_1': 'en', 'name': 'English'}] \n", "3 81452156.0 127.0 [{'iso_639_1': 'en', 'name': 'English'}] \n", "4 76578911.0 106.0 [{'iso_639_1': 'en', 'name': 'English'}] \n", "\n", " status tagline \\\n", "0 Released NaN \n", "1 Released Roll the dice and unleash the excitement! \n", "2 Released Still Yelling. Still Fighting. Still Ready for... \n", "3 Released Friends are the people who let you be yourself... \n", "4 Released Just When His World Is Back To Normal... He's ... \n", "\n", " title video vote_average vote_count \n", "0 Toy Story False 7.7 5415.0 \n", "1 Jumanji False 6.9 2413.0 \n", "2 Grumpier Old Men False 6.5 92.0 \n", "3 Waiting to Exhale False 6.1 34.0 \n", "4 Father of the Bride Part II False 5.7 173.0 \n", "\n", "[5 rows x 24 columns]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "df = pd.read_csv('../data/movies_metadata.csv')\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "50.0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Calculate the number of votes garnered by the 80th percentile movie\n", "m = df['vote_count'].quantile(0.80)\n", "m" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(8963, 24)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Only consider movies longer than 45 minutes and shorter than 300 minutes\n", "q_movies = df[(df['runtime'] >= 45) & (df['runtime'] <= 300)]\n", "\n", "#Only consider movies that have garnered more than m votes\n", "q_movies = q_movies[q_movies['vote_count'] >= m]\n", "\n", "#Inspect the number of movies that made the cut\n", "q_movies.shape" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.618207215134185" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Calculate C\n", "C = df['vote_average'].mean()\n", "C" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Function to compute the IMDB weighted rating for each movie\n", "def weighted_rating(x, m=m, C=C):\n", " v = x['vote_count']\n", " R = x['vote_average']\n", " # Compute the weighted score\n", " return (v/(v+m) * R) + (m/(m+v) * C)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Compute the score using the weighted_rating function defined above\n", "q_movies['score'] = q_movies.apply(weighted_rating, axis=1)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
titlevote_countvote_averagescoreruntime
10309Dilwale Dulhania Le Jayenge661.09.18.855148190.0
314The Shawshank Redemption8358.08.58.482863142.0
834The Godfather6024.08.58.476278175.0
40251Your Name.1030.08.58.366584106.0
12481The Dark Knight12269.08.38.289115152.0
2843Fight Club9678.08.38.286216139.0
292Pulp Fiction8670.08.38.284623154.0
522Schindler's List4436.08.38.270109195.0
23673Whiplash4376.08.38.269704105.0
5481Spirited Away3968.08.38.266628125.0
2211Life Is Beautiful3643.08.38.263691116.0
1178The Godfather: Part II3418.08.38.261335200.0
1152One Flew Over the Cuckoo's Nest3001.08.38.256051133.0
1176Psycho2405.08.38.245381109.0
351Forrest Gump8147.08.28.184252142.0
1184Once Upon a Time in America1104.08.38.183804229.0
1154The Empire Strikes Back5998.08.28.178656124.0
18465The Intouchables5410.08.28.176357112.0
289Leon: The Professional4293.08.28.170276110.0
3030The Green Mile4166.08.28.169381189.0
1170GoodFellas3211.08.28.160414145.0
2216American History X3120.08.28.159278119.0
116112 Angry Men2130.08.28.14078596.0
9698Howl's Moving Castle2049.08.28.138499119.0
2884Princess Mononoke2041.08.28.138264134.0
\n", "
" ], "text/plain": [ " title vote_count vote_average score \\\n", "10309 Dilwale Dulhania Le Jayenge 661.0 9.1 8.855148 \n", "314 The Shawshank Redemption 8358.0 8.5 8.482863 \n", "834 The Godfather 6024.0 8.5 8.476278 \n", "40251 Your Name. 1030.0 8.5 8.366584 \n", "12481 The Dark Knight 12269.0 8.3 8.289115 \n", "2843 Fight Club 9678.0 8.3 8.286216 \n", "292 Pulp Fiction 8670.0 8.3 8.284623 \n", "522 Schindler's List 4436.0 8.3 8.270109 \n", "23673 Whiplash 4376.0 8.3 8.269704 \n", "5481 Spirited Away 3968.0 8.3 8.266628 \n", "2211 Life Is Beautiful 3643.0 8.3 8.263691 \n", "1178 The Godfather: Part II 3418.0 8.3 8.261335 \n", "1152 One Flew Over the Cuckoo's Nest 3001.0 8.3 8.256051 \n", "1176 Psycho 2405.0 8.3 8.245381 \n", "351 Forrest Gump 8147.0 8.2 8.184252 \n", "1184 Once Upon a Time in America 1104.0 8.3 8.183804 \n", "1154 The Empire Strikes Back 5998.0 8.2 8.178656 \n", "18465 The Intouchables 5410.0 8.2 8.176357 \n", "289 Leon: The Professional 4293.0 8.2 8.170276 \n", "3030 The Green Mile 4166.0 8.2 8.169381 \n", "1170 GoodFellas 3211.0 8.2 8.160414 \n", "2216 American History X 3120.0 8.2 8.159278 \n", "1161 12 Angry Men 2130.0 8.2 8.140785 \n", "9698 Howl's Moving Castle 2049.0 8.2 8.138499 \n", "2884 Princess Mononoke 2041.0 8.2 8.138264 \n", "\n", " runtime \n", "10309 190.0 \n", "314 142.0 \n", "834 175.0 \n", "40251 106.0 \n", "12481 152.0 \n", "2843 139.0 \n", "292 154.0 \n", "522 195.0 \n", "23673 105.0 \n", "5481 125.0 \n", "2211 116.0 \n", "1178 200.0 \n", "1152 133.0 \n", "1176 109.0 \n", "351 142.0 \n", "1184 229.0 \n", "1154 124.0 \n", "18465 112.0 \n", "289 110.0 \n", "3030 189.0 \n", "1170 145.0 \n", "2216 119.0 \n", "1161 96.0 \n", "9698 119.0 \n", "2884 134.0 " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Sort movies in descending order of their scores\n", "q_movies = q_movies.sort_values('score', ascending=False)\n", "\n", "#Print the top 25 movies\n", "q_movies[['title', 'vote_count', 'vote_average', 'score', 'runtime']].head(25)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4" } }, "nbformat": 4, "nbformat_minor": 2 }