October 20th, Q&A session: Get you issues solved and questions answered!

GitHub logo
Edit

Recommendation Systems

Caution
This is an experimental API that could be changed in the next releases.

Collaborative filtering is commonly used for recommender systems. These techniques aim to fill in the missing entries of a user-item association matrix. Apache Ignite ML currently supports model-based collaborative filtering, in which users and products are described by a small set of latent factors that can be used to predict missing entries.

The standard approach to matrix factorization-based collaborative filtering treats the entries in the user-item matrix as explicit preferences given by the user to the item, for example, users giving ratings to movies.

Example of recommendation system based on MovieLens dataset.

IgniteCache<Integer, RatingPoint> movielensCache = loadMovieLensDataset(ignite, 10_000);

RecommendationTrainer trainer = new RecommendationTrainer()
  .withMaxIterations(-1)
  .withMinMdlImprovement(10)
  .withBatchSize(10)
  .withLearningRate(10)
  .withLearningEnvironmentBuilder(envBuilder)
  .withTrainerEnvironment(envBuilder.buildForTrainer());

RecommendationModel<Integer, Integer> mdl = trainer.fit(new CacheBasedDatasetBuilder<>(ignite, movielensCache));
Caution
The Evaluator is not support the recommendation systems yet.

The next example demonstrates how to calculate metrics over the given cache manually and locally on the client node:

double mean = 0;

try (QueryCursor<Cache.Entry<Integer, RatingPoint>> cursor = movielensCache.query(new ScanQuery<>())) {
  for (Cache.Entry<Integer, RatingPoint> e : cursor) {
    ObjectSubjectRatingTriplet<Integer, Integer> triplet = e.getValue();
    mean += triplet.getRating();
  }
  mean /= movielensCache.size();
}

double tss = 0, rss = 0;

try (QueryCursor<Cache.Entry<Integer, RatingPoint>> cursor = movielensCache.query(new ScanQuery<>())) {
  for (Cache.Entry<Integer, RatingPoint> e : cursor) {
    ObjectSubjectRatingTriplet<Integer, Integer> triplet = e.getValue();
    tss += Math.pow(triplet.getRating() - mean, 2);
    rss += Math.pow(triplet.getRating() - mdl.predict(triplet), 2);
  }
}

double r2 = 1.0 - rss / tss;