IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / examples / src / main / java / org / apache / ignite / examples / ml / preprocessing / NormalizationExample.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.preprocessing;
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.Vector;
30 import org.apache.ignite.ml.math.VectorUtils;
31 import org.apache.ignite.ml.math.functions.IgniteBiFunction;
32 import org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer;
33
34 /**
35 * Example that shows how to use normalization preprocessor to normalize each vector in the given data.
36 */
37 public class NormalizationExample {
38 /** Run example. */
39 public static void main(String[] args) throws Exception {
40 try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
41 System.out.println(">>> Normalization example started.");
42
43 IgniteCache<Integer, Person> persons = createCache(ignite);
44
45 // Defines first preprocessor that extracts features from an upstream data.
46 IgniteBiFunction<Integer, Person, Vector> featureExtractor = (k, v) -> VectorUtils.of(
47 v.getAge(),
48 v.getSalary()
49 );
50
51 // Defines second preprocessor that normalizes features.
52 IgniteBiFunction<Integer, Person, Vector> preprocessor = new NormalizationTrainer<Integer, Person>()
53 .withP(1)
54 .fit(ignite, persons, featureExtractor);
55
56 // Creates a cache based simple dataset containing features and providing standard dataset API.
57 try (SimpleDataset<?> dataset = DatasetFactory.createSimpleDataset(ignite, persons, preprocessor)) {
58 // Calculation of the mean value. This calculation will be performed in map-reduce manner.
59 double[] mean = dataset.mean();
60 System.out.println("Mean \n\t" + Arrays.toString(mean));
61
62 // Calculation of the standard deviation. This calculation will be performed in map-reduce manner.
63 double[] std = dataset.std();
64 System.out.println("Standard deviation \n\t" + Arrays.toString(std));
65
66 // Calculation of the covariance matrix. This calculation will be performed in map-reduce manner.
67 double[][] cov = dataset.cov();
68 System.out.println("Covariance matrix ");
69 for (double[] row : cov)
70 System.out.println("\t" + Arrays.toString(row));
71
72 // Calculation of the correlation matrix. This calculation will be performed in map-reduce manner.
73 double[][] corr = dataset.corr();
74 System.out.println("Correlation matrix ");
75 for (double[] row : corr)
76 System.out.println("\t" + Arrays.toString(row));
77 }
78
79 System.out.println(">>> Normalization example completed.");
80 }
81 }
82
83 /** */
84 private static IgniteCache<Integer, Person> createCache(Ignite ignite) {
85 CacheConfiguration<Integer, Person> cacheConfiguration = new CacheConfiguration<>();
86
87 cacheConfiguration.setName("PERSONS");
88 cacheConfiguration.setAffinity(new RendezvousAffinityFunction(false, 2));
89
90 IgniteCache<Integer, Person> persons = ignite.createCache(cacheConfiguration);
91
92 persons.put(1, new Person("Mike", 10, 20));
93 persons.put(2, new Person("John", 20, 10));
94 persons.put(3, new Person("George", 30, 0));
95 persons.put(4, new Person("Karl", 25, 15));
96
97 return persons;
98 }
99 }