IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / modules / ml / src / main / java / org / apache / ignite / ml / tree / data / DecisionTreeDataBuilder.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.data;
19
20 import java.io.Serializable;
21 import java.util.Iterator;
22 import org.apache.ignite.ml.dataset.PartitionDataBuilder;
23 import org.apache.ignite.ml.dataset.UpstreamEntry;
24 import org.apache.ignite.ml.math.Vector;
25 import org.apache.ignite.ml.math.functions.IgniteBiFunction;
26
27 /**
28 * A partition {@code data} builder that makes {@link DecisionTreeData}.
29 *
30 * @param <K> Type of a key in <tt>upstream</tt> data.
31 * @param <V> Type of a value in <tt>upstream</tt> data.
32 * @param <C> Type of a partition <tt>context</tt>.
33 */
34 public class DecisionTreeDataBuilder<K, V, C extends Serializable>
35 implements PartitionDataBuilder<K, V, C, DecisionTreeData> {
36 /** */
37 private static final long serialVersionUID = 3678784980215216039L;
38
39 /** Function that extracts features from an {@code upstream} data. */
40 private final IgniteBiFunction<K, V, Vector> featureExtractor;
41
42 /** Function that extracts labels from an {@code upstream} data. */
43 private final IgniteBiFunction<K, V, Double> lbExtractor;
44
45 /**
46 * Constructs a new instance of decision tree data builder.
47 *
48 * @param featureExtractor Function that extracts features from an {@code upstream} data.
49 * @param lbExtractor Function that extracts labels from an {@code upstream} data.
50 */
51 public DecisionTreeDataBuilder(IgniteBiFunction<K, V, Vector> featureExtractor,
52 IgniteBiFunction<K, V, Double> lbExtractor) {
53 this.featureExtractor = featureExtractor;
54 this.lbExtractor = lbExtractor;
55 }
56
57 /** {@inheritDoc} */
58 @Override public DecisionTreeData build(Iterator<UpstreamEntry<K, V>> upstreamData, long upstreamDataSize, C ctx) {
59 double[][] features = new double[Math.toIntExact(upstreamDataSize)][];
60 double[] labels = new double[Math.toIntExact(upstreamDataSize)];
61
62 int ptr = 0;
63 while (upstreamData.hasNext()) {
64 UpstreamEntry<K, V> entry = upstreamData.next();
65
66 features[ptr] = featureExtractor.apply(entry.getKey(), entry.getValue()).asArray();
67
68 labels[ptr] = lbExtractor.apply(entry.getKey(), entry.getValue());
69
70 ptr++;
71 }
72
73 return new DecisionTreeData(features, labels);
74 }
75 }