diff --git "a/Week16_\353\263\265\354\212\265\352\263\274\354\240\234_\352\266\214\355\230\234\354\210\230.ipynb" "b/Week16_\353\263\265\354\212\265\352\263\274\354\240\234_\352\266\214\355\230\234\354\210\230.ipynb"
new file mode 100644
index 0000000..47cf87c
--- /dev/null
+++ "b/Week16_\353\263\265\354\212\265\352\263\274\354\240\234_\352\266\214\355\230\234\354\210\230.ipynb"
@@ -0,0 +1,5677 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": []
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "id": "2-pQmWC9d4xO",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 188
+ },
+ "outputId": "84df0074-1b0e-4d86-c641-25faff36fc6b"
+ },
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ " budget genres \\\n",
+ "0 237000000 [{\"id\": 28, \"name\": \"Action\"}, {\"id\": 12, \"nam... \n",
+ "\n",
+ " homepage id \\\n",
+ "0 http://www.avatarmovie.com/ 19995 \n",
+ "\n",
+ " keywords original_language \\\n",
+ "0 [{\"id\": 1463, \"name\": \"culture clash\"}, {\"id\":... en \n",
+ "\n",
+ " original_title overview \\\n",
+ "0 Avatar In the 22nd century, a paraplegic Marine is di... \n",
+ "\n",
+ " popularity production_companies \\\n",
+ "0 150.437577 [{\"name\": \"Ingenious Film Partners\", \"id\": 289... \n",
+ "\n",
+ " production_countries release_date revenue \\\n",
+ "0 [{\"iso_3166_1\": \"US\", \"name\": \"United States o... 2009-12-10 2787965087 \n",
+ "\n",
+ " runtime spoken_languages status \\\n",
+ "0 162.0 [{\"iso_639_1\": \"en\", \"name\": \"English\"}, {\"iso... Released \n",
+ "\n",
+ " tagline title vote_average vote_count \n",
+ "0 Enter the World of Pandora. Avatar 7.2 11800 "
+ ],
+ "text/html": [
+ "\n",
+ "
\n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " budget | \n",
+ " genres | \n",
+ " homepage | \n",
+ " id | \n",
+ " keywords | \n",
+ " original_language | \n",
+ " original_title | \n",
+ " overview | \n",
+ " popularity | \n",
+ " production_companies | \n",
+ " production_countries | \n",
+ " release_date | \n",
+ " revenue | \n",
+ " runtime | \n",
+ " spoken_languages | \n",
+ " status | \n",
+ " tagline | \n",
+ " title | \n",
+ " vote_average | \n",
+ " vote_count | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " 237000000 | \n",
+ " [{\"id\": 28, \"name\": \"Action\"}, {\"id\": 12, \"nam... | \n",
+ " http://www.avatarmovie.com/ | \n",
+ " 19995 | \n",
+ " [{\"id\": 1463, \"name\": \"culture clash\"}, {\"id\":... | \n",
+ " en | \n",
+ " Avatar | \n",
+ " In the 22nd century, a paraplegic Marine is di... | \n",
+ " 150.437577 | \n",
+ " [{\"name\": \"Ingenious Film Partners\", \"id\": 289... | \n",
+ " [{\"iso_3166_1\": \"US\", \"name\": \"United States o... | \n",
+ " 2009-12-10 | \n",
+ " 2787965087 | \n",
+ " 162.0 | \n",
+ " [{\"iso_639_1\": \"en\", \"name\": \"English\"}, {\"iso... | \n",
+ " Released | \n",
+ " Enter the World of Pandora. | \n",
+ " Avatar | \n",
+ " 7.2 | \n",
+ " 11800 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ "
\n",
+ "
\n"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "dataframe",
+ "variable_name": "movies",
+ "summary": "{\n \"name\": \"movies\",\n \"rows\": 4803,\n \"fields\": [\n {\n \"column\": \"budget\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 40722391,\n \"min\": 0,\n \"max\": 380000000,\n \"num_unique_values\": 436,\n \"samples\": [\n 439000,\n 68000000,\n 700000\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"genres\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 1175,\n \"samples\": [\n \"[{\\\"id\\\": 14, \\\"name\\\": \\\"Fantasy\\\"}, {\\\"id\\\": 12, \\\"name\\\": \\\"Adventure\\\"}, {\\\"id\\\": 16, \\\"name\\\": \\\"Animation\\\"}]\",\n \"[{\\\"id\\\": 28, \\\"name\\\": \\\"Action\\\"}, {\\\"id\\\": 35, \\\"name\\\": \\\"Comedy\\\"}, {\\\"id\\\": 80, \\\"name\\\": \\\"Crime\\\"}, {\\\"id\\\": 18, \\\"name\\\": \\\"Drama\\\"}]\",\n \"[{\\\"id\\\": 12, \\\"name\\\": \\\"Adventure\\\"}, {\\\"id\\\": 16, \\\"name\\\": \\\"Animation\\\"}, {\\\"id\\\": 10751, \\\"name\\\": \\\"Family\\\"}, {\\\"id\\\": 14, \\\"name\\\": \\\"Fantasy\\\"}, {\\\"id\\\": 878, \\\"name\\\": \\\"Science Fiction\\\"}]\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"homepage\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 1691,\n \"samples\": [\n \"https://www.warnerbros.com/running-scared\",\n \"http://www.51birchstreet.com/index.php\",\n \"http://movies2.foxjapan.com/glee/\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 88694,\n \"min\": 5,\n \"max\": 459488,\n \"num_unique_values\": 4803,\n \"samples\": [\n 8427,\n 13006,\n 18041\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"keywords\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4222,\n \"samples\": [\n \"[{\\\"id\\\": 782, \\\"name\\\": \\\"assassin\\\"}, {\\\"id\\\": 1872, \\\"name\\\": \\\"loss of father\\\"}, {\\\"id\\\": 2908, \\\"name\\\": \\\"secret society\\\"}, {\\\"id\\\": 3045, \\\"name\\\": \\\"mission of murder\\\"}, {\\\"id\\\": 9748, \\\"name\\\": \\\"revenge\\\"}]\",\n \"[{\\\"id\\\": 2987, \\\"name\\\": \\\"gang war\\\"}, {\\\"id\\\": 4942, \\\"name\\\": \\\"victim of murder\\\"}, {\\\"id\\\": 5332, \\\"name\\\": \\\"greed\\\"}, {\\\"id\\\": 6062, \\\"name\\\": \\\"hostility\\\"}, {\\\"id\\\": 156212, \\\"name\\\": \\\"spaghetti western\\\"}]\",\n \"[{\\\"id\\\": 703, \\\"name\\\": \\\"detective\\\"}, {\\\"id\\\": 1299, \\\"name\\\": \\\"monster\\\"}, {\\\"id\\\": 6101, \\\"name\\\": \\\"engine\\\"}, {\\\"id\\\": 10988, \\\"name\\\": \\\"based on tv series\\\"}, {\\\"id\\\": 15162, \\\"name\\\": \\\"dog\\\"}]\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"original_language\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 37,\n \"samples\": [\n \"xx\",\n \"ta\",\n \"es\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"original_title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4801,\n \"samples\": [\n \"I Spy\",\n \"Love Letters\",\n \"Sleepover\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"overview\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4800,\n \"samples\": [\n \"When the Switchblade, the most sophisticated prototype stealth fighter created yet, is stolen from the U.S. government, one of the United States' top spies, Alex Scott, is called to action. What he doesn't expect is to get teamed up with a cocky civilian, World Class Boxing Champion Kelly Robinson, on a dangerous top secret espionage mission. Their assignment: using equal parts skill and humor, catch Arnold Gundars, one of the world's most successful arms dealers.\",\n \"When \\\"street smart\\\" rapper Christopher \\\"C-Note\\\" Hawkins (Big Boi) applies for a membership to all-white Carolina Pines Country Club, the establishment's proprietors are hardly ready to oblige him.\",\n \"As their first year of high school looms ahead, best friends Julie, Hannah, Yancy and Farrah have one last summer sleepover. Little do they know they're about to embark on the adventure of a lifetime. Desperate to shed their nerdy status, they take part in a night-long scavenger hunt that pits them against their popular archrivals. Everything under the sun goes on -- from taking Yancy's father's car to sneaking into nightclubs!\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"popularity\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 31.816649749537806,\n \"min\": 0.0,\n \"max\": 875.581305,\n \"num_unique_values\": 4802,\n \"samples\": [\n 13.267631,\n 0.010909,\n 5.842299\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"production_companies\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3697,\n \"samples\": [\n \"[{\\\"name\\\": \\\"Paramount Pictures\\\", \\\"id\\\": 4}, {\\\"name\\\": \\\"Cherry Alley Productions\\\", \\\"id\\\": 2232}]\",\n \"[{\\\"name\\\": \\\"Twentieth Century Fox Film Corporation\\\", \\\"id\\\": 306}, {\\\"name\\\": \\\"Dune Entertainment\\\", \\\"id\\\": 444}, {\\\"name\\\": \\\"Regency Enterprises\\\", \\\"id\\\": 508}, {\\\"name\\\": \\\"Guy Walks into a Bar Productions\\\", \\\"id\\\": 2645}, {\\\"name\\\": \\\"Deep River Productions\\\", \\\"id\\\": 2646}, {\\\"name\\\": \\\"Friendly Films (II)\\\", \\\"id\\\": 81136}]\",\n \"[{\\\"name\\\": \\\"Twentieth Century Fox Film Corporation\\\", \\\"id\\\": 306}]\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"production_countries\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 469,\n \"samples\": [\n \"[{\\\"iso_3166_1\\\": \\\"ES\\\", \\\"name\\\": \\\"Spain\\\"}, {\\\"iso_3166_1\\\": \\\"GB\\\", \\\"name\\\": \\\"United Kingdom\\\"}, {\\\"iso_3166_1\\\": \\\"US\\\", \\\"name\\\": \\\"United States of America\\\"}, {\\\"iso_3166_1\\\": \\\"FR\\\", \\\"name\\\": \\\"France\\\"}]\",\n \"[{\\\"iso_3166_1\\\": \\\"US\\\", \\\"name\\\": \\\"United States of America\\\"}, {\\\"iso_3166_1\\\": \\\"CA\\\", \\\"name\\\": \\\"Canada\\\"}, {\\\"iso_3166_1\\\": \\\"DE\\\", \\\"name\\\": \\\"Germany\\\"}]\",\n \"[{\\\"iso_3166_1\\\": \\\"DE\\\", \\\"name\\\": \\\"Germany\\\"}, {\\\"iso_3166_1\\\": \\\"ES\\\", \\\"name\\\": \\\"Spain\\\"}, {\\\"iso_3166_1\\\": \\\"GB\\\", \\\"name\\\": \\\"United Kingdom\\\"}, {\\\"iso_3166_1\\\": \\\"US\\\", \\\"name\\\": \\\"United States of America\\\"}]\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"release_date\",\n \"properties\": {\n \"dtype\": \"object\",\n \"num_unique_values\": 3280,\n \"samples\": [\n \"1966-10-16\",\n \"1987-07-31\",\n \"1993-09-23\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"revenue\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 162857100,\n \"min\": 0,\n \"max\": 2787965087,\n \"num_unique_values\": 3297,\n \"samples\": [\n 11833696,\n 10462500,\n 17807569\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"runtime\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 22.611934588844207,\n \"min\": 0.0,\n \"max\": 338.0,\n \"num_unique_values\": 156,\n \"samples\": [\n 74.0,\n 85.0,\n 170.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"spoken_languages\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 544,\n \"samples\": [\n \"[{\\\"iso_639_1\\\": \\\"es\\\", \\\"name\\\": \\\"Espa\\\\u00f1ol\\\"}, {\\\"iso_639_1\\\": \\\"en\\\", \\\"name\\\": \\\"English\\\"}, {\\\"iso_639_1\\\": \\\"fr\\\", \\\"name\\\": \\\"Fran\\\\u00e7ais\\\"}, {\\\"iso_639_1\\\": \\\"hu\\\", \\\"name\\\": \\\"Magyar\\\"}]\",\n \"[{\\\"iso_639_1\\\": \\\"en\\\", \\\"name\\\": \\\"English\\\"}, {\\\"iso_639_1\\\": \\\"it\\\", \\\"name\\\": \\\"Italiano\\\"}, {\\\"iso_639_1\\\": \\\"pt\\\", \\\"name\\\": \\\"Portugu\\\\u00eas\\\"}]\",\n \"[{\\\"iso_639_1\\\": \\\"de\\\", \\\"name\\\": \\\"Deutsch\\\"}, {\\\"iso_639_1\\\": \\\"it\\\", \\\"name\\\": \\\"Italiano\\\"}, {\\\"iso_639_1\\\": \\\"la\\\", \\\"name\\\": \\\"Latin\\\"}, {\\\"iso_639_1\\\": \\\"pl\\\", \\\"name\\\": \\\"Polski\\\"}]\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"status\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"Released\",\n \"Post Production\",\n \"Rumored\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"tagline\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3944,\n \"samples\": [\n \"When you're 17, every day is war.\",\n \"An Unspeakable Horror. A Creative Genius. Captured For Eternity.\",\n \"May the schwartz be with you\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4800,\n \"samples\": [\n \"I Spy\",\n \"Who's Your Caddy?\",\n \"Sleepover\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_average\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1.1946121628478925,\n \"min\": 0.0,\n \"max\": 10.0,\n \"num_unique_values\": 71,\n \"samples\": [\n 5.1,\n 7.2,\n 4.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_count\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1234,\n \"min\": 0,\n \"max\": 13752,\n \"num_unique_values\": 1609,\n \"samples\": [\n 7604,\n 3428,\n 225\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
+ }
+ },
+ "metadata": {},
+ "execution_count": 1
+ }
+ ],
+ "source": [
+ "import pandas as pd\n",
+ "import numpy as np\n",
+ "import warnings\n",
+ "warnings.filterwarnings('ignore')\n",
+ "movies = pd.read_csv('/content/drive/MyDrive/Euron/tmdb_5000_movies.csv')\n",
+ "movies.head(1)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "movies_df = movies[['id','title','overview','genres', 'vote_average', 'vote_count', 'popularity', 'keywords', 'overview']]\n",
+ "pd.set_option('max_colwidth', 100)\n",
+ "movies_df[['genres', 'keywords']][:1]"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 98
+ },
+ "id": "qj-XLYJvgPQM",
+ "outputId": "e5a43828-3de2-464c-c0b3-973a4dfaddb4"
+ },
+ "execution_count": 4,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ " genres \\\n",
+ "0 [{\"id\": 28, \"name\": \"Action\"}, {\"id\": 12, \"name\": \"Adventure\"}, {\"id\": 14, \"name\": \"Fantasy\"}, {... \n",
+ "\n",
+ " keywords \n",
+ "0 [{\"id\": 1463, \"name\": \"culture clash\"}, {\"id\": 2964, \"name\": \"future\"}, {\"id\": 3386, \"name\": \"sp... "
+ ],
+ "text/html": [
+ "\n",
+ " \n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " genres | \n",
+ " keywords | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " [{\"id\": 28, \"name\": \"Action\"}, {\"id\": 12, \"name\": \"Adventure\"}, {\"id\": 14, \"name\": \"Fantasy\"}, {... | \n",
+ " [{\"id\": 1463, \"name\": \"culture clash\"}, {\"id\": 2964, \"name\": \"future\"}, {\"id\": 3386, \"name\": \"sp... | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ "
\n",
+ "
\n"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "dataframe",
+ "summary": "{\n \"name\": \"movies_df[['genres', 'keywords']][:1]\",\n \"rows\": 1,\n \"fields\": [\n {\n \"column\": \"genres\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"[{\\\"id\\\": 28, \\\"name\\\": \\\"Action\\\"}, {\\\"id\\\": 12, \\\"name\\\": \\\"Adventure\\\"}, {\\\"id\\\": 14, \\\"name\\\": \\\"Fantasy\\\"}, {\\\"id\\\": 878, \\\"name\\\": \\\"Science Fiction\\\"}]\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"keywords\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"[{\\\"id\\\": 1463, \\\"name\\\": \\\"culture clash\\\"}, {\\\"id\\\": 2964, \\\"name\\\": \\\"future\\\"}, {\\\"id\\\": 3386, \\\"name\\\": \\\"space war\\\"}, {\\\"id\\\": 3388, \\\"name\\\": \\\"space colony\\\"}, {\\\"id\\\": 3679, \\\"name\\\": \\\"society\\\"}, {\\\"id\\\": 3801, \\\"name\\\": \\\"space travel\\\"}, {\\\"id\\\": 9685, \\\"name\\\": \\\"futuristic\\\"}, {\\\"id\\\": 9840, \\\"name\\\": \\\"romance\\\"}, {\\\"id\\\": 9882, \\\"name\\\": \\\"space\\\"}, {\\\"id\\\": 9951, \\\"name\\\": \\\"alien\\\"}, {\\\"id\\\": 10148, \\\"name\\\": \\\"tribe\\\"}, {\\\"id\\\": 10158, \\\"name\\\": \\\"alien planet\\\"}, {\\\"id\\\": 10987, \\\"name\\\": \\\"cgi\\\"}, {\\\"id\\\": 11399, \\\"name\\\": \\\"marine\\\"}, {\\\"id\\\": 13065, \\\"name\\\": \\\"soldier\\\"}, {\\\"id\\\": 14643, \\\"name\\\": \\\"battle\\\"}, {\\\"id\\\": 14720, \\\"name\\\": \\\"love affair\\\"}, {\\\"id\\\": 165431, \\\"name\\\": \\\"anti war\\\"}, {\\\"id\\\": 193554, \\\"name\\\": \\\"power relations\\\"}, {\\\"id\\\": 206690, \\\"name\\\": \\\"mind and soul\\\"}, {\\\"id\\\": 209714, \\\"name\\\": \\\"3d\\\"}]\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
+ }
+ },
+ "metadata": {},
+ "execution_count": 4
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from ast import literal_eval\n",
+ "\n",
+ "movies_df['genres'] = movies_df['genres'].apply(literal_eval)\n",
+ "movies_df['keywords'] = movies_df['keywords'].apply(literal_eval)\n"
+ ],
+ "metadata": {
+ "id": "SvGrOG2xihxQ"
+ },
+ "execution_count": 5,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "movies_df['genres'] = movies_df['genres'].apply(lambda x : [ y['name'] for y in x])\n",
+ "movies_df['keywords'] = movies_df['keywords'].apply(lambda x : [ y['name'] for y in x])\n",
+ "\n",
+ "movies_df[['genres', 'keywords']][:1]\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 98
+ },
+ "id": "yAHsImzsihut",
+ "outputId": "9e09c175-78bd-4e16-9392-f0dc94de0411"
+ },
+ "execution_count": 6,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ " genres \\\n",
+ "0 [Action, Adventure, Fantasy, Science Fiction] \n",
+ "\n",
+ " keywords \n",
+ "0 [culture clash, future, space war, space colony, society, space travel, futuristic, romance, spa... "
+ ],
+ "text/html": [
+ "\n",
+ " \n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " genres | \n",
+ " keywords | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " [Action, Adventure, Fantasy, Science Fiction] | \n",
+ " [culture clash, future, space war, space colony, society, space travel, futuristic, romance, spa... | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ "
\n",
+ "
\n"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "dataframe",
+ "summary": "{\n \"name\": \"movies_df[['genres', 'keywords']][:1]\",\n \"rows\": 1,\n \"fields\": [\n {\n \"column\": \"genres\",\n \"properties\": {\n \"dtype\": \"object\",\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"keywords\",\n \"properties\": {\n \"dtype\": \"object\",\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
+ }
+ },
+ "metadata": {},
+ "execution_count": 6
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from sklearn.feature_extraction.text import CountVectorizer\n",
+ "\n",
+ "# CountVectorizer를 적용하기 위해 공백문자로 word 단위가 구분되는 문자열로 변환.\n",
+ "movies_df['genres_literal'] = movies_df['genres'].apply(lambda x : (' ').join(x))\n",
+ "\n",
+ "count_vect = CountVectorizer(min_df=1, ngram_range=(1, 2))\n",
+ "genre_mat = count_vect.fit_transform(movies_df['genres_literal'])\n",
+ "print(genre_mat.shape)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "T_rCUCitihr4",
+ "outputId": "f4892201-2be6-4876-cecd-8cf74e2f163e"
+ },
+ "execution_count": 9,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "(4803, 276)\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from sklearn.metrics.pairwise import cosine_similarity\n",
+ "\n",
+ "genre_sim = cosine_similarity(genre_mat, genre_mat)\n",
+ "print(genre_sim.shape)\n",
+ "print(genre_sim[2])\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "rNiPVxfTiho_",
+ "outputId": "5c3c59b7-828f-402d-836d-8245225b2772"
+ },
+ "execution_count": 10,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "(4803, 4803)\n",
+ "[0.4472136 0.4 1. ... 0. 0. 0. ]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "genre_sim_sorted_ind = genre_sim.argsort()[:, ::-1]\n",
+ "print(genre_sim_sorted_ind[:1])\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "1DVwtM2Wihml",
+ "outputId": "0755576f-4ab2-47c9-aca2-6e3c41f35511"
+ },
+ "execution_count": 11,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[[ 0 46 3494 ... 3331 3333 2031]]\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def find_sim_movie(df, sorted_ind, title_name, top_n=10):\n",
+ "\n",
+ " title_movie = df[df['title'] == title_name]\n",
+ "\n",
+ " title_index = title_movie.index.values\n",
+ " similar_indexes = sorted_ind[title_index, :(top_n)]\n",
+ "\n",
+ " # dataframe에서 index 사용하기 위해서 1차원 ndarray로 변경\n",
+ " print(similar_indexes)\n",
+ " similar_indexes = similar_indexes.reshape(-1)\n",
+ "\n",
+ " return df.iloc[similar_indexes]\n"
+ ],
+ "metadata": {
+ "id": "TsmIFjdWihkW"
+ },
+ "execution_count": 12,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "similar_movies = find_sim_movie(movies_df, genre_sim_sorted_ind, 'The Godfather', 10)\n",
+ "similar_movies[['title', 'vote_average']]\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 381
+ },
+ "id": "T03rtKVJihiE",
+ "outputId": "7634c200-48c5-4070-de0e-33f10a5e3ced"
+ },
+ "execution_count": 13,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "[[1881 3378 3866 1370 1464 588 3887 3594 2839 892]]\n"
+ ]
+ },
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ " title vote_average\n",
+ "1881 The Shawshank Redemption 8.5\n",
+ "3378 Auto Focus 6.1\n",
+ "3866 City of God 8.1\n",
+ "1370 21 6.5\n",
+ "1464 Black Water Transit 0.0\n",
+ "588 Wall Street: Money Never Sleeps 5.8\n",
+ "3887 Trainspotting 7.8\n",
+ "3594 Spring Breakers 5.0\n",
+ "2839 Rounders 6.9\n",
+ "892 Casino 7.8"
+ ],
+ "text/html": [
+ "\n",
+ " \n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " title | \n",
+ " vote_average | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 1881 | \n",
+ " The Shawshank Redemption | \n",
+ " 8.5 | \n",
+ "
\n",
+ " \n",
+ " | 3378 | \n",
+ " Auto Focus | \n",
+ " 6.1 | \n",
+ "
\n",
+ " \n",
+ " | 3866 | \n",
+ " City of God | \n",
+ " 8.1 | \n",
+ "
\n",
+ " \n",
+ " | 1370 | \n",
+ " 21 | \n",
+ " 6.5 | \n",
+ "
\n",
+ " \n",
+ " | 1464 | \n",
+ " Black Water Transit | \n",
+ " 0.0 | \n",
+ "
\n",
+ " \n",
+ " | 588 | \n",
+ " Wall Street: Money Never Sleeps | \n",
+ " 5.8 | \n",
+ "
\n",
+ " \n",
+ " | 3887 | \n",
+ " Trainspotting | \n",
+ " 7.8 | \n",
+ "
\n",
+ " \n",
+ " | 3594 | \n",
+ " Spring Breakers | \n",
+ " 5.0 | \n",
+ "
\n",
+ " \n",
+ " | 2839 | \n",
+ " Rounders | \n",
+ " 6.9 | \n",
+ "
\n",
+ " \n",
+ " | 892 | \n",
+ " Casino | \n",
+ " 7.8 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ "
\n",
+ "
\n"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "dataframe",
+ "summary": "{\n \"name\": \"similar_movies[['title', 'vote_average']]\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"Rounders\",\n \"Auto Focus\",\n \"Wall Street: Money Never Sleeps\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_average\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2.4636242498490803,\n \"min\": 0.0,\n \"max\": 8.5,\n \"num_unique_values\": 9,\n \"samples\": [\n 5.0,\n 6.1,\n 5.8\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
+ }
+ },
+ "metadata": {},
+ "execution_count": 13
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "movies_df[['title', 'vote_average', 'vote_count']].sort_values('vote_average',\n",
+ " ascending=False)[:10]\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 363
+ },
+ "id": "wNzr7OY4iq48",
+ "outputId": "471cd742-2b3c-4597-bab6-02d121f7855d"
+ },
+ "execution_count": 14,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ " title vote_average vote_count\n",
+ "4662 Little Big Top 10.0 1\n",
+ "3519 Stiff Upper Lips 10.0 1\n",
+ "4045 Dancer, Texas Pop. 81 10.0 1\n",
+ "4247 Me You and Five Bucks 10.0 2\n",
+ "3992 Sardaarji 9.5 2\n",
+ "2386 One Man's Hero 9.3 2\n",
+ "1881 The Shawshank Redemption 8.5 8205\n",
+ "2970 There Goes My Baby 8.5 2\n",
+ "3337 The Godfather 8.4 5893\n",
+ "2796 The Prisoner of Zenda 8.4 11"
+ ],
+ "text/html": [
+ "\n",
+ " \n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " title | \n",
+ " vote_average | \n",
+ " vote_count | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 4662 | \n",
+ " Little Big Top | \n",
+ " 10.0 | \n",
+ " 1 | \n",
+ "
\n",
+ " \n",
+ " | 3519 | \n",
+ " Stiff Upper Lips | \n",
+ " 10.0 | \n",
+ " 1 | \n",
+ "
\n",
+ " \n",
+ " | 4045 | \n",
+ " Dancer, Texas Pop. 81 | \n",
+ " 10.0 | \n",
+ " 1 | \n",
+ "
\n",
+ " \n",
+ " | 4247 | \n",
+ " Me You and Five Bucks | \n",
+ " 10.0 | \n",
+ " 2 | \n",
+ "
\n",
+ " \n",
+ " | 3992 | \n",
+ " Sardaarji | \n",
+ " 9.5 | \n",
+ " 2 | \n",
+ "
\n",
+ " \n",
+ " | 2386 | \n",
+ " One Man's Hero | \n",
+ " 9.3 | \n",
+ " 2 | \n",
+ "
\n",
+ " \n",
+ " | 1881 | \n",
+ " The Shawshank Redemption | \n",
+ " 8.5 | \n",
+ " 8205 | \n",
+ "
\n",
+ " \n",
+ " | 2970 | \n",
+ " There Goes My Baby | \n",
+ " 8.5 | \n",
+ " 2 | \n",
+ "
\n",
+ " \n",
+ " | 3337 | \n",
+ " The Godfather | \n",
+ " 8.4 | \n",
+ " 5893 | \n",
+ "
\n",
+ " \n",
+ " | 2796 | \n",
+ " The Prisoner of Zenda | \n",
+ " 8.4 | \n",
+ " 11 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ "
\n",
+ "
\n"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "dataframe",
+ "summary": "{\n \"name\": \" ascending=False)[:10]\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"The Godfather\",\n \"Stiff Upper Lips\",\n \"One Man's Hero\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_average\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.7366591251499343,\n \"min\": 8.4,\n \"max\": 10.0,\n \"num_unique_values\": 5,\n \"samples\": [\n 9.5,\n 8.4,\n 9.3\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_count\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 3020,\n \"min\": 1,\n \"max\": 8205,\n \"num_unique_values\": 5,\n \"samples\": [\n 2,\n 11,\n 8205\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
+ }
+ },
+ "metadata": {},
+ "execution_count": 14
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "C = movies_df['vote_average'].mean()\n",
+ "m = movies_df['vote_count'].quantile(0.6)\n",
+ "\n",
+ "print('C:', round(C, 3), 'm:', round(m, 3))\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "PG8vL2Luiq2z",
+ "outputId": "7b983b5e-53c4-4517-b54a-77a0e105a3ba"
+ },
+ "execution_count": 15,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "C: 6.092 m: 370.2\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "percentile = 0.6\n",
+ "m = movies_df['vote_count'].quantile(percentile)\n",
+ "C = movies_df['vote_average'].mean()\n",
+ "\n",
+ "def weighted_vote_average(record):\n",
+ " v = record['vote_count']\n",
+ " R = record['vote_average']\n",
+ "\n",
+ " return ( (v/(v+m)) * R ) + ( (m/(m+v)) * C )\n",
+ "\n",
+ "movies_df['weighted_vote'] = movies_df.apply(weighted_vote_average, axis=1)\n",
+ "\n",
+ "movies_df[['title', 'vote_average', 'weighted_vote', 'vote_count']].sort_values(\n",
+ " 'weighted_vote', ascending=False)[:10]\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 363
+ },
+ "id": "J0ZwH_q5iq0W",
+ "outputId": "2511038a-3811-4259-e24f-8a7d4986abc6"
+ },
+ "execution_count": 16,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ " title vote_average weighted_vote vote_count\n",
+ "1881 The Shawshank Redemption 8.5 8.396052 8205\n",
+ "3337 The Godfather 8.4 8.263591 5893\n",
+ "662 Fight Club 8.3 8.216455 9413\n",
+ "3232 Pulp Fiction 8.3 8.207102 8428\n",
+ "65 The Dark Knight 8.2 8.136930 12002\n",
+ "1818 Schindler's List 8.3 8.126069 4329\n",
+ "3865 Whiplash 8.3 8.123248 4254\n",
+ "809 Forrest Gump 8.2 8.105954 7927\n",
+ "2294 Spirited Away 8.3 8.105867 3840\n",
+ "2731 The Godfather: Part II 8.3 8.079586 3338"
+ ],
+ "text/html": [
+ "\n",
+ " \n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " title | \n",
+ " vote_average | \n",
+ " weighted_vote | \n",
+ " vote_count | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 1881 | \n",
+ " The Shawshank Redemption | \n",
+ " 8.5 | \n",
+ " 8.396052 | \n",
+ " 8205 | \n",
+ "
\n",
+ " \n",
+ " | 3337 | \n",
+ " The Godfather | \n",
+ " 8.4 | \n",
+ " 8.263591 | \n",
+ " 5893 | \n",
+ "
\n",
+ " \n",
+ " | 662 | \n",
+ " Fight Club | \n",
+ " 8.3 | \n",
+ " 8.216455 | \n",
+ " 9413 | \n",
+ "
\n",
+ " \n",
+ " | 3232 | \n",
+ " Pulp Fiction | \n",
+ " 8.3 | \n",
+ " 8.207102 | \n",
+ " 8428 | \n",
+ "
\n",
+ " \n",
+ " | 65 | \n",
+ " The Dark Knight | \n",
+ " 8.2 | \n",
+ " 8.136930 | \n",
+ " 12002 | \n",
+ "
\n",
+ " \n",
+ " | 1818 | \n",
+ " Schindler's List | \n",
+ " 8.3 | \n",
+ " 8.126069 | \n",
+ " 4329 | \n",
+ "
\n",
+ " \n",
+ " | 3865 | \n",
+ " Whiplash | \n",
+ " 8.3 | \n",
+ " 8.123248 | \n",
+ " 4254 | \n",
+ "
\n",
+ " \n",
+ " | 809 | \n",
+ " Forrest Gump | \n",
+ " 8.2 | \n",
+ " 8.105954 | \n",
+ " 7927 | \n",
+ "
\n",
+ " \n",
+ " | 2294 | \n",
+ " Spirited Away | \n",
+ " 8.3 | \n",
+ " 8.105867 | \n",
+ " 3840 | \n",
+ "
\n",
+ " \n",
+ " | 2731 | \n",
+ " The Godfather: Part II | \n",
+ " 8.3 | \n",
+ " 8.079586 | \n",
+ " 3338 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ "
\n",
+ "
\n"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "dataframe",
+ "summary": "{\n \"name\": \" 'weighted_vote', ascending=False)[:10]\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"Spirited Away\",\n \"The Godfather\",\n \"Schindler's List\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_average\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.08755950357709151,\n \"min\": 8.2,\n \"max\": 8.5,\n \"num_unique_values\": 4,\n \"samples\": [\n 8.4,\n 8.2,\n 8.5\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"weighted_vote\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.09696608479450805,\n \"min\": 8.07958629828635,\n \"max\": 8.39605162693645,\n \"num_unique_values\": 10,\n \"samples\": [\n 8.105867158639835,\n 8.263590802034972,\n 8.126068673669016\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_count\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2866,\n \"min\": 3338,\n \"max\": 12002,\n \"num_unique_values\": 10,\n \"samples\": [\n 3840,\n 5893,\n 4329\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
+ }
+ },
+ "metadata": {},
+ "execution_count": 16
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def find_sim_movie(df, sorted_ind, title_name, top_n=10):\n",
+ " title_movie = df[df['title'] == title_name]\n",
+ " title_index = title_movie.index.values\n",
+ "\n",
+ " similar_indexes = sorted_ind[title_index, :(top_n)]\n",
+ "\n",
+ " similar_indexes = similar_indexes.reshape(-1)\n",
+ " similar_indexes = similar_indexes[similar_indexes != title_index]\n",
+ "\n",
+ " return df.iloc[similar_indexes].sort_values('weighted_vote', ascending=False)[:top_n]\n",
+ "\n",
+ "\n",
+ "similar_movies = find_sim_movie(movies_df, genre_sim_sorted_ind, 'The Godfather', 10)\n",
+ "similar_movies[['title', 'vote_average', 'weighted_vote']]\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 363
+ },
+ "id": "DY7CvKdJiqxk",
+ "outputId": "72169d85-79ae-4ee9-9f1a-4e0cff67c67d"
+ },
+ "execution_count": 17,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ " title vote_average weighted_vote\n",
+ "1881 The Shawshank Redemption 8.5 8.396052\n",
+ "3866 City of God 8.1 7.759693\n",
+ "3887 Trainspotting 7.8 7.591009\n",
+ "892 Casino 7.8 7.423040\n",
+ "2839 Rounders 6.9 6.530427\n",
+ "1370 21 6.5 6.413490\n",
+ "3378 Auto Focus 6.1 6.093200\n",
+ "1464 Black Water Transit 0.0 6.092172\n",
+ "588 Wall Street: Money Never Sleeps 5.8 5.925303\n",
+ "3594 Spring Breakers 5.0 5.210453"
+ ],
+ "text/html": [
+ "\n",
+ " \n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " title | \n",
+ " vote_average | \n",
+ " weighted_vote | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 1881 | \n",
+ " The Shawshank Redemption | \n",
+ " 8.5 | \n",
+ " 8.396052 | \n",
+ "
\n",
+ " \n",
+ " | 3866 | \n",
+ " City of God | \n",
+ " 8.1 | \n",
+ " 7.759693 | \n",
+ "
\n",
+ " \n",
+ " | 3887 | \n",
+ " Trainspotting | \n",
+ " 7.8 | \n",
+ " 7.591009 | \n",
+ "
\n",
+ " \n",
+ " | 892 | \n",
+ " Casino | \n",
+ " 7.8 | \n",
+ " 7.423040 | \n",
+ "
\n",
+ " \n",
+ " | 2839 | \n",
+ " Rounders | \n",
+ " 6.9 | \n",
+ " 6.530427 | \n",
+ "
\n",
+ " \n",
+ " | 1370 | \n",
+ " 21 | \n",
+ " 6.5 | \n",
+ " 6.413490 | \n",
+ "
\n",
+ " \n",
+ " | 3378 | \n",
+ " Auto Focus | \n",
+ " 6.1 | \n",
+ " 6.093200 | \n",
+ "
\n",
+ " \n",
+ " | 1464 | \n",
+ " Black Water Transit | \n",
+ " 0.0 | \n",
+ " 6.092172 | \n",
+ "
\n",
+ " \n",
+ " | 588 | \n",
+ " Wall Street: Money Never Sleeps | \n",
+ " 5.8 | \n",
+ " 5.925303 | \n",
+ "
\n",
+ " \n",
+ " | 3594 | \n",
+ " Spring Breakers | \n",
+ " 5.0 | \n",
+ " 5.210453 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ "
\n",
+ "
\n"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "dataframe",
+ "summary": "{\n \"name\": \"similar_movies[['title', 'vote_average', 'weighted_vote']]\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"Wall Street: Money Never Sleeps\",\n \"City of God\",\n \"21\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_average\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2.4636242498490803,\n \"min\": 0.0,\n \"max\": 8.5,\n \"num_unique_values\": 9,\n \"samples\": [\n 5.8,\n 8.1,\n 6.1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"weighted_vote\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.9980384451283587,\n \"min\": 5.210452795807534,\n \"max\": 8.39605162693645,\n \"num_unique_values\": 10,\n \"samples\": [\n 5.925303419028538,\n 7.759693210926396,\n 6.413489520573822\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
+ }
+ },
+ "metadata": {},
+ "execution_count": 17
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "import pandas as pd\n",
+ "import numpy as np\n",
+ "movies = pd.read_csv('/content/drive/MyDrive/Euron/ml-latest-small/movies.csv')\n",
+ "ratings = pd.read_csv('/content/drive/MyDrive/Euron/ml-latest-small/ratings.csv')\n",
+ "print(ratings.shape)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "XagGQcaXiquU",
+ "outputId": "e8ef71c2-983b-421d-8a69-ab1b76dc0f8f"
+ },
+ "execution_count": 1,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "(100836, 4)\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "rating_movies = pd.merge(ratings, movies, on='movieId')\n",
+ "\n",
+ "ratings_matrix = rating_movies.pivot_table('rating', index='userId', columns='title')\n"
+ ],
+ "metadata": {
+ "id": "wCWDQBUoiqsC"
+ },
+ "execution_count": 2,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "ratings_matrix = ratings_matrix.fillna(0)\n",
+ "ratings_matrix.head(3)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 346
+ },
+ "id": "rftK7IazkOpz",
+ "outputId": "5c7b2844-b25c-4a4b-df1d-2cae6d2526e1"
+ },
+ "execution_count": 3,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "title '71 (2014) 'Hellboy': The Seeds of Creation (2004) \\\n",
+ "userId \n",
+ "1 0.0 0.0 \n",
+ "2 0.0 0.0 \n",
+ "3 0.0 0.0 \n",
+ "\n",
+ "title 'Round Midnight (1986) 'Salem's Lot (2004) \\\n",
+ "userId \n",
+ "1 0.0 0.0 \n",
+ "2 0.0 0.0 \n",
+ "3 0.0 0.0 \n",
+ "\n",
+ "title 'Til There Was You (1997) 'Tis the Season for Love (2015) \\\n",
+ "userId \n",
+ "1 0.0 0.0 \n",
+ "2 0.0 0.0 \n",
+ "3 0.0 0.0 \n",
+ "\n",
+ "title 'burbs, The (1989) 'night Mother (1986) (500) Days of Summer (2009) \\\n",
+ "userId \n",
+ "1 0.0 0.0 0.0 \n",
+ "2 0.0 0.0 0.0 \n",
+ "3 0.0 0.0 0.0 \n",
+ "\n",
+ "title *batteries not included (1987) ... Zulu (2013) [REC] (2007) \\\n",
+ "userId ... \n",
+ "1 0.0 ... 0.0 0.0 \n",
+ "2 0.0 ... 0.0 0.0 \n",
+ "3 0.0 ... 0.0 0.0 \n",
+ "\n",
+ "title [REC]² (2009) [REC]³ 3 Génesis (2012) \\\n",
+ "userId \n",
+ "1 0.0 0.0 \n",
+ "2 0.0 0.0 \n",
+ "3 0.0 0.0 \n",
+ "\n",
+ "title anohana: The Flower We Saw That Day - The Movie (2013) \\\n",
+ "userId \n",
+ "1 0.0 \n",
+ "2 0.0 \n",
+ "3 0.0 \n",
+ "\n",
+ "title eXistenZ (1999) xXx (2002) xXx: State of the Union (2005) \\\n",
+ "userId \n",
+ "1 0.0 0.0 0.0 \n",
+ "2 0.0 0.0 0.0 \n",
+ "3 0.0 0.0 0.0 \n",
+ "\n",
+ "title ¡Three Amigos! (1986) À nous la liberté (Freedom for Us) (1931) \n",
+ "userId \n",
+ "1 4.0 0.0 \n",
+ "2 0.0 0.0 \n",
+ "3 0.0 0.0 \n",
+ "\n",
+ "[3 rows x 9719 columns]"
+ ],
+ "text/html": [
+ "\n",
+ " \n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | title | \n",
+ " '71 (2014) | \n",
+ " 'Hellboy': The Seeds of Creation (2004) | \n",
+ " 'Round Midnight (1986) | \n",
+ " 'Salem's Lot (2004) | \n",
+ " 'Til There Was You (1997) | \n",
+ " 'Tis the Season for Love (2015) | \n",
+ " 'burbs, The (1989) | \n",
+ " 'night Mother (1986) | \n",
+ " (500) Days of Summer (2009) | \n",
+ " *batteries not included (1987) | \n",
+ " ... | \n",
+ " Zulu (2013) | \n",
+ " [REC] (2007) | \n",
+ " [REC]² (2009) | \n",
+ " [REC]³ 3 Génesis (2012) | \n",
+ " anohana: The Flower We Saw That Day - The Movie (2013) | \n",
+ " eXistenZ (1999) | \n",
+ " xXx (2002) | \n",
+ " xXx: State of the Union (2005) | \n",
+ " ¡Three Amigos! (1986) | \n",
+ " À nous la liberté (Freedom for Us) (1931) | \n",
+ "
\n",
+ " \n",
+ " | userId | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 1 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 4.0 | \n",
+ " 0.0 | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
3 rows × 9719 columns
\n",
+ "
\n",
+ "
\n",
+ "
\n"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "dataframe",
+ "variable_name": "ratings_matrix"
+ }
+ },
+ "metadata": {},
+ "execution_count": 3
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "ratings_matrix_T = ratings_matrix.transpose()\n",
+ "ratings_matrix_T.head(3)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 346
+ },
+ "id": "HYwMrPqakOjb",
+ "outputId": "3ffe11f9-ca69-4526-b7fc-7ef31087af37"
+ },
+ "execution_count": 4,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "userId 1 2 3 4 5 6 7 \\\n",
+ "title \n",
+ "'71 (2014) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
+ "'Round Midnight (1986) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
+ "\n",
+ "userId 8 9 10 ... 601 602 603 \\\n",
+ "title ... \n",
+ "'71 (2014) 0.0 0.0 0.0 ... 0.0 0.0 0.0 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.0 0.0 0.0 ... 0.0 0.0 0.0 \n",
+ "'Round Midnight (1986) 0.0 0.0 0.0 ... 0.0 0.0 0.0 \n",
+ "\n",
+ "userId 604 605 606 607 608 609 610 \n",
+ "title \n",
+ "'71 (2014) 0.0 0.0 0.0 0.0 0.0 0.0 4.0 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
+ "'Round Midnight (1986) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
+ "\n",
+ "[3 rows x 610 columns]"
+ ],
+ "text/html": [
+ "\n",
+ " \n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | userId | \n",
+ " 1 | \n",
+ " 2 | \n",
+ " 3 | \n",
+ " 4 | \n",
+ " 5 | \n",
+ " 6 | \n",
+ " 7 | \n",
+ " 8 | \n",
+ " 9 | \n",
+ " 10 | \n",
+ " ... | \n",
+ " 601 | \n",
+ " 602 | \n",
+ " 603 | \n",
+ " 604 | \n",
+ " 605 | \n",
+ " 606 | \n",
+ " 607 | \n",
+ " 608 | \n",
+ " 609 | \n",
+ " 610 | \n",
+ "
\n",
+ " \n",
+ " | title | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | '71 (2014) | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 4.0 | \n",
+ "
\n",
+ " \n",
+ " | 'Hellboy': The Seeds of Creation (2004) | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ "
\n",
+ " \n",
+ " | 'Round Midnight (1986) | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
3 rows × 610 columns
\n",
+ "
\n",
+ "
\n",
+ "
\n"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "dataframe",
+ "variable_name": "ratings_matrix_T"
+ }
+ },
+ "metadata": {},
+ "execution_count": 4
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from sklearn.metrics.pairwise import cosine_similarity\n",
+ "\n",
+ "item_sim = cosine_similarity(ratings_matrix_T, ratings_matrix_T)\n",
+ "\n",
+ "item_sim_df = pd.DataFrame(\n",
+ " data=item_sim,\n",
+ " index=ratings_matrix.columns,\n",
+ " columns=ratings_matrix.columns\n",
+ ")\n",
+ "\n",
+ "print(item_sim_df.shape)\n",
+ "item_sim_df.head(3)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 486
+ },
+ "id": "ZZy2NSqnkOg2",
+ "outputId": "9d015f6d-e811-49ed-cf60-515a6e8a45d3"
+ },
+ "execution_count": 5,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "(9719, 9719)\n"
+ ]
+ },
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "title '71 (2014) \\\n",
+ "title \n",
+ "'71 (2014) 1.0 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.0 \n",
+ "'Round Midnight (1986) 0.0 \n",
+ "\n",
+ "title 'Hellboy': The Seeds of Creation (2004) \\\n",
+ "title \n",
+ "'71 (2014) 0.000000 \n",
+ "'Hellboy': The Seeds of Creation (2004) 1.000000 \n",
+ "'Round Midnight (1986) 0.707107 \n",
+ "\n",
+ "title 'Round Midnight (1986) \\\n",
+ "title \n",
+ "'71 (2014) 0.000000 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.707107 \n",
+ "'Round Midnight (1986) 1.000000 \n",
+ "\n",
+ "title 'Salem's Lot (2004) \\\n",
+ "title \n",
+ "'71 (2014) 0.0 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.0 \n",
+ "'Round Midnight (1986) 0.0 \n",
+ "\n",
+ "title 'Til There Was You (1997) \\\n",
+ "title \n",
+ "'71 (2014) 0.0 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.0 \n",
+ "'Round Midnight (1986) 0.0 \n",
+ "\n",
+ "title 'Tis the Season for Love (2015) \\\n",
+ "title \n",
+ "'71 (2014) 0.0 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.0 \n",
+ "'Round Midnight (1986) 0.0 \n",
+ "\n",
+ "title 'burbs, The (1989) \\\n",
+ "title \n",
+ "'71 (2014) 0.000000 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.000000 \n",
+ "'Round Midnight (1986) 0.176777 \n",
+ "\n",
+ "title 'night Mother (1986) \\\n",
+ "title \n",
+ "'71 (2014) 0.0 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.0 \n",
+ "'Round Midnight (1986) 0.0 \n",
+ "\n",
+ "title (500) Days of Summer (2009) \\\n",
+ "title \n",
+ "'71 (2014) 0.141653 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.000000 \n",
+ "'Round Midnight (1986) 0.000000 \n",
+ "\n",
+ "title *batteries not included (1987) ... \\\n",
+ "title ... \n",
+ "'71 (2014) 0.0 ... \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.0 ... \n",
+ "'Round Midnight (1986) 0.0 ... \n",
+ "\n",
+ "title Zulu (2013) [REC] (2007) \\\n",
+ "title \n",
+ "'71 (2014) 0.0 0.342055 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.0 0.000000 \n",
+ "'Round Midnight (1986) 0.0 0.000000 \n",
+ "\n",
+ "title [REC]² (2009) \\\n",
+ "title \n",
+ "'71 (2014) 0.543305 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.000000 \n",
+ "'Round Midnight (1986) 0.000000 \n",
+ "\n",
+ "title [REC]³ 3 Génesis (2012) \\\n",
+ "title \n",
+ "'71 (2014) 0.707107 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.000000 \n",
+ "'Round Midnight (1986) 0.000000 \n",
+ "\n",
+ "title anohana: The Flower We Saw That Day - The Movie (2013) \\\n",
+ "title \n",
+ "'71 (2014) 0.0 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.0 \n",
+ "'Round Midnight (1986) 0.0 \n",
+ "\n",
+ "title eXistenZ (1999) xXx (2002) \\\n",
+ "title \n",
+ "'71 (2014) 0.0 0.139431 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.0 0.000000 \n",
+ "'Round Midnight (1986) 0.0 0.000000 \n",
+ "\n",
+ "title xXx: State of the Union (2005) \\\n",
+ "title \n",
+ "'71 (2014) 0.327327 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.000000 \n",
+ "'Round Midnight (1986) 0.000000 \n",
+ "\n",
+ "title ¡Three Amigos! (1986) \\\n",
+ "title \n",
+ "'71 (2014) 0.0 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.0 \n",
+ "'Round Midnight (1986) 0.0 \n",
+ "\n",
+ "title À nous la liberté (Freedom for Us) (1931) \n",
+ "title \n",
+ "'71 (2014) 0.0 \n",
+ "'Hellboy': The Seeds of Creation (2004) 0.0 \n",
+ "'Round Midnight (1986) 0.0 \n",
+ "\n",
+ "[3 rows x 9719 columns]"
+ ],
+ "text/html": [
+ "\n",
+ " \n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | title | \n",
+ " '71 (2014) | \n",
+ " 'Hellboy': The Seeds of Creation (2004) | \n",
+ " 'Round Midnight (1986) | \n",
+ " 'Salem's Lot (2004) | \n",
+ " 'Til There Was You (1997) | \n",
+ " 'Tis the Season for Love (2015) | \n",
+ " 'burbs, The (1989) | \n",
+ " 'night Mother (1986) | \n",
+ " (500) Days of Summer (2009) | \n",
+ " *batteries not included (1987) | \n",
+ " ... | \n",
+ " Zulu (2013) | \n",
+ " [REC] (2007) | \n",
+ " [REC]² (2009) | \n",
+ " [REC]³ 3 Génesis (2012) | \n",
+ " anohana: The Flower We Saw That Day - The Movie (2013) | \n",
+ " eXistenZ (1999) | \n",
+ " xXx (2002) | \n",
+ " xXx: State of the Union (2005) | \n",
+ " ¡Three Amigos! (1986) | \n",
+ " À nous la liberté (Freedom for Us) (1931) | \n",
+ "
\n",
+ " \n",
+ " | title | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | '71 (2014) | \n",
+ " 1.0 | \n",
+ " 0.000000 | \n",
+ " 0.000000 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.000000 | \n",
+ " 0.0 | \n",
+ " 0.141653 | \n",
+ " 0.0 | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 0.342055 | \n",
+ " 0.543305 | \n",
+ " 0.707107 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.139431 | \n",
+ " 0.327327 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ "
\n",
+ " \n",
+ " | 'Hellboy': The Seeds of Creation (2004) | \n",
+ " 0.0 | \n",
+ " 1.000000 | \n",
+ " 0.707107 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.000000 | \n",
+ " 0.0 | \n",
+ " 0.000000 | \n",
+ " 0.0 | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 0.000000 | \n",
+ " 0.000000 | \n",
+ " 0.000000 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.000000 | \n",
+ " 0.000000 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ "
\n",
+ " \n",
+ " | 'Round Midnight (1986) | \n",
+ " 0.0 | \n",
+ " 0.707107 | \n",
+ " 1.000000 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.176777 | \n",
+ " 0.0 | \n",
+ " 0.000000 | \n",
+ " 0.0 | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 0.000000 | \n",
+ " 0.000000 | \n",
+ " 0.000000 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ " 0.000000 | \n",
+ " 0.000000 | \n",
+ " 0.0 | \n",
+ " 0.0 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
3 rows × 9719 columns
\n",
+ "
\n",
+ "
\n",
+ "
\n"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "dataframe",
+ "variable_name": "item_sim_df"
+ }
+ },
+ "metadata": {},
+ "execution_count": 5
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "item_sim_df['Godfather, The (1972)'].sort_values(ascending=False)[:6]\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 304
+ },
+ "id": "PXa0pKUzkOdi",
+ "outputId": "3392bb06-5cb5-488c-e90b-c4477ccb00cf"
+ },
+ "execution_count": 7,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "title\n",
+ "Godfather, The (1972) 1.000000\n",
+ "Godfather: Part II, The (1974) 0.821773\n",
+ "Goodfellas (1990) 0.664841\n",
+ "One Flew Over the Cuckoo's Nest (1975) 0.620536\n",
+ "Star Wars: Episode IV - A New Hope (1977) 0.595317\n",
+ "Fargo (1996) 0.588614\n",
+ "Name: Godfather, The (1972), dtype: float64"
+ ],
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " Godfather, The (1972) | \n",
+ "
\n",
+ " \n",
+ " | title | \n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | Godfather, The (1972) | \n",
+ " 1.000000 | \n",
+ "
\n",
+ " \n",
+ " | Godfather: Part II, The (1974) | \n",
+ " 0.821773 | \n",
+ "
\n",
+ " \n",
+ " | Goodfellas (1990) | \n",
+ " 0.664841 | \n",
+ "
\n",
+ " \n",
+ " | One Flew Over the Cuckoo's Nest (1975) | \n",
+ " 0.620536 | \n",
+ "
\n",
+ " \n",
+ " | Star Wars: Episode IV - A New Hope (1977) | \n",
+ " 0.595317 | \n",
+ "
\n",
+ " \n",
+ " | Fargo (1996) | \n",
+ " 0.588614 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 7
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "item_sim_df['Inception (2010)'].sort_values(ascending=False)[:6]\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 304
+ },
+ "id": "pAC6jX6AkObS",
+ "outputId": "c1e3368d-7e6e-414d-a6d0-8b26beb8afe2"
+ },
+ "execution_count": 6,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "title\n",
+ "Inception (2010) 1.000000\n",
+ "Dark Knight, The (2008) 0.727263\n",
+ "Inglourious Basterds (2009) 0.646103\n",
+ "Shutter Island (2010) 0.617736\n",
+ "Dark Knight Rises, The (2012) 0.617504\n",
+ "Fight Club (1999) 0.615417\n",
+ "Name: Inception (2010), dtype: float64"
+ ],
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " Inception (2010) | \n",
+ "
\n",
+ " \n",
+ " | title | \n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | Inception (2010) | \n",
+ " 1.000000 | \n",
+ "
\n",
+ " \n",
+ " | Dark Knight, The (2008) | \n",
+ " 0.727263 | \n",
+ "
\n",
+ " \n",
+ " | Inglourious Basterds (2009) | \n",
+ " 0.646103 | \n",
+ "
\n",
+ " \n",
+ " | Shutter Island (2010) | \n",
+ " 0.617736 | \n",
+ "
\n",
+ " \n",
+ " | Dark Knight Rises, The (2012) | \n",
+ " 0.617504 | \n",
+ "
\n",
+ " \n",
+ " | Fight Club (1999) | \n",
+ " 0.615417 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 6
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def predict_rating(ratings_arr, item_sim_arr):\n",
+ " ratings_pred = ratings_arr.dot(item_sim_arr) / np.array([np.abs(item_sim_arr).sum(axis=1)])\n",
+ " return ratings_pred\n"
+ ],
+ "metadata": {
+ "id": "M28VlNUkkOZE"
+ },
+ "execution_count": 8,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "ratings_pred = predict_rating(ratings_matrix.values, item_sim_df.values)\n",
+ "\n",
+ "ratings_pred_matrix = pd.DataFrame(\n",
+ " data=ratings_pred,\n",
+ " index=ratings_matrix.index,\n",
+ " columns=ratings_matrix.columns\n",
+ ")\n",
+ "\n",
+ "ratings_pred_matrix.head(3)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 346
+ },
+ "id": "SCyvGa45kOW1",
+ "outputId": "7e07052c-d4f9-42e7-ce64-fe886c85613a"
+ },
+ "execution_count": 9,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "title '71 (2014) 'Hellboy': The Seeds of Creation (2004) \\\n",
+ "userId \n",
+ "1 0.070345 0.577855 \n",
+ "2 0.018260 0.042744 \n",
+ "3 0.011884 0.030279 \n",
+ "\n",
+ "title 'Round Midnight (1986) 'Salem's Lot (2004) \\\n",
+ "userId \n",
+ "1 0.321696 0.227055 \n",
+ "2 0.018861 0.000000 \n",
+ "3 0.064437 0.003762 \n",
+ "\n",
+ "title 'Til There Was You (1997) 'Tis the Season for Love (2015) \\\n",
+ "userId \n",
+ "1 0.206958 0.194615 \n",
+ "2 0.000000 0.035995 \n",
+ "3 0.003749 0.002722 \n",
+ "\n",
+ "title 'burbs, The (1989) 'night Mother (1986) (500) Days of Summer (2009) \\\n",
+ "userId \n",
+ "1 0.249883 0.102542 0.157084 \n",
+ "2 0.013413 0.002314 0.032213 \n",
+ "3 0.014625 0.002085 0.005666 \n",
+ "\n",
+ "title *batteries not included (1987) ... Zulu (2013) [REC] (2007) \\\n",
+ "userId ... \n",
+ "1 0.178197 ... 0.113608 0.181738 \n",
+ "2 0.014863 ... 0.015640 0.020855 \n",
+ "3 0.006272 ... 0.006923 0.011665 \n",
+ "\n",
+ "title [REC]² (2009) [REC]³ 3 Génesis (2012) \\\n",
+ "userId \n",
+ "1 0.133962 0.128574 \n",
+ "2 0.020119 0.015745 \n",
+ "3 0.011800 0.012225 \n",
+ "\n",
+ "title anohana: The Flower We Saw That Day - The Movie (2013) \\\n",
+ "userId \n",
+ "1 0.006179 \n",
+ "2 0.049983 \n",
+ "3 0.000000 \n",
+ "\n",
+ "title eXistenZ (1999) xXx (2002) xXx: State of the Union (2005) \\\n",
+ "userId \n",
+ "1 0.212070 0.192921 0.136024 \n",
+ "2 0.014876 0.021616 0.024528 \n",
+ "3 0.008194 0.007017 0.009229 \n",
+ "\n",
+ "title ¡Three Amigos! (1986) À nous la liberté (Freedom for Us) (1931) \n",
+ "userId \n",
+ "1 0.292955 0.720347 \n",
+ "2 0.017563 0.000000 \n",
+ "3 0.010420 0.084501 \n",
+ "\n",
+ "[3 rows x 9719 columns]"
+ ],
+ "text/html": [
+ "\n",
+ " \n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | title | \n",
+ " '71 (2014) | \n",
+ " 'Hellboy': The Seeds of Creation (2004) | \n",
+ " 'Round Midnight (1986) | \n",
+ " 'Salem's Lot (2004) | \n",
+ " 'Til There Was You (1997) | \n",
+ " 'Tis the Season for Love (2015) | \n",
+ " 'burbs, The (1989) | \n",
+ " 'night Mother (1986) | \n",
+ " (500) Days of Summer (2009) | \n",
+ " *batteries not included (1987) | \n",
+ " ... | \n",
+ " Zulu (2013) | \n",
+ " [REC] (2007) | \n",
+ " [REC]² (2009) | \n",
+ " [REC]³ 3 Génesis (2012) | \n",
+ " anohana: The Flower We Saw That Day - The Movie (2013) | \n",
+ " eXistenZ (1999) | \n",
+ " xXx (2002) | \n",
+ " xXx: State of the Union (2005) | \n",
+ " ¡Three Amigos! (1986) | \n",
+ " À nous la liberté (Freedom for Us) (1931) | \n",
+ "
\n",
+ " \n",
+ " | userId | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 1 | \n",
+ " 0.070345 | \n",
+ " 0.577855 | \n",
+ " 0.321696 | \n",
+ " 0.227055 | \n",
+ " 0.206958 | \n",
+ " 0.194615 | \n",
+ " 0.249883 | \n",
+ " 0.102542 | \n",
+ " 0.157084 | \n",
+ " 0.178197 | \n",
+ " ... | \n",
+ " 0.113608 | \n",
+ " 0.181738 | \n",
+ " 0.133962 | \n",
+ " 0.128574 | \n",
+ " 0.006179 | \n",
+ " 0.212070 | \n",
+ " 0.192921 | \n",
+ " 0.136024 | \n",
+ " 0.292955 | \n",
+ " 0.720347 | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " 0.018260 | \n",
+ " 0.042744 | \n",
+ " 0.018861 | \n",
+ " 0.000000 | \n",
+ " 0.000000 | \n",
+ " 0.035995 | \n",
+ " 0.013413 | \n",
+ " 0.002314 | \n",
+ " 0.032213 | \n",
+ " 0.014863 | \n",
+ " ... | \n",
+ " 0.015640 | \n",
+ " 0.020855 | \n",
+ " 0.020119 | \n",
+ " 0.015745 | \n",
+ " 0.049983 | \n",
+ " 0.014876 | \n",
+ " 0.021616 | \n",
+ " 0.024528 | \n",
+ " 0.017563 | \n",
+ " 0.000000 | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " 0.011884 | \n",
+ " 0.030279 | \n",
+ " 0.064437 | \n",
+ " 0.003762 | \n",
+ " 0.003749 | \n",
+ " 0.002722 | \n",
+ " 0.014625 | \n",
+ " 0.002085 | \n",
+ " 0.005666 | \n",
+ " 0.006272 | \n",
+ " ... | \n",
+ " 0.006923 | \n",
+ " 0.011665 | \n",
+ " 0.011800 | \n",
+ " 0.012225 | \n",
+ " 0.000000 | \n",
+ " 0.008194 | \n",
+ " 0.007017 | \n",
+ " 0.009229 | \n",
+ " 0.010420 | \n",
+ " 0.084501 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
3 rows × 9719 columns
\n",
+ "
\n",
+ "
\n",
+ "
\n"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "dataframe",
+ "variable_name": "ratings_pred_matrix"
+ }
+ },
+ "metadata": {},
+ "execution_count": 9
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from sklearn.metrics import mean_squared_error\n",
+ "\n",
+ "def get_mse(pred, actual):\n",
+ " pred = pred[actual.nonzero()].flatten()\n",
+ " actual = actual[actual.nonzero()].flatten()\n",
+ " return mean_squared_error(pred, actual)\n",
+ "\n",
+ "print('아이템 기반 전체 평균 MSE : ', get_mse(ratings_pred, ratings_matrix.values))\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "yADlSImTkOUP",
+ "outputId": "c80934c1-02e7-482d-d8ba-351d87dd0a86"
+ },
+ "execution_count": 10,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "아이템 기반 전체 평균 MSE : 9.895354759094706\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def predict_rating_topsim(ratings_arr, item_sim_arr, n=20):\n",
+ " pred = np.zeros(ratings_arr.shape)\n",
+ "\n",
+ " for col in range(ratings_arr.shape[1]):\n",
+ " top_n_items = [np.argsort(item_sim_arr[:, col])[:-n-1:-1]]\n",
+ "\n",
+ " for row in range(ratings_arr.shape[0]):\n",
+ " pred[row, col] = item_sim_arr[col, :][top_n_items].dot(\n",
+ " ratings_arr[row, :][top_n_items].T\n",
+ " )\n",
+ " pred[row, col] /= np.sum(np.abs(item_sim_arr[col, :][top_n_items]))\n",
+ "\n",
+ " return pred\n"
+ ],
+ "metadata": {
+ "id": "06n5BS3QkOR5"
+ },
+ "execution_count": 11,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "ratings_pred_matrix = pd.DataFrame(\n",
+ " data=ratings_pred,\n",
+ " index=ratings_matrix.index,\n",
+ " columns=ratings_matrix.columns\n",
+ ")\n"
+ ],
+ "metadata": {
+ "id": "N4pOdnVTke2K"
+ },
+ "execution_count": 12,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "user_rating_id = ratings_matrix.loc[9, :]\n",
+ "user_rating_id[user_rating_id > 0].sort_values(ascending=False)[:10]\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 429
+ },
+ "id": "vOA00B76keyv",
+ "outputId": "21acc54f-71e3-459b-8382-0d18908cc47e"
+ },
+ "execution_count": 13,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "title\n",
+ "Adaptation (2002) 5.0\n",
+ "Austin Powers in Goldmember (2002) 5.0\n",
+ "Back to the Future (1985) 5.0\n",
+ "Citizen Kane (1941) 5.0\n",
+ "Lord of the Rings: The Fellowship of the Ring, The (2001) 5.0\n",
+ "Lord of the Rings: The Two Towers, The (2002) 5.0\n",
+ "Producers, The (1968) 5.0\n",
+ "Raiders of the Lost Ark (Indiana Jones and the Raiders of the Lost Ark) (1981) 5.0\n",
+ "Elling (2001) 4.0\n",
+ "King of Comedy, The (1983) 4.0\n",
+ "Name: 9, dtype: float64"
+ ],
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " | title | \n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | Adaptation (2002) | \n",
+ " 5.0 | \n",
+ "
\n",
+ " \n",
+ " | Austin Powers in Goldmember (2002) | \n",
+ " 5.0 | \n",
+ "
\n",
+ " \n",
+ " | Back to the Future (1985) | \n",
+ " 5.0 | \n",
+ "
\n",
+ " \n",
+ " | Citizen Kane (1941) | \n",
+ " 5.0 | \n",
+ "
\n",
+ " \n",
+ " | Lord of the Rings: The Fellowship of the Ring, The (2001) | \n",
+ " 5.0 | \n",
+ "
\n",
+ " \n",
+ " | Lord of the Rings: The Two Towers, The (2002) | \n",
+ " 5.0 | \n",
+ "
\n",
+ " \n",
+ " | Producers, The (1968) | \n",
+ " 5.0 | \n",
+ "
\n",
+ " \n",
+ " | Raiders of the Lost Ark (Indiana Jones and the Raiders of the Lost Ark) (1981) | \n",
+ " 5.0 | \n",
+ "
\n",
+ " \n",
+ " | Elling (2001) | \n",
+ " 4.0 | \n",
+ "
\n",
+ " \n",
+ " | King of Comedy, The (1983) | \n",
+ " 4.0 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 13
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def get_unseen_movies(ratings_matrix, userId):\n",
+ " user_rating = ratings_matrix.loc[userId, :]\n",
+ "\n",
+ " already_seen = user_rating[user_rating > 0].index.tolist()\n",
+ "\n",
+ " movies_list = ratings_matrix.columns.tolist()\n",
+ "\n",
+ " unseen_list = [movie for movie in movies_list if movie not in already_seen]\n",
+ "\n",
+ " return unseen_list\n"
+ ],
+ "metadata": {
+ "id": "hfGfCFFHkewm"
+ },
+ "execution_count": 14,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def recom_movie_by_userid(pred_df, userId, unseen_list, top_n=10):\n",
+ " recomm_movies = pred_df.loc[userId, unseen_list].sort_values(ascending=False)[:top_n]\n",
+ " return recomm_movies\n",
+ "\n",
+ "\n",
+ "unseen_list = get_unseen_movies(ratings_matrix, 9)\n",
+ "\n",
+ "recom_movies = recom_movie_by_userid(ratings_pred_matrix, 9, unseen_list, top_n=10)\n",
+ "\n",
+ "recom_movies = pd.DataFrame(\n",
+ " data=recom_movies.values,\n",
+ " index=recom_movies.index,\n",
+ " columns=['pred_score']\n",
+ ")\n",
+ "\n",
+ "recom_movies\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 394
+ },
+ "id": "1fkjyzvpkerc",
+ "outputId": "007c7c36-e4ef-4fbc-83e6-061bfefac7ed"
+ },
+ "execution_count": 15,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ " pred_score\n",
+ "title \n",
+ "Venom (1982) 0.303278\n",
+ "Dr. Goldfoot and the Bikini Machine (1965) 0.258705\n",
+ "Frankie and Johnny (1966) 0.234754\n",
+ "English Vinglish (2012) 0.214774\n",
+ "Harmonists, The (1997) 0.169338\n",
+ "Cassandra's Dream (2007) 0.163884\n",
+ "Story of Women (Affaire de femmes, Une) (1988) 0.163884\n",
+ "Marriage of Maria Braun, The (Ehe der Maria Bra... 0.163884\n",
+ "Passenger, The (Professione: reporter) (1975) 0.163884\n",
+ "3:10 to Yuma (1957) 0.163884"
+ ],
+ "text/html": [
+ "\n",
+ " \n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " pred_score | \n",
+ "
\n",
+ " \n",
+ " | title | \n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | Venom (1982) | \n",
+ " 0.303278 | \n",
+ "
\n",
+ " \n",
+ " | Dr. Goldfoot and the Bikini Machine (1965) | \n",
+ " 0.258705 | \n",
+ "
\n",
+ " \n",
+ " | Frankie and Johnny (1966) | \n",
+ " 0.234754 | \n",
+ "
\n",
+ " \n",
+ " | English Vinglish (2012) | \n",
+ " 0.214774 | \n",
+ "
\n",
+ " \n",
+ " | Harmonists, The (1997) | \n",
+ " 0.169338 | \n",
+ "
\n",
+ " \n",
+ " | Cassandra's Dream (2007) | \n",
+ " 0.163884 | \n",
+ "
\n",
+ " \n",
+ " | Story of Women (Affaire de femmes, Une) (1988) | \n",
+ " 0.163884 | \n",
+ "
\n",
+ " \n",
+ " | Marriage of Maria Braun, The (Ehe der Maria Braun, Die) (1979) | \n",
+ " 0.163884 | \n",
+ "
\n",
+ " \n",
+ " | Passenger, The (Professione: reporter) (1975) | \n",
+ " 0.163884 | \n",
+ "
\n",
+ " \n",
+ " | 3:10 to Yuma (1957) | \n",
+ " 0.163884 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ "
\n",
+ "
\n"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "dataframe",
+ "variable_name": "recom_movies",
+ "summary": "{\n \"name\": \"recom_movies\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"Passenger, The (Professione: reporter) (1975)\",\n \"Dr. Goldfoot and the Bikini Machine (1965)\",\n \"Cassandra's Dream (2007)\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"pred_score\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.0505535384403895,\n \"min\": 0.16388374689344673,\n \"max\": 0.3032783247338148,\n \"num_unique_values\": 6,\n \"samples\": [\n 0.3032783247338148,\n 0.2587053710450948,\n 0.16388374689344673\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
+ }
+ },
+ "metadata": {},
+ "execution_count": 15
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def matrix_factorization(R, K, steps=200, learning_rate=0.01, r_lambda=0.01):\n",
+ " num_users, num_items = R.shape\n",
+ "\n",
+ " np.random.seed(1)\n",
+ " P = np.random.normal(scale=1./K, size=(num_users, K))\n",
+ " Q = np.random.normal(scale=1./K, size=(num_items, K))\n",
+ "\n",
+ " non_zeros = [(i, j, R[i, j]) for i in range(num_users)\n",
+ " for j in range(num_items) if R[i, j] > 0]\n",
+ "\n",
+ " for step in range(steps):\n",
+ " for i, j, r in non_zeros:\n",
+ " eij = r - np.dot(P[i, :], Q[j, :].T)\n",
+ "\n",
+ " P[i, :] = P[i, :] + learning_rate * (eij * Q[j, :] - r_lambda * P[i, :])\n",
+ " Q[j, :] = Q[j, :] + learning_rate * (eij * P[i, :] - r_lambda * Q[j, :])\n",
+ "\n",
+ " rmse = get_rmse(R, P, Q, non_zeros)\n",
+ "\n",
+ " if (step % 10) == 0:\n",
+ " print(\"### iteration step :\", step, \" rmse :\", rmse)\n",
+ "\n",
+ " return P, Q\n"
+ ],
+ "metadata": {
+ "id": "d8SDa0Fikenp"
+ },
+ "execution_count": 16,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "import pandas as pd\n",
+ "import numpy as np\n",
+ "\n",
+ "movies = pd.read_csv('/content/drive/MyDrive/Euron/ml-latest-small/movies.csv')\n",
+ "ratings = pd.read_csv('/content/drive/MyDrive/Euron/ml-latest-small/ratings.csv')\n",
+ "\n",
+ "ratings = ratings[['userId', 'movieId', 'rating']]\n",
+ "ratings_matrix = ratings.pivot_table('rating', index='userId', columns='movieId')\n",
+ "\n",
+ "rating_movies = pd.merge(ratings, movies, on='movieId')\n"
+ ],
+ "metadata": {
+ "id": "5Anr_2-Lkell"
+ },
+ "execution_count": 18,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def get_rmse(R, P, Q, non_zeros):\n",
+ " error = 0\n",
+ " for i, j, r in non_zeros:\n",
+ " error += pow(r - np.dot(P[i, :], Q[j, :].T), 2)\n",
+ " return np.sqrt(error / len(non_zeros))\n"
+ ],
+ "metadata": {
+ "id": "NgOtxuOdlhLn"
+ },
+ "execution_count": 20,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "ratings_matrix = rating_movies.pivot_table(\n",
+ " 'rating',\n",
+ " index='userId',\n",
+ " columns='title'\n",
+ ")\n",
+ "\n",
+ "P, Q = matrix_factorization(\n",
+ " ratings_matrix.values,\n",
+ " K=50,\n",
+ " steps=200,\n",
+ " learning_rate=0.01,\n",
+ " r_lambda=0.01\n",
+ ")\n",
+ "\n",
+ "pred_matrix = np.dot(P, Q.T)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "q7UqxMhFkejb",
+ "outputId": "43500f89-1445-45b4-e1c9-f623fbf207fa"
+ },
+ "execution_count": 21,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "### iteration step : 0 rmse : 2.9023619751337115\n",
+ "### iteration step : 10 rmse : 0.7335768591017939\n",
+ "### iteration step : 20 rmse : 0.5115539026853438\n",
+ "### iteration step : 30 rmse : 0.37261628282537734\n",
+ "### iteration step : 40 rmse : 0.29608182991810145\n",
+ "### iteration step : 50 rmse : 0.2520353192341621\n",
+ "### iteration step : 60 rmse : 0.22487503275269882\n",
+ "### iteration step : 70 rmse : 0.20685455302331512\n",
+ "### iteration step : 80 rmse : 0.19413418783028674\n",
+ "### iteration step : 90 rmse : 0.1847008200272031\n",
+ "### iteration step : 100 rmse : 0.17742927527209082\n",
+ "### iteration step : 110 rmse : 0.17165226964707506\n",
+ "### iteration step : 120 rmse : 0.16695181946871496\n",
+ "### iteration step : 130 rmse : 0.16305292191997453\n",
+ "### iteration step : 140 rmse : 0.159766919296796\n",
+ "### iteration step : 150 rmse : 0.15695986999457337\n",
+ "### iteration step : 160 rmse : 0.15453398186715442\n",
+ "### iteration step : 170 rmse : 0.1524161855107769\n",
+ "### iteration step : 180 rmse : 0.1505508073962834\n",
+ "### iteration step : 190 rmse : 0.14889470913232075\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "ratings_pred_matrix = pd.DataFrame(\n",
+ " data=pred_matrix,\n",
+ " index=ratings_matrix.index,\n",
+ " columns=ratings_matrix.columns\n",
+ ")\n",
+ "\n",
+ "ratings_pred_matrix.head(3)\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 346
+ },
+ "id": "SsFlcOixks0C",
+ "outputId": "9ebb4a06-dec6-4e1d-8afb-e1948b66df20"
+ },
+ "execution_count": 22,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "title '71 (2014) 'Hellboy': The Seeds of Creation (2004) \\\n",
+ "userId \n",
+ "1 3.055084 4.092018 \n",
+ "2 3.170119 3.657992 \n",
+ "3 2.307073 1.658853 \n",
+ "\n",
+ "title 'Round Midnight (1986) 'Salem's Lot (2004) \\\n",
+ "userId \n",
+ "1 3.564130 4.502167 \n",
+ "2 3.308707 4.166521 \n",
+ "3 1.443538 2.208859 \n",
+ "\n",
+ "title 'Til There Was You (1997) 'Tis the Season for Love (2015) \\\n",
+ "userId \n",
+ "1 3.981215 1.271694 \n",
+ "2 4.311890 1.275469 \n",
+ "3 2.229486 0.780760 \n",
+ "\n",
+ "title 'burbs, The (1989) 'night Mother (1986) (500) Days of Summer (2009) \\\n",
+ "userId \n",
+ "1 3.603274 2.333266 5.091749 \n",
+ "2 4.237972 1.900366 3.392859 \n",
+ "3 1.997043 0.924908 2.970700 \n",
+ "\n",
+ "title *batteries not included (1987) ... Zulu (2013) [REC] (2007) \\\n",
+ "userId ... \n",
+ "1 3.972454 ... 1.402608 4.208382 \n",
+ "2 3.647421 ... 0.973811 3.528264 \n",
+ "3 2.551446 ... 0.520354 1.709494 \n",
+ "\n",
+ "title [REC]² (2009) [REC]³ 3 Génesis (2012) \\\n",
+ "userId \n",
+ "1 3.705957 2.720514 \n",
+ "2 3.361532 2.672535 \n",
+ "3 2.281596 1.782833 \n",
+ "\n",
+ "title anohana: The Flower We Saw That Day - The Movie (2013) \\\n",
+ "userId \n",
+ "1 2.787331 \n",
+ "2 2.404456 \n",
+ "3 1.635173 \n",
+ "\n",
+ "title eXistenZ (1999) xXx (2002) xXx: State of the Union (2005) \\\n",
+ "userId \n",
+ "1 3.475076 3.253458 2.161087 \n",
+ "2 4.232789 2.911602 1.634576 \n",
+ "3 1.323276 2.887580 1.042618 \n",
+ "\n",
+ "title ¡Three Amigos! (1986) À nous la liberté (Freedom for Us) (1931) \n",
+ "userId \n",
+ "1 4.010495 0.859474 \n",
+ "2 4.135735 0.725684 \n",
+ "3 2.293890 0.396941 \n",
+ "\n",
+ "[3 rows x 9719 columns]"
+ ],
+ "text/html": [
+ "\n",
+ " \n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | title | \n",
+ " '71 (2014) | \n",
+ " 'Hellboy': The Seeds of Creation (2004) | \n",
+ " 'Round Midnight (1986) | \n",
+ " 'Salem's Lot (2004) | \n",
+ " 'Til There Was You (1997) | \n",
+ " 'Tis the Season for Love (2015) | \n",
+ " 'burbs, The (1989) | \n",
+ " 'night Mother (1986) | \n",
+ " (500) Days of Summer (2009) | \n",
+ " *batteries not included (1987) | \n",
+ " ... | \n",
+ " Zulu (2013) | \n",
+ " [REC] (2007) | \n",
+ " [REC]² (2009) | \n",
+ " [REC]³ 3 Génesis (2012) | \n",
+ " anohana: The Flower We Saw That Day - The Movie (2013) | \n",
+ " eXistenZ (1999) | \n",
+ " xXx (2002) | \n",
+ " xXx: State of the Union (2005) | \n",
+ " ¡Three Amigos! (1986) | \n",
+ " À nous la liberté (Freedom for Us) (1931) | \n",
+ "
\n",
+ " \n",
+ " | userId | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 1 | \n",
+ " 3.055084 | \n",
+ " 4.092018 | \n",
+ " 3.564130 | \n",
+ " 4.502167 | \n",
+ " 3.981215 | \n",
+ " 1.271694 | \n",
+ " 3.603274 | \n",
+ " 2.333266 | \n",
+ " 5.091749 | \n",
+ " 3.972454 | \n",
+ " ... | \n",
+ " 1.402608 | \n",
+ " 4.208382 | \n",
+ " 3.705957 | \n",
+ " 2.720514 | \n",
+ " 2.787331 | \n",
+ " 3.475076 | \n",
+ " 3.253458 | \n",
+ " 2.161087 | \n",
+ " 4.010495 | \n",
+ " 0.859474 | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " 3.170119 | \n",
+ " 3.657992 | \n",
+ " 3.308707 | \n",
+ " 4.166521 | \n",
+ " 4.311890 | \n",
+ " 1.275469 | \n",
+ " 4.237972 | \n",
+ " 1.900366 | \n",
+ " 3.392859 | \n",
+ " 3.647421 | \n",
+ " ... | \n",
+ " 0.973811 | \n",
+ " 3.528264 | \n",
+ " 3.361532 | \n",
+ " 2.672535 | \n",
+ " 2.404456 | \n",
+ " 4.232789 | \n",
+ " 2.911602 | \n",
+ " 1.634576 | \n",
+ " 4.135735 | \n",
+ " 0.725684 | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " 2.307073 | \n",
+ " 1.658853 | \n",
+ " 1.443538 | \n",
+ " 2.208859 | \n",
+ " 2.229486 | \n",
+ " 0.780760 | \n",
+ " 1.997043 | \n",
+ " 0.924908 | \n",
+ " 2.970700 | \n",
+ " 2.551446 | \n",
+ " ... | \n",
+ " 0.520354 | \n",
+ " 1.709494 | \n",
+ " 2.281596 | \n",
+ " 1.782833 | \n",
+ " 1.635173 | \n",
+ " 1.323276 | \n",
+ " 2.887580 | \n",
+ " 1.042618 | \n",
+ " 2.293890 | \n",
+ " 0.396941 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
3 rows × 9719 columns
\n",
+ "
\n",
+ "
\n",
+ "
\n"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "dataframe",
+ "variable_name": "ratings_pred_matrix"
+ }
+ },
+ "metadata": {},
+ "execution_count": 22
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "unseen_list = get_unseen_movies(ratings_matrix, 9)\n",
+ "\n",
+ "recom_movies = recom_movie_by_userid(\n",
+ " ratings_pred_matrix,\n",
+ " 9,\n",
+ " unseen_list,\n",
+ " top_n=10\n",
+ ")\n",
+ "\n",
+ "recom_movies = pd.DataFrame(\n",
+ " data=recom_movies.values,\n",
+ " index=recom_movies.index,\n",
+ " columns=['pred_score']\n",
+ ")\n",
+ "\n",
+ "recom_movies\n"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 394
+ },
+ "id": "OFOPASYEktv5",
+ "outputId": "db159045-42b6-4130-f5cd-e53871a48942"
+ },
+ "execution_count": 23,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ " pred_score\n",
+ "title \n",
+ "Rear Window (1954) 5.704612\n",
+ "South Park: Bigger, Longer and Uncut (1999) 5.451100\n",
+ "Rounders (1998) 5.298393\n",
+ "Blade Runner (1982) 5.244951\n",
+ "Roger & Me (1989) 5.191962\n",
+ "Gattaca (1997) 5.183179\n",
+ "Ben-Hur (1959) 5.130463\n",
+ "Rosencrantz and Guildenstern Are Dead (1990) 5.087375\n",
+ "Big Lebowski, The (1998) 5.038690\n",
+ "Star Wars: Episode V - The Empire Strikes Back ... 4.989601"
+ ],
+ "text/html": [
+ "\n",
+ " \n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " pred_score | \n",
+ "
\n",
+ " \n",
+ " | title | \n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | Rear Window (1954) | \n",
+ " 5.704612 | \n",
+ "
\n",
+ " \n",
+ " | South Park: Bigger, Longer and Uncut (1999) | \n",
+ " 5.451100 | \n",
+ "
\n",
+ " \n",
+ " | Rounders (1998) | \n",
+ " 5.298393 | \n",
+ "
\n",
+ " \n",
+ " | Blade Runner (1982) | \n",
+ " 5.244951 | \n",
+ "
\n",
+ " \n",
+ " | Roger & Me (1989) | \n",
+ " 5.191962 | \n",
+ "
\n",
+ " \n",
+ " | Gattaca (1997) | \n",
+ " 5.183179 | \n",
+ "
\n",
+ " \n",
+ " | Ben-Hur (1959) | \n",
+ " 5.130463 | \n",
+ "
\n",
+ " \n",
+ " | Rosencrantz and Guildenstern Are Dead (1990) | \n",
+ " 5.087375 | \n",
+ "
\n",
+ " \n",
+ " | Big Lebowski, The (1998) | \n",
+ " 5.038690 | \n",
+ "
\n",
+ " \n",
+ " | Star Wars: Episode V - The Empire Strikes Back (1980) | \n",
+ " 4.989601 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ "
\n",
+ "
\n"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "dataframe",
+ "variable_name": "recom_movies",
+ "summary": "{\n \"name\": \"recom_movies\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"Big Lebowski, The (1998)\",\n \"South Park: Bigger, Longer and Uncut (1999)\",\n \"Gattaca (1997)\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"pred_score\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.21272885538651393,\n \"min\": 4.989601238872484,\n \"max\": 5.704612469838172,\n \"num_unique_values\": 10,\n \"samples\": [\n 5.0386897288205725,\n 5.451100205772531,\n 5.183178550884765\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
+ }
+ },
+ "metadata": {},
+ "execution_count": 23
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file