IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / modules / ml / src / test / java / org / apache / ignite / ml / tree / performance / DecisionTreeMNISTTest.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.ml.tree.performance;
19
20 import java.io.IOException;
21 import java.util.HashMap;
22 import java.util.Map;
23 import org.apache.ignite.ml.math.VectorUtils;
24 import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector;
25 import org.apache.ignite.ml.nn.performance.MnistMLPTestUtil;
26 import org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer;
27 import org.apache.ignite.ml.tree.DecisionTreeNode;
28 import org.apache.ignite.ml.tree.impurity.util.SimpleStepFunctionCompressor;
29 import org.apache.ignite.ml.util.MnistUtils;
30 import org.junit.Test;
31
32 import static junit.framework.TestCase.assertTrue;
33
34 /**
35 * Tests {@link DecisionTreeClassificationTrainer} on the MNIST dataset using locally stored data. For manual run.
36 */
37 public class DecisionTreeMNISTTest {
38 /** Tests on the MNIST dataset. For manual run. */
39 @Test
40 public void testMNIST() throws IOException {
41 Map<Integer, MnistUtils.MnistLabeledImage> trainingSet = new HashMap<>();
42
43 int i = 0;
44 for (MnistUtils.MnistLabeledImage e : MnistMLPTestUtil.loadTrainingSet(60_000))
45 trainingSet.put(i++, e);
46
47
48 DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(
49 8,
50 0,
51 new SimpleStepFunctionCompressor<>());
52
53 DecisionTreeNode mdl = trainer.fit(
54 trainingSet,
55 10,
56 (k, v) -> VectorUtils.of(v.getPixels()),
57 (k, v) -> (double) v.getLabel()
58 );
59
60 int correctAnswers = 0;
61 int incorrectAnswers = 0;
62
63 for (MnistUtils.MnistLabeledImage e : MnistMLPTestUtil.loadTestSet(10_000)) {
64 double res = mdl.apply(new DenseLocalOnHeapVector(e.getPixels()));
65
66 if (res == e.getLabel())
67 correctAnswers++;
68 else
69 incorrectAnswers++;
70 }
71
72 double accuracy = 1.0 * correctAnswers / (correctAnswers + incorrectAnswers);
73
74 assertTrue(accuracy > 0.8);
75 }
76 }