{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Recommender.ipynb",
      "provenance": [],
      "authorship_tag": "ABX9TyPBDgD1qFu0/EIsajARFSqp",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/josharsh/100LinesOfCode/blob/master/Recommender.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TerMUBJrbecn",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 374
        },
        "outputId": "bf3ca8d2-5c5b-492c-e29b-bc3a58f9a70a"
      },
      "source": [
        "!pip install tensorflow==1.15.0"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Requirement already satisfied: tensorflow==1.15.0 in /usr/local/lib/python3.6/dist-packages (1.15.0)\n",
            "Requirement already satisfied: keras-preprocessing>=1.0.5 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (1.1.0)\n",
            "Requirement already satisfied: tensorboard<1.16.0,>=1.15.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (1.15.0)\n",
            "Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (1.1.0)\n",
            "Requirement already satisfied: numpy<2.0,>=1.16.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (1.17.5)\n",
            "Requirement already satisfied: keras-applications>=1.0.8 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (1.0.8)\n",
            "Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (3.1.0)\n",
            "Requirement already satisfied: grpcio>=1.8.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (1.27.1)\n",
            "Requirement already satisfied: google-pasta>=0.1.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (0.1.8)\n",
            "Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (1.12.0)\n",
            "Requirement already satisfied: astor>=0.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (0.8.1)\n",
            "Requirement already satisfied: tensorflow-estimator==1.15.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (1.15.1)\n",
            "Requirement already satisfied: gast==0.2.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (0.2.2)\n",
            "Requirement already satisfied: wheel>=0.26 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (0.34.2)\n",
            "Requirement already satisfied: protobuf>=3.6.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (3.10.0)\n",
            "Requirement already satisfied: absl-py>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (0.9.0)\n",
            "Requirement already satisfied: wrapt>=1.11.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow==1.15.0) (1.11.2)\n",
            "Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.6/dist-packages (from tensorboard<1.16.0,>=1.15.0->tensorflow==1.15.0) (45.1.0)\n",
            "Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.6/dist-packages (from tensorboard<1.16.0,>=1.15.0->tensorflow==1.15.0) (1.0.0)\n",
            "Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.6/dist-packages (from tensorboard<1.16.0,>=1.15.0->tensorflow==1.15.0) (3.2.1)\n",
            "Requirement already satisfied: h5py in /usr/local/lib/python3.6/dist-packages (from keras-applications>=1.0.8->tensorflow==1.15.0) (2.8.0)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QFEYmrH3fNib",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import numpy as np\n",
        "import tensorflow as tf\n",
        "tf.enable_eager_execution()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cTFl1X-IfY-n",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "\n",
        "users = ['Ryan', 'Danielle',  'Vijay', 'Chris']\n",
        "movies = ['Star Wars', 'The Dark Knight', 'Shrek', 'The Incredibles', 'Bleu', 'Memento']\n",
        "features = ['Action', 'Sci-Fi', 'Comedy', 'Cartoon', 'Drama']\n",
        "num_users = len(users)\n",
        "num_movies = len(movies)\n",
        "num_feats = len(features)\n",
        "num_recommendations = 2"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bhNg2L7Xfstj",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "users_movies = tf.constant([\n",
        "                [4,  6,  8,  0, 0, 0],\n",
        "                [0,  0, 10,  0, 8, 3],\n",
        "                [0,  6,  0,  0, 3, 7],\n",
        "                [10, 9,  0,  5, 0, 2]],dtype=tf.float32)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zQ5SX4Epfu21",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "movies_feats = tf.constant([\n",
        "                [1, 1, 0, 0, 1],\n",
        "                [1, 1, 0, 0, 0],\n",
        "                [0, 0, 1, 1, 0],\n",
        "                [1, 0, 1, 1, 0],\n",
        "                [0, 0, 0, 0, 1],\n",
        "                [1, 0, 0, 0, 1]],dtype=tf.float32)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qUrY_jY-fxWa",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "users_feats=tf.matmul(users_movies,movies_feats)\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wvUsDkAqf13R",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 119
        },
        "outputId": "c89ce673-1333-4497-8756-78e1923e1883"
      },
      "source": [
        "users_feats = users_feats/tf.reduce_sum(users_feats,axis=1,keepdims=True)\n",
        "users_feats"
      ],
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tf.Tensor: id=114, shape=(4, 5), dtype=float32, numpy=\n",
              "array([[0.25      , 0.25      , 0.2       , 0.2       , 0.1       ],\n",
              "       [0.0882353 , 0.        , 0.29411766, 0.29411766, 0.32352942],\n",
              "       [0.44827586, 0.20689656, 0.        , 0.        , 0.3448276 ],\n",
              "       [0.3880597 , 0.2835821 , 0.07462686, 0.07462686, 0.17910448]],\n",
              "      dtype=float32)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Cnhz7ZHlgTG6",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "5b36e417-1d4a-4258-b43d-405502d9a738"
      },
      "source": [
        "top_users_features = tf.nn.top_k(users_feats, num_feats)[1]\n",
        "top_users_features"
      ],
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tf.Tensor: id=117, shape=(4, 5), dtype=int32, numpy=\n",
              "array([[0, 1, 2, 3, 4],\n",
              "       [4, 2, 3, 0, 1],\n",
              "       [0, 4, 1, 2, 3],\n",
              "       [0, 1, 4, 2, 3]], dtype=int32)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bauCLVzEgYI8",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "d57cb77f-447c-43de-8fd2-bb0cf2bed75f"
      },
      "source": [
        "for i in range(num_users):\n",
        "    feature_names = [features[index] for index in top_users_features[i]]\n",
        "    print('{}: {}'.format(users[i],feature_names))"
      ],
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Ryan: ['Action', 'Sci-Fi', 'Comedy', 'Cartoon', 'Drama']\n",
            "Danielle: ['Drama', 'Comedy', 'Cartoon', 'Action', 'Sci-Fi']\n",
            "Vijay: ['Action', 'Drama', 'Sci-Fi', 'Comedy', 'Cartoon']\n",
            "Chris: ['Action', 'Sci-Fi', 'Drama', 'Comedy', 'Cartoon']\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VIxti5iqgaUG",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "movies_feats_trans=tf.transpose(movies_feats)\n",
        "users_feats_trans=tf.transpose(users_feats)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HNtdTpT_iAbx",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        },
        "outputId": "da8767be-c422-49a5-a487-fd382b26381e"
      },
      "source": [
        "users_ratings =  tf.matmul(movies_feats,users_feats_trans)\n",
        "users_ratings"
      ],
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tf.Tensor: id=220, shape=(6, 4), dtype=float32, numpy=\n",
              "array([[0.6       , 0.4117647 , 1.        , 0.8507463 ],\n",
              "       [0.5       , 0.0882353 , 0.6551724 , 0.6716418 ],\n",
              "       [0.4       , 0.5882353 , 0.        , 0.14925373],\n",
              "       [0.65      , 0.67647064, 0.44827586, 0.53731346],\n",
              "       [0.1       , 0.32352942, 0.3448276 , 0.17910448],\n",
              "       [0.35      , 0.4117647 , 0.79310346, 0.5671642 ]], dtype=float32)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "b-_bbn-OjZ2y",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "users_movies\n",
        "users_ratings\n",
        "user_r=tf.transpose(users_ratings);"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8XfJRNhNiQMo",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 170
        },
        "outputId": "e865d53d-f258-4089-ac6e-6f263551d07a"
      },
      "source": [
        "users_ratings_new = tf.where(tf.equal(users_movies, tf.zeros_like(users_movies)),\n",
        "                                  user_r,\n",
        "                                  tf.zeros_like(tf.cast(users_movies, tf.float32)))\n",
        "users_ratings_new"
      ],
      "execution_count": 30,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tf.Tensor: id=233, shape=(4, 6), dtype=float32, numpy=\n",
              "array([[0.        , 0.        , 0.        , 0.65      , 0.1       ,\n",
              "        0.35      ],\n",
              "       [0.4117647 , 0.0882353 , 0.        , 0.67647064, 0.        ,\n",
              "        0.        ],\n",
              "       [1.        , 0.        , 0.        , 0.44827586, 0.        ,\n",
              "        0.        ],\n",
              "       [0.        , 0.        , 0.14925373, 0.        , 0.17910448,\n",
              "        0.        ]], dtype=float32)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 30
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LqYdAtRNjG62",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "3d10807f-09d6-4afe-97a8-d21f4d72480c"
      },
      "source": [
        "top_movies = tf.nn.top_k(users_ratings_new, num_recommendations)[1]\n",
        "top_movies"
      ],
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tf.Tensor: id=236, shape=(4, 2), dtype=int32, numpy=\n",
              "array([[3, 5],\n",
              "       [3, 0],\n",
              "       [0, 3],\n",
              "       [4, 2]], dtype=int32)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 31
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qnnUfPOBjsu1",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "82890b08-7c6a-45c3-ead2-15d4870b29de"
      },
      "source": [
        "\n",
        "for i in range(num_users):\n",
        "    movie_names = [movies[index] for index in top_movies[i]]\n",
        "    print('{}: {}'.format(users[i],movie_names))"
      ],
      "execution_count": 32,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Ryan: ['The Incredibles', 'Memento']\n",
            "Danielle: ['The Incredibles', 'Star Wars']\n",
            "Vijay: ['Star Wars', 'The Incredibles']\n",
            "Chris: ['Bleu', 'Shrek']\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xetXtjGSjuKs",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}
