IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / examples / src / main / java / org / apache / ignite / examples / ml / dataset / CacheBasedDatasetExample.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.ignite.examples.ml.dataset;
19
20 import java.util.Arrays;
21 import org.apache.ignite.Ignite;
22 import org.apache.ignite.IgniteCache;
23 import org.apache.ignite.Ignition;
24 import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
25 import org.apache.ignite.configuration.CacheConfiguration;
26 import org.apache.ignite.examples.ml.dataset.model.Person;
27 import org.apache.ignite.ml.dataset.DatasetFactory;
28 import org.apache.ignite.ml.dataset.primitive.SimpleDataset;
29 import org.apache.ignite.ml.math.VectorUtils;
30
31 /**
32 * Example that shows how to create dataset based on an existing Ignite Cache and then use it to calculate {@code mean}
33 * and {@code std} values as well as {@code covariance} and {@code correlation} matrices.
34 */
35 public class CacheBasedDatasetExample {
36 /** Run example. */
37 public static void main(String[] args) throws Exception {
38 try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
39 System.out.println(">>> Cache Based Dataset example started.");
40
41 IgniteCache<Integer, Person> persons = createCache(ignite);
42
43 // Creates a cache based simple dataset containing features and providing standard dataset API.
44 try (SimpleDataset<?> dataset = DatasetFactory.createSimpleDataset(
45 ignite,
46 persons,
47 (k, v) -> VectorUtils.of(v.getAge(), v.getSalary())
48 )) {
49 // Calculation of the mean value. This calculation will be performed in map-reduce manner.
50 double[] mean = dataset.mean();
51 System.out.println("Mean \n\t" + Arrays.toString(mean));
52
53 // Calculation of the standard deviation. This calculation will be performed in map-reduce manner.
54 double[] std = dataset.std();
55 System.out.println("Standard deviation \n\t" + Arrays.toString(std));
56
57 // Calculation of the covariance matrix. This calculation will be performed in map-reduce manner.
58 double[][] cov = dataset.cov();
59 System.out.println("Covariance matrix ");
60 for (double[] row : cov)
61 System.out.println("\t" + Arrays.toString(row));
62
63 // Calculation of the correlation matrix. This calculation will be performed in map-reduce manner.
64 double[][] corr = dataset.corr();
65 System.out.println("Correlation matrix ");
66 for (double[] row : corr)
67 System.out.println("\t" + Arrays.toString(row));
68 }
69
70 System.out.println(">>> Cache Based Dataset example completed.");
71 }
72 }
73
74 /** */
75 private static IgniteCache<Integer, Person> createCache(Ignite ignite) {
76 CacheConfiguration<Integer, Person> cacheConfiguration = new CacheConfiguration<>();
77
78 cacheConfiguration.setName("PERSONS");
79 cacheConfiguration.setAffinity(new RendezvousAffinityFunction(false, 2));
80
81 IgniteCache<Integer, Person> persons = ignite.createCache(cacheConfiguration);
82
83 persons.put(1, new Person("Mike", 42, 10000));
84 persons.put(2, new Person("John", 32, 64000));
85 persons.put(3, new Person("George", 53, 120000));
86 persons.put(4, new Person("Karl", 24, 70000));
87
88 return persons;
89 }
90 }