IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / modules / ml / src / main / java / org / apache / ignite / ml / preprocessing / imputing / ImputerPreprocessor.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.imputing;
19
20 import org.apache.ignite.ml.math.Vector;
21 import org.apache.ignite.ml.math.functions.IgniteBiFunction;
22
23 /**
24 * Preprocessing function that makes imputing.
25 *
26 * @param <K> Type of a key in {@code upstream} data.
27 * @param <V> Type of a value in {@code upstream} data.
28 */
29 public class ImputerPreprocessor<K, V> implements IgniteBiFunction<K, V, Vector> {
30 /** */
31 private static final long serialVersionUID = 6887800576392623469L;
32
33 /** Filling values. */
34 private final Vector imputingValues;
35
36 /** Base preprocessor. */
37 private final IgniteBiFunction<K, V, Vector> basePreprocessor;
38
39 /**
40 * Constructs a new instance of imputing preprocessor.
41 *
42 * @param basePreprocessor Base preprocessor.
43 */
44 public ImputerPreprocessor(Vector imputingValues,
45 IgniteBiFunction<K, V, Vector> basePreprocessor) {
46 this.imputingValues = imputingValues;
47 this.basePreprocessor = basePreprocessor;
48 }
49
50 /**
51 * Applies this preprocessor.
52 *
53 * @param k Key.
54 * @param v Value.
55 * @return Preprocessed row.
56 */
57 @Override public Vector apply(K k, V v) {
58 Vector res = basePreprocessor.apply(k, v);
59
60 assert res.size() == imputingValues.size();
61
62 for (int i = 0; i < res.size(); i++) {
63 if (Double.valueOf(res.get(i)).equals(Double.NaN))
64 res.set(i, imputingValues.get(i));
65 }
66 return res;
67 }
68 }