IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / modules / ml / src / main / java / org / apache / ignite / ml / regressions / linear / FeatureExtractorWrapper.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.regressions.linear;
19
20 import java.util.Arrays;
21 import org.apache.ignite.ml.math.Vector;
22 import org.apache.ignite.ml.math.VectorUtils;
23 import org.apache.ignite.ml.math.functions.IgniteBiFunction;
24
25 /**
26 * Feature extractor wrapper that adds additional column filled by 1.
27 *
28 * @param <K> Type of a key in {@code upstream} data.
29 * @param <V> Type of a value in {@code upstream} data.
30 */
31 public class FeatureExtractorWrapper<K, V> implements IgniteBiFunction<K, V, Vector> {
32 /** */
33 private static final long serialVersionUID = -2686524650955735635L;
34
35 /** Underlying feature extractor. */
36 private final IgniteBiFunction<K, V, Vector> featureExtractor;
37
38 /**
39 * Constructs a new instance of feature extractor wrapper.
40 *
41 * @param featureExtractor Underlying feature extractor.
42 */
43 FeatureExtractorWrapper(IgniteBiFunction<K, V, Vector> featureExtractor) {
44 this.featureExtractor = featureExtractor;
45 }
46
47 /** {@inheritDoc} */
48 @Override public Vector apply(K k, V v) {
49 double[] featureRow = featureExtractor.apply(k, v).asArray();
50 double[] row = Arrays.copyOf(featureRow, featureRow.length + 1);
51
52 row[featureRow.length] = 1.0;
53
54 return VectorUtils.of(row);
55 }
56 }