class_docstr: Implements the MovieLens Bandit environment.
  
  This environment implements the MovieLens 100K dataset, available at:
  https://www.kaggle.com/prajitdatta/movielens-100k-dataset
  
  This dataset contains 100K ratings from 943 users on 1682 items.
  This csv list of:
  user id | item id | rating | timestamp.
  This environment computes a low-rank matrix factorization (using SVD) of the
  data matrix A, such that: A ~= U * V.
  
  The reward of recommending item `j` to user `i` is provided as A_{ij}.
class_docstr_tok: ["Implements","the","MovieLens","Bandit","environment",".","This","environment","implements","the","MovieLens","100K","dataset",",","available","at",":","https",":","\/\/www.kaggle.com\/prajitdatta\/movielens-100k-dataset","This","dataset","contains","100K","ratings","from","943","users","on","1682","items",".","This","csv","list","of",":","user","id","|","item","id","|","rating","|","timestamp",".","This","environment","computes","a","low-rank","matrix","factorization","(","using","SVD",")","of","the","data","matrix","A",",","such","that",":","A","~=","U","*","V.","The","reward","of","recommending","item","`","j","`","to","user","`","i","`","is","provided","as","A_","{","ij","}","."]
docstr: Initializes the MovieLens Bandit environment.
  
  Args:
    data_dir: (string) Directory where the data lies (in text form). rank_k :
      (int) Which rank to use in the matrix factorization.
    batch_size: (int) Number of observations generated per call.
    num_movies: (int) Only the first `num_movies` movies will be used by the
      environment. The rest is cut out from the data.
    csv_delimiter: (string) The delimiter to use in loading the data csv file.
    name: The name of this environment instance.
docstr_tok: ["Initializes","the","MovieLens","Bandit","environment",".","Args",":","data_dir",":","(","string",")","Directory","where","the","data","lies","(","in","text","form",")",".","rank_k",":","(","int",")","Which","rank","to","use","in","the","matrix","factorization",".","batch_size",":","(","int",")","Number","of","observations","generated","per","call",".","num_movies",":","(","int",")","Only","the","first","`","num_movies","`","movies","will","be","used","by","the","environment",".","The","rest","is","cut","out","from","the","data",".","csv_delimiter",":","(","string",")","The","delimiter","to","use","in","loading","the","data","csv","file",".","name",":","The","name","of","this","environment","instance","."]
code: def __init__(
      self,
      data_dir: Text,
      rank_k: int,
      batch_size: int = 1,
      num_movies: int = 20,
      csv_delimiter: Text = ",",
      name: Optional[Text] = "movielens",
  ):
      """Initializes the MovieLens Bandit environment.
  
      Args:
        data_dir: (string) Directory where the data lies (in text form). rank_k :
          (int) Which rank to use in the matrix factorization.
        batch_size: (int) Number of observations generated per call.
        num_movies: (int) Only the first `num_movies` movies will be used by the
          environment. The rest is cut out from the data.
        csv_delimiter: (string) The delimiter to use in loading the data csv file.
        name: The name of this environment instance.
      """
      self._num_actions = num_movies
      self._batch_size = batch_size
      self._context_dim = rank_k
  
      # Compute the matrix factorization.
      self._data_matrix = dataset_utilities.load_movielens_data(
          data_dir, delimiter=csv_delimiter
      )
      # Keep only the first items.
      self._data_matrix = self._data_matrix[:, :num_movies]
      # Filter the users with no iterm rated.
      nonzero_users = list(
          np.nonzero(np.sum(self._data_matrix, axis=1) > 0.0)[0]
      )
      self._data_matrix = self._data_matrix[nonzero_users, :]
      self._effective_num_users = len(nonzero_users)
  
      # Compute the SVD.
      u, s, vh = np.linalg.svd(self._data_matrix, full_matrices=False)
  
      # Keep only the largest singular values.
      self._u_hat = u[:, :rank_k] * np.sqrt(s[:rank_k])
      self._v_hat = np.transpose(
          np.transpose(vh[:rank_k, :]) * np.sqrt(s[:rank_k])
      )
      self._approx_ratings_matrix = np.matmul(self._u_hat, self._v_hat)
  
      self._current_users = np.zeros(batch_size)
      self._previous_users = np.zeros(batch_size)
  
      self._action_spec = array_spec.BoundedArraySpec(
          shape=(),
          dtype=np.int32,
          minimum=0,
          maximum=self._num_actions - 1,
          name="action",
      )
      observation_spec = array_spec.ArraySpec(
          shape=(self._context_dim,),
          dtype=np.float64,
          name="observation",
      )
      self._time_step_spec = ts.time_step_spec(observation_spec)
      self._observation = np.zeros(
          (self._batch_size, self._context_dim)
      )
  
      self._optimal_action_table = np.argmax(
          self._approx_ratings_matrix, axis=1
      )
      self._optimal_reward_table = np.max(
          self._approx_ratings_matrix, axis=1
      )
  
      super(MovieLensPyEnvironment, self).__init__(
          observation_spec, self._action_spec, name=name
      )
  
file_path: agents/tf_agents/bandits/environments/movielens_py_environment.py
imports: from __future__ import absolute_import
  import random
  from typing import Optional, Text
  import gin
  import numpy
  from tf_agents.bandits.environments import bandit_py_environment
  from tf_agents.bandits.environments import dataset_utilities
  from tf_agents.specs import array_spec
  from tf_agents.trajectories import time_step
label_desc: Use image node_id 1 to create a new MovieLensPyEnvironment object from inherited base classes: bandit_py_environment with example: obj = MovieLensPyEnvironment(data_dir, rank_k, batch_size, num_movies, csv_delimiter, name)