IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / examples / src / main / java / org / apache / ignite / examples / ml / tutorial / Step_2_Imputing.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.tutorial;
19
20 import java.io.FileNotFoundException;
21 import org.apache.ignite.Ignite;
22 import org.apache.ignite.IgniteCache;
23 import org.apache.ignite.Ignition;
24 import org.apache.ignite.ml.math.Vector;
25 import org.apache.ignite.ml.math.VectorUtils;
26 import org.apache.ignite.ml.math.functions.IgniteBiFunction;
27 import org.apache.ignite.ml.preprocessing.imputing.ImputerTrainer;
28 import org.apache.ignite.ml.selection.scoring.evaluator.Evaluator;
29 import org.apache.ignite.ml.selection.scoring.metric.Accuracy;
30 import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer;
31 import org.apache.ignite.ml.tree.DecisionTreeNode;
32 import org.apache.ignite.thread.IgniteThread;
33
34 /**
35 * Usage of imputer to fill missed data (Double.NaN) values in the chosen columns.
36 */
37 public class Step_2_Imputing {
38 /** Run example. */
39 public static void main(String[] args) throws InterruptedException {
40 try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
41 IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(),
42 Step_2_Imputing.class.getSimpleName(), () -> {
43 try {
44 IgniteCache<Integer, Object[]> dataCache = TitanicUtils.readPassengers(ignite);
45
46 IgniteBiFunction<Integer, Object[], Vector> featureExtractor = (k, v) -> VectorUtils.of((double) v[0], (double) v[5], (double) v[6]);
47
48 IgniteBiFunction<Integer, Object[], Double> lbExtractor = (k, v) -> (double) v[1];
49
50 IgniteBiFunction<Integer, Object[], Vector> imputingPreprocessor = new ImputerTrainer<Integer, Object[]>()
51 .fit(ignite,
52 dataCache,
53 featureExtractor // "pclass", "sibsp", "parch"
54 );
55
56 DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0);
57
58 // Train decision tree model.
59 DecisionTreeNode mdl = trainer.fit(
60 ignite,
61 dataCache,
62 imputingPreprocessor,
63 lbExtractor
64 );
65
66 double accuracy = Evaluator.evaluate(
67 dataCache,
68 mdl,
69 imputingPreprocessor,
70 lbExtractor,
71 new Accuracy<>()
72 );
73
74 System.out.println("\n>>> Accuracy " + accuracy);
75 System.out.println("\n>>> Test Error " + (1 - accuracy));
76 }
77 catch (FileNotFoundException e) {
78 e.printStackTrace();
79 }
80 });
81
82 igniteThread.start();
83 igniteThread.join();
84 }
85 }
86 }