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