apache-ignite
70 строк · 2.9 Кб
1// Licensed to the Apache Software Foundation (ASF) under one or more
2// contributor license agreements. See the NOTICE file distributed with
3// this work for additional information regarding copyright ownership.
4// The ASF licenses this file to You under the Apache License, Version 2.0
5// (the "License"); you may not use this file except in compliance with
6// the License. You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15= Recommendation Systems
16
17CAUTION: This is an experimental API that could be changed in the next releases.
18
19Collaborative 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.
20
21The 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.
22
23Example of recommendation system based on https://grouplens.org/datasets/movielens[MovieLens dataset].
24
25
26
27[source, java]
28----
29IgniteCache<Integer, RatingPoint> movielensCache = loadMovieLensDataset(ignite, 10_000);
30
31RecommendationTrainer trainer = new RecommendationTrainer()
32.withMaxIterations(-1)
33.withMinMdlImprovement(10)
34.withBatchSize(10)
35.withLearningRate(10)
36.withLearningEnvironmentBuilder(envBuilder)
37.withTrainerEnvironment(envBuilder.buildForTrainer());
38
39RecommendationModel<Integer, Integer> mdl = trainer.fit(new CacheBasedDatasetBuilder<>(ignite, movielensCache));
40----
41
42CAUTION: The Evaluator is not support the recommendation systems yet.
43
44The next example demonstrates how to calculate metrics over the given cache manually and locally on the client node:
45
46
47[source, java]
48----
49double mean = 0;
50
51try (QueryCursor<Cache.Entry<Integer, RatingPoint>> cursor = movielensCache.query(new ScanQuery<>())) {
52for (Cache.Entry<Integer, RatingPoint> e : cursor) {
53ObjectSubjectRatingTriplet<Integer, Integer> triplet = e.getValue();
54mean += triplet.getRating();
55}
56mean /= movielensCache.size();
57}
58
59double tss = 0, rss = 0;
60
61try (QueryCursor<Cache.Entry<Integer, RatingPoint>> cursor = movielensCache.query(new ScanQuery<>())) {
62for (Cache.Entry<Integer, RatingPoint> e : cursor) {
63ObjectSubjectRatingTriplet<Integer, Integer> triplet = e.getValue();
64tss += Math.pow(triplet.getRating() - mean, 2);
65rss += Math.pow(triplet.getRating() - mdl.predict(triplet), 2);
66}
67}
68
69double r2 = 1.0 - rss / tss;
70----
71
72