{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Exploratory Data Analysis","metadata":{"execution":{"iopub.status.busy":"2022-03-05T15:01:24.025492Z","iopub.execute_input":"2022-03-05T15:01:24.025788Z","iopub.status.idle":"2022-03-05T15:01:25.232117Z","shell.execute_reply.started":"2022-03-05T15:01:24.025758Z","shell.execute_reply":"2022-03-05T15:01:25.231343Z"}}},{"cell_type":"code","source":"import pandas as pd\nmovies = pd.read_csv(\"../input/the-movies-dataset/movies_metadata.csv\")\nmovies.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:11:58.846763Z","iopub.execute_input":"2022-03-06T01:11:58.847184Z","iopub.status.idle":"2022-03-06T01:12:00.103404Z","shell.execute_reply.started":"2022-03-06T01:11:58.847080Z","shell.execute_reply":"2022-03-06T01:12:00.102733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"movies.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:00.104880Z","iopub.execute_input":"2022-03-06T01:12:00.105288Z","iopub.status.idle":"2022-03-06T01:12:00.174801Z","shell.execute_reply.started":"2022-03-06T01:12:00.105243Z","shell.execute_reply":"2022-03-06T01:12:00.173890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"movies.columns.unique()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:00.176105Z","iopub.execute_input":"2022-03-06T01:12:00.176833Z","iopub.status.idle":"2022-03-06T01:12:00.185260Z","shell.execute_reply.started":"2022-03-06T01:12:00.176774Z","shell.execute_reply":"2022-03-06T01:12:00.184297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"movies.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:00.187519Z","iopub.execute_input":"2022-03-06T01:12:00.187860Z","iopub.status.idle":"2022-03-06T01:12:00.243782Z","shell.execute_reply.started":"2022-03-06T01:12:00.187821Z","shell.execute_reply":"2022-03-06T01:12:00.242815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keywords = pd.read_csv(\"../input/the-movies-dataset/credits.csv\")\nlinks = pd.read_csv(\"../input/the-movies-dataset/links.csv\")\nlinks_small = pd.read_csv(\"../input/the-movies-dataset/links_small.csv\")\nratings = pd.read_csv(\"../input/the-movies-dataset/ratings.csv\")\nratings_small = pd.read_csv(\"../input/the-movies-dataset/ratings_small.csv\") ","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:00.245254Z","iopub.execute_input":"2022-03-06T01:12:00.245505Z","iopub.status.idle":"2022-03-06T01:12:18.469206Z","shell.execute_reply.started":"2022-03-06T01:12:00.245475Z","shell.execute_reply":"2022-03-06T01:12:18.468206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keywords.tail(-10)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:18.470581Z","iopub.execute_input":"2022-03-06T01:12:18.470901Z","iopub.status.idle":"2022-03-06T01:12:18.486021Z","shell.execute_reply.started":"2022-03-06T01:12:18.470860Z","shell.execute_reply":"2022-03-06T01:12:18.485026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ratings.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:18.487553Z","iopub.execute_input":"2022-03-06T01:12:18.488525Z","iopub.status.idle":"2022-03-06T01:12:18.505434Z","shell.execute_reply.started":"2022-03-06T01:12:18.488485Z","shell.execute_reply":"2022-03-06T01:12:18.504494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ratings.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:18.506934Z","iopub.execute_input":"2022-03-06T01:12:18.507612Z","iopub.status.idle":"2022-03-06T01:12:18.519305Z","shell.execute_reply.started":"2022-03-06T01:12:18.507559Z","shell.execute_reply":"2022-03-06T01:12:18.518423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ratings_small.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:18.520617Z","iopub.execute_input":"2022-03-06T01:12:18.520882Z","iopub.status.idle":"2022-03-06T01:12:18.539829Z","shell.execute_reply.started":"2022-03-06T01:12:18.520849Z","shell.execute_reply":"2022-03-06T01:12:18.538719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ratings_by_movie = ratings_small[[\"movieId\",\"rating\"]]","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:18.542425Z","iopub.execute_input":"2022-03-06T01:12:18.542690Z","iopub.status.idle":"2022-03-06T01:12:18.551759Z","shell.execute_reply.started":"2022-03-06T01:12:18.542659Z","shell.execute_reply":"2022-03-06T01:12:18.551129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ratings_by_movie.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:18.552953Z","iopub.execute_input":"2022-03-06T01:12:18.553900Z","iopub.status.idle":"2022-03-06T01:12:18.569682Z","shell.execute_reply.started":"2022-03-06T01:12:18.553862Z","shell.execute_reply":"2022-03-06T01:12:18.568354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ratings_by_movie.sort_values(\"rating\", ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:18.571191Z","iopub.execute_input":"2022-03-06T01:12:18.571482Z","iopub.status.idle":"2022-03-06T01:12:18.598507Z","shell.execute_reply.started":"2022-03-06T01:12:18.571454Z","shell.execute_reply":"2022-03-06T01:12:18.597818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"revenue = movies[[\"genres\",\"revenue\"]]\nrevenue.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:18.599843Z","iopub.execute_input":"2022-03-06T01:12:18.600529Z","iopub.status.idle":"2022-03-06T01:12:18.612106Z","shell.execute_reply.started":"2022-03-06T01:12:18.600494Z","shell.execute_reply":"2022-03-06T01:12:18.611089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ratings_by_movie.hist()","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:18.613624Z","iopub.execute_input":"2022-03-06T01:12:18.614189Z","iopub.status.idle":"2022-03-06T01:12:19.169952Z","shell.execute_reply.started":"2022-03-06T01:12:18.614132Z","shell.execute_reply":"2022-03-06T01:12:19.168900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Simple Recommender","metadata":{}},{"cell_type":"markdown","source":"**Now we working on defining a new metric rather than rating as it is often misleading. When we are calculating rating, we dont consider the popularity of a movie. It can happen that we are consider a movie rating of 9 from only 10 voters as 'better' than a movie with 4.7+ rating with 10,000 voters. For this reason, we are caclulating a metric called 'weighted rating' which considers the factor mentioned above.**","metadata":{}},{"cell_type":"code","source":"#We are calculating the mean of vote average column\nC = movies[\"vote_average\"].mean()\nprint(C)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:19.171213Z","iopub.execute_input":"2022-03-06T01:12:19.171454Z","iopub.status.idle":"2022-03-06T01:12:19.177322Z","shell.execute_reply.started":"2022-03-06T01:12:19.171426Z","shell.execute_reply":"2022-03-06T01:12:19.176720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#calculating the minimum number of votes required to be in the chart, m\nm  = movies['vote_count'].quantile(0.75)\nprint(m)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:19.178500Z","iopub.execute_input":"2022-03-06T01:12:19.179235Z","iopub.status.idle":"2022-03-06T01:12:19.194248Z","shell.execute_reply.started":"2022-03-06T01:12:19.179201Z","shell.execute_reply":"2022-03-06T01:12:19.193489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Filtering out all qualified movies into a new DataFrame\nq_movies = movies.copy().loc[movies['vote_count'] >= m]\nq_movies.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:19.195345Z","iopub.execute_input":"2022-03-06T01:12:19.195969Z","iopub.status.idle":"2022-03-06T01:12:19.230862Z","shell.execute_reply.started":"2022-03-06T01:12:19.195929Z","shell.execute_reply":"2022-03-06T01:12:19.230184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"movies.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:19.231881Z","iopub.execute_input":"2022-03-06T01:12:19.232497Z","iopub.status.idle":"2022-03-06T01:12:19.238086Z","shell.execute_reply.started":"2022-03-06T01:12:19.232464Z","shell.execute_reply":"2022-03-06T01:12:19.237232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#function to calculate weighted rating of each movie\ndef weighted_rating(x, m=m, C=C):\n    v = x['vote_count']\n    R = x['vote_average']\n    # Calculation based on the IMDB formula\n    return (v/(v+m) * R) + (m/(m+v) * C)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:19.239129Z","iopub.execute_input":"2022-03-06T01:12:19.239718Z","iopub.status.idle":"2022-03-06T01:12:19.249305Z","shell.execute_reply.started":"2022-03-06T01:12:19.239687Z","shell.execute_reply":"2022-03-06T01:12:19.248492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Defining a new feature 'score' and calculate its value with `weighted_rating()`\nq_movies['score'] = q_movies.apply(weighted_rating, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:19.250317Z","iopub.execute_input":"2022-03-06T01:12:19.250946Z","iopub.status.idle":"2022-03-06T01:12:19.444760Z","shell.execute_reply.started":"2022-03-06T01:12:19.250912Z","shell.execute_reply":"2022-03-06T01:12:19.443648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Sort movies based on score calculated above\nq_movies = q_movies.sort_values('score', ascending=False)\n\n#Print the top 15 movies\nq_movies[['title', 'vote_count', 'vote_average', 'score']].head(20)","metadata":{"execution":{"iopub.status.busy":"2022-03-06T01:12:19.446075Z","iopub.execute_input":"2022-03-06T01:12:19.446713Z","iopub.status.idle":"2022-03-06T01:12:19.476887Z","shell.execute_reply.started":"2022-03-06T01:12:19.446678Z","shell.execute_reply":"2022-03-06T01:12:19.475704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Using content,Genres & Keywords can also be attempted and might produce better recommenders. In this case, we can use similarity functions, such as - cosine similarity**","metadata":{}}]}