Skip to content

Instantly share code, notes, and snippets.

@pythrick
Created June 28, 2020 20:01
Show Gist options
  • Select an option

  • Save pythrick/9db4243246f0213426576ffd3da0a585 to your computer and use it in GitHub Desktop.

Select an option

Save pythrick/9db4243246f0213426576ffd3da0a585 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"unames = ['user_id', 'gender', 'age', 'occupation', 'zip']\n",
"users = pd.read_table('datasets/movielens/users.dat', sep='::',\n",
" header=None, names=unames, engine='python')\n",
"\n",
"rnames = ['user_id', 'movie_id', 'rating', 'timestamp']\n",
"ratings = pd.read_table('datasets/movielens/ratings.dat', sep='::',\n",
" header=None, names=rnames, engine='python')\n",
"\n",
"mnames = ['movie_id', 'title', 'genres']\n",
"movies = pd.read_table('datasets/movielens/movies.dat', sep='::',\n",
" header=None, names=mnames, engine='python')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>user_id</th>\n",
" <th>movie_id</th>\n",
" <th>rating</th>\n",
" <th>timestamp</th>\n",
" <th>gender</th>\n",
" <th>age</th>\n",
" <th>occupation</th>\n",
" <th>zip</th>\n",
" <th>title</th>\n",
" <th>genres</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>1193</td>\n",
" <td>5</td>\n",
" <td>978300760</td>\n",
" <td>F</td>\n",
" <td>1</td>\n",
" <td>10</td>\n",
" <td>48067</td>\n",
" <td>One Flew Over the Cuckoo's Nest (1975)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>1193</td>\n",
" <td>5</td>\n",
" <td>978298413</td>\n",
" <td>M</td>\n",
" <td>56</td>\n",
" <td>16</td>\n",
" <td>70072</td>\n",
" <td>One Flew Over the Cuckoo's Nest (1975)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>12</td>\n",
" <td>1193</td>\n",
" <td>4</td>\n",
" <td>978220179</td>\n",
" <td>M</td>\n",
" <td>25</td>\n",
" <td>12</td>\n",
" <td>32793</td>\n",
" <td>One Flew Over the Cuckoo's Nest (1975)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>15</td>\n",
" <td>1193</td>\n",
" <td>4</td>\n",
" <td>978199279</td>\n",
" <td>M</td>\n",
" <td>25</td>\n",
" <td>7</td>\n",
" <td>22903</td>\n",
" <td>One Flew Over the Cuckoo's Nest (1975)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>17</td>\n",
" <td>1193</td>\n",
" <td>5</td>\n",
" <td>978158471</td>\n",
" <td>M</td>\n",
" <td>50</td>\n",
" <td>1</td>\n",
" <td>95350</td>\n",
" <td>One Flew Over the Cuckoo's Nest (1975)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1000204</th>\n",
" <td>5949</td>\n",
" <td>2198</td>\n",
" <td>5</td>\n",
" <td>958846401</td>\n",
" <td>M</td>\n",
" <td>18</td>\n",
" <td>17</td>\n",
" <td>47901</td>\n",
" <td>Modulations (1998)</td>\n",
" <td>Documentary</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1000205</th>\n",
" <td>5675</td>\n",
" <td>2703</td>\n",
" <td>3</td>\n",
" <td>976029116</td>\n",
" <td>M</td>\n",
" <td>35</td>\n",
" <td>14</td>\n",
" <td>30030</td>\n",
" <td>Broken Vessels (1998)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1000206</th>\n",
" <td>5780</td>\n",
" <td>2845</td>\n",
" <td>1</td>\n",
" <td>958153068</td>\n",
" <td>M</td>\n",
" <td>18</td>\n",
" <td>17</td>\n",
" <td>92886</td>\n",
" <td>White Boys (1999)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1000207</th>\n",
" <td>5851</td>\n",
" <td>3607</td>\n",
" <td>5</td>\n",
" <td>957756608</td>\n",
" <td>F</td>\n",
" <td>18</td>\n",
" <td>20</td>\n",
" <td>55410</td>\n",
" <td>One Little Indian (1973)</td>\n",
" <td>Comedy|Drama|Western</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1000208</th>\n",
" <td>5938</td>\n",
" <td>2909</td>\n",
" <td>4</td>\n",
" <td>957273353</td>\n",
" <td>M</td>\n",
" <td>25</td>\n",
" <td>1</td>\n",
" <td>35401</td>\n",
" <td>Five Wives, Three Secretaries and Me (1998)</td>\n",
" <td>Documentary</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1000209 rows × 10 columns</p>\n",
"</div>"
],
"text/plain": [
" user_id movie_id rating timestamp gender age occupation zip \\\n",
"0 1 1193 5 978300760 F 1 10 48067 \n",
"1 2 1193 5 978298413 M 56 16 70072 \n",
"2 12 1193 4 978220179 M 25 12 32793 \n",
"3 15 1193 4 978199279 M 25 7 22903 \n",
"4 17 1193 5 978158471 M 50 1 95350 \n",
"... ... ... ... ... ... ... ... ... \n",
"1000204 5949 2198 5 958846401 M 18 17 47901 \n",
"1000205 5675 2703 3 976029116 M 35 14 30030 \n",
"1000206 5780 2845 1 958153068 M 18 17 92886 \n",
"1000207 5851 3607 5 957756608 F 18 20 55410 \n",
"1000208 5938 2909 4 957273353 M 25 1 35401 \n",
"\n",
" title genres \n",
"0 One Flew Over the Cuckoo's Nest (1975) Drama \n",
"1 One Flew Over the Cuckoo's Nest (1975) Drama \n",
"2 One Flew Over the Cuckoo's Nest (1975) Drama \n",
"3 One Flew Over the Cuckoo's Nest (1975) Drama \n",
"4 One Flew Over the Cuckoo's Nest (1975) Drama \n",
"... ... ... \n",
"1000204 Modulations (1998) Documentary \n",
"1000205 Broken Vessels (1998) Drama \n",
"1000206 White Boys (1999) Drama \n",
"1000207 One Little Indian (1973) Comedy|Drama|Western \n",
"1000208 Five Wives, Three Secretaries and Me (1998) Documentary \n",
"\n",
"[1000209 rows x 10 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.merge(pd.merge(ratings, users), movies)\n",
"data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Os 10 filmes melhor classificados (com mais de 300 avaliações)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>rating</th>\n",
" </tr>\n",
" <tr>\n",
" <th>title</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Seven Samurai (The Magnificent Seven) (Shichinin no samurai) (1954)</th>\n",
" <td>4.560510</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Shawshank Redemption, The (1994)</th>\n",
" <td>4.554558</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Godfather, The (1972)</th>\n",
" <td>4.524966</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Close Shave, A (1995)</th>\n",
" <td>4.520548</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Usual Suspects, The (1995)</th>\n",
" <td>4.517106</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Schindler's List (1993)</th>\n",
" <td>4.510417</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Wrong Trousers, The (1993)</th>\n",
" <td>4.507937</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Sunset Blvd. (a.k.a. Sunset Boulevard) (1950)</th>\n",
" <td>4.491489</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Raiders of the Lost Ark (1981)</th>\n",
" <td>4.477725</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Rear Window (1954)</th>\n",
" <td>4.476190</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" rating\n",
"title \n",
"Seven Samurai (The Magnificent Seven) (Shichini... 4.560510\n",
"Shawshank Redemption, The (1994) 4.554558\n",
"Godfather, The (1972) 4.524966\n",
"Close Shave, A (1995) 4.520548\n",
"Usual Suspects, The (1995) 4.517106\n",
"Schindler's List (1993) 4.510417\n",
"Wrong Trousers, The (1993) 4.507937\n",
"Sunset Blvd. (a.k.a. Sunset Boulevard) (1950) 4.491489\n",
"Raiders of the Lost Ark (1981) 4.477725\n",
"Rear Window (1954) 4.476190"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mean_ratings = data.pivot_table('rating', index='title', aggfunc='mean')\n",
"ratings_by_title = data.groupby('title').size()\n",
"active_titles = ratings_by_title.index[ratings_by_title >= 300]\n",
"top_rated_movies = mean_ratings.loc[active_titles].sort_values(by=\"rating\", ascending=False)[:10]\n",
"top_rated_movies"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Os 10 filmes pior classificados (com mais de 300 avaliações)"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>rating</th>\n",
" </tr>\n",
" <tr>\n",
" <th>title</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Battlefield Earth (2000)</th>\n",
" <td>1.611111</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Speed 2: Cruise Control (1997)</th>\n",
" <td>1.871935</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Super Mario Bros. (1993)</th>\n",
" <td>1.874286</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Superman IV: The Quest for Peace (1987)</th>\n",
" <td>1.888554</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Grease 2 (1982)</th>\n",
" <td>1.963696</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Barb Wire (1996)</th>\n",
" <td>2.030000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Howard the Duck (1986)</th>\n",
" <td>2.099078</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Wild Wild West (1999)</th>\n",
" <td>2.158537</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Inspector Gadget (1999)</th>\n",
" <td>2.166220</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Anaconda (1997)</th>\n",
" <td>2.200501</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" rating\n",
"title \n",
"Battlefield Earth (2000) 1.611111\n",
"Speed 2: Cruise Control (1997) 1.871935\n",
"Super Mario Bros. (1993) 1.874286\n",
"Superman IV: The Quest for Peace (1987) 1.888554\n",
"Grease 2 (1982) 1.963696\n",
"Barb Wire (1996) 2.030000\n",
"Howard the Duck (1986) 2.099078\n",
"Wild Wild West (1999) 2.158537\n",
"Inspector Gadget (1999) 2.166220\n",
"Anaconda (1997) 2.200501"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lowest_rated_movies = mean_ratings.loc[active_titles].sort_values(by=\"rating\")[:10]\n",
"lowest_rated_movies"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Quantidade de filmes por gênero (considerar apenas o primeiro gênero de cada filme)"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>title</th>\n",
" </tr>\n",
" <tr>\n",
" <th>first_genre</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Action</th>\n",
" <td>503</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Adventure</th>\n",
" <td>155</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Animation</th>\n",
" <td>90</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Children's</th>\n",
" <td>89</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Comedy</th>\n",
" <td>1024</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Crime</th>\n",
" <td>131</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Documentary</th>\n",
" <td>123</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Drama</th>\n",
" <td>1176</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Fantasy</th>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Film-Noir</th>\n",
" <td>25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Horror</th>\n",
" <td>262</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Musical</th>\n",
" <td>25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Mystery</th>\n",
" <td>36</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Romance</th>\n",
" <td>50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Sci-Fi</th>\n",
" <td>46</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Thriller</th>\n",
" <td>101</td>\n",
" </tr>\n",
" <tr>\n",
" <th>War</th>\n",
" <td>12</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Western</th>\n",
" <td>33</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" title\n",
"first_genre \n",
"Action 503\n",
"Adventure 155\n",
"Animation 90\n",
"Children's 89\n",
"Comedy 1024\n",
"Crime 131\n",
"Documentary 123\n",
"Drama 1176\n",
"Fantasy 2\n",
"Film-Noir 25\n",
"Horror 262\n",
"Musical 25\n",
"Mystery 36\n",
"Romance 50\n",
"Sci-Fi 46\n",
"Thriller 101\n",
"War 12\n",
"Western 33"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"movies[\"first_genre\"] = movies[\"genres\"].apply(lambda x: x.split(\"|\")[0])\n",
"movies.pivot_table('title', index='first_genre', aggfunc='count')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Os 10 filmes melhor classificados no gênero \"comedy\""
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>rating</th>\n",
" </tr>\n",
" <tr>\n",
" <th>title</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Follow the Bitch (1998)</th>\n",
" <td>5.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>One Little Indian (1973)</th>\n",
" <td>5.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Smashing Time (1967)</th>\n",
" <td>5.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Yojimbo (1961)</th>\n",
" <td>4.404651</td>\n",
" </tr>\n",
" <tr>\n",
" <th>City Lights (1931)</th>\n",
" <td>4.387454</td>\n",
" </tr>\n",
" <tr>\n",
" <th>General, The (1927)</th>\n",
" <td>4.368932</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Monty Python and the Holy Grail (1974)</th>\n",
" <td>4.335210</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Soft Fruit (1999)</th>\n",
" <td>4.333333</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Life Is Beautiful (La Vita � bella) (1997)</th>\n",
" <td>4.329861</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Sting, The (1973)</th>\n",
" <td>4.320305</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" rating\n",
"title \n",
"Follow the Bitch (1998) 5.000000\n",
"One Little Indian (1973) 5.000000\n",
"Smashing Time (1967) 5.000000\n",
"Yojimbo (1961) 4.404651\n",
"City Lights (1931) 4.387454\n",
"General, The (1927) 4.368932\n",
"Monty Python and the Holy Grail (1974) 4.335210\n",
"Soft Fruit (1999) 4.333333\n",
"Life Is Beautiful (La Vita � bella) (1997) 4.329861\n",
"Sting, The (1973) 4.320305"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data[\"first_genre\"] = data[\"genres\"].apply(lambda x: x.split(\"|\")[0])\n",
"comedy_movies = data[data.first_genre == \"Comedy\"]\n",
"mean_ratings = comedy_movies.pivot_table('rating', index='title', aggfunc='mean')\n",
"top_rated_comedies = mean_ratings.sort_values(by=\"rating\", ascending=False)[:10]\n",
"top_rated_comedies"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment