code: def calculate_similar_movies(
      output_filename, model_name="als", min_rating=4.0, variant="20m"
  ):
      # read in the input data file
      start = time.time()
      titles, ratings = get_movielens(variant)
  
      # remove things < min_rating, and convert to implicit dataset
      # by considering ratings as a binary preference only
      ratings.data[ratings.data < min_rating] = 0
      ratings.eliminate_zeros()
      ratings.data = np.ones(len(ratings.data))
  
      log.info("read data file in %s", time.time() - start)
  
      # generate a recommender model based off the input params
      if model_name == "als":
          model = AlternatingLeastSquares()
  
          # lets weight these models by bm25weight.
          log.debug("weighting matrix by bm25_weight")
          ratings = (bm25_weight(ratings, B=0.9) * 5).tocsr()
  
      elif model_name == "bpr":
          model = BayesianPersonalizedRanking()
  
      elif model_name == "lmf":
          model = LogisticMatrixFactorization()
  
      elif model_name == "tfidf":
          model = TFIDFRecommender()
  
      elif model_name == "cosine":
          model = CosineRecommender()
  
      elif model_name == "bm25":
          model = BM25Recommender(B=0.2)
  
      else:
          raise NotImplementedError(
              f"model {model_name} isn't implemented for this example"
          )
  
      user_ratings = ratings.T.tocsr()
  
      # train the model
      log.debug("training model %s", model_name)
      start = time.time()
      model.fit(user_ratings)
      log.debug(
          "trained model '%s' in %s", model_name, time.time() - start
      )
      log.debug("calculating top movies")
  
      user_count = np.ediff1d(ratings.indptr)
      to_generate = sorted(
          np.arange(len(titles)), key=lambda x: -user_count[x]
      )
  
      log.debug("calculating similar movies")
      with tqdm.tqdm(total=len(to_generate)) as progress:
          with codecs.open(output_filename, "w", "utf8") as o:
              batch_size = 1000
              for startidx in range(0, len(to_generate), batch_size):
                  batch = to_generate[startidx : startidx + batch_size]
                  ids, scores = model.similar_items(batch, 11)
                  for i, movieid in enumerate(batch):
                      # if this movie has no ratings, skip over (for instance 'Graffiti Bridge' has
                      # no ratings > 4 meaning we've filtered out all data for it.
                      if (
                          ratings.indptr[movieid]
                          != ratings.indptr[movieid + 1]
                      ):
                          title = titles[movieid]
                          for other, score in zip(ids[i], scores[i]):
                              o.write(
                                  f"{title}\t{titles[other]}\t{score}\n"
                              )
                  progress.update(len(batch))
  
file_path: implicit/examples/movielens.py
imports: from __future__ import print_function
  import argparse
  import codecs
  import logging
  import time
  import numpy
  import tqdm
  from implicit.als import AlternatingLeastSquares
  from implicit.bpr import BayesianPersonalizedRanking
  from implicit.datasets.movielens import get_movielens
  from implicit.lmf import LogisticMatrixFactorization
  from implicit.nearest_neighbours import BM25Recommender, CosineRecommender, TFIDFRecommender, bm25_weight
label_desc: Use image node_id 1 for calling a global function with example usage: calculate_similar_movies(output_filename, model_name, min_rating, variant) without return types