IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / modules / ml / src / test / java / org / apache / ignite / ml / preprocessing / encoding / StringEncoderPreprocessorTest.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.preprocessing.encoding;
19
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import org.apache.ignite.ml.preprocessing.encoding.stringencoder.StringEncoderPreprocessor;
23 import org.junit.Test;
24
25 import static org.junit.Assert.assertArrayEquals;
26
27 /**
28 * Tests for {@link StringEncoderPreprocessor}.
29 */
30 public class StringEncoderPreprocessorTest {
31 /** Tests {@code apply()} method. */
32 @Test
33 public void testApply() {
34 String[][] data = new String[][]{
35 {"1", "Moscow", "A"},
36 {"2", "Moscow", "B"},
37 {"2", "Moscow", "B"},
38 };
39
40 StringEncoderPreprocessor<Integer, String[]> preprocessor = new StringEncoderPreprocessor<Integer, String[]>(
41 new HashMap[]{new HashMap() {
42 {
43 put("1", 1);
44 put("2", 0);
45 }
46 }, new HashMap() {
47 {
48 put("Moscow", 0);
49 }
50 }, new HashMap() {
51 {
52 put("A", 1);
53 put("B", 0);
54 }
55 }},
56 (k, v) -> v,
57 new HashSet() {
58 {
59 add(0);
60 add(1);
61 add(2);
62 }
63 });
64
65 double[][] postProcessedData = new double[][]{
66 {1.0, 0.0, 1.0},
67 {0.0, 0.0, 0.0},
68 {0.0, 0.0, 0.0},
69 };
70
71 for (int i = 0; i < data.length; i++)
72 assertArrayEquals(postProcessedData[i], preprocessor.apply(i, data[i]).asArray(), 1e-8);
73 }
74 }