IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / modules / ml / src / main / java / org / apache / ignite / ml / dataset / primitive / builder / data / SimpleLabeledDatasetDataBuilder.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.dataset.primitive.builder.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.dataset.primitive.data.SimpleLabeledDatasetData;
25 import org.apache.ignite.ml.math.Vector;
26 import org.apache.ignite.ml.math.functions.IgniteBiFunction;
27
28 /**
29 * A partition {@code data} builder that makes {@link SimpleLabeledDatasetData}.
30 *
31 * @param <K> Type of a key in <tt>upstream</tt> data.
32 * @param <V> Type of a value in <tt>upstream</tt> data.
33 * @param <C> type of a partition <tt>context</tt>.
34 */
35 public class SimpleLabeledDatasetDataBuilder<K, V, C extends Serializable>
36 implements PartitionDataBuilder<K, V, C, SimpleLabeledDatasetData> {
37 /** */
38 private static final long serialVersionUID = 3678784980215216039L;
39
40 /** Function that extracts features from an {@code upstream} data. */
41 private final IgniteBiFunction<K, V, Vector> featureExtractor;
42
43 /** Function that extracts labels from an {@code upstream} data. */
44 private final IgniteBiFunction<K, V, double[]> lbExtractor;
45
46 /**
47 * Constructs a new instance of partition {@code data} builder that makes {@link SimpleLabeledDatasetData}.
48 *
49 * @param featureExtractor Function that extracts features from an {@code upstream} data.
50 * @param lbExtractor Function that extracts labels from an {@code upstream} data.
51 */
52 public SimpleLabeledDatasetDataBuilder(IgniteBiFunction<K, V, Vector> featureExtractor,
53 IgniteBiFunction<K, V, double[]> lbExtractor) {
54 this.featureExtractor = featureExtractor;
55 this.lbExtractor = lbExtractor;
56 }
57
58 /** {@inheritDoc} */
59 @Override public SimpleLabeledDatasetData build(Iterator<UpstreamEntry<K, V>> upstreamData,
60 long upstreamDataSize, C ctx) {
61 // Prepares the matrix of features in flat column-major format.
62 int featureCols = -1;
63 int lbCols = -1;
64 double[] features = null;
65 double[] labels = null;
66
67 int ptr = 0;
68 while (upstreamData.hasNext()) {
69 UpstreamEntry<K, V> entry = upstreamData.next();
70
71 Vector featureRow = featureExtractor.apply(entry.getKey(), entry.getValue());
72
73 if (featureCols < 0) {
74 featureCols = featureRow.size();
75 features = new double[Math.toIntExact(upstreamDataSize * featureCols)];
76 }
77 else
78 assert featureRow.size() == featureCols : "Feature extractor must return exactly " + featureCols
79 + " features";
80
81 for (int i = 0; i < featureCols; i++)
82 features[Math.toIntExact(i * upstreamDataSize) + ptr] = featureRow.get(i);
83
84 double[] lbRow = lbExtractor.apply(entry.getKey(), entry.getValue());
85
86 if (lbCols < 0) {
87 lbCols = lbRow.length;
88 labels = new double[Math.toIntExact(upstreamDataSize * lbCols)];
89 }
90
91 assert lbRow.length == lbCols : "Label extractor must return exactly " + lbCols + " labels";
92
93 for (int i = 0; i < lbCols; i++)
94 labels[Math.toIntExact(i * upstreamDataSize) + ptr] = lbRow[i];
95
96 ptr++;
97 }
98
99 return new SimpleLabeledDatasetData(features, labels, Math.toIntExact(upstreamDataSize));
100 }
101 }