IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / modules / ml / src / main / java / org / apache / ignite / ml / structures / partition / LabeledDatasetPartitionDataBuilderOnHeap.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.structures.partition;
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 import org.apache.ignite.ml.structures.LabeledDataset;
27 import org.apache.ignite.ml.structures.LabeledVector;
28
29 /**
30 * Partition data builder that builds {@link LabeledDataset}.
31 *
32 * @param <K> Type of a key in <tt>upstream</tt> data.
33 * @param <V> Type of a value in <tt>upstream</tt> data.
34 * @param <C> Type of a partition <tt>context</tt>.
35 */
36 public class LabeledDatasetPartitionDataBuilderOnHeap<K, V, C extends Serializable>
37 implements PartitionDataBuilder<K, V, C, LabeledDataset<Double, LabeledVector>> {
38 /** */
39 private static final long serialVersionUID = -7820760153954269227L;
40
41 /** Extractor of X matrix row. */
42 private final IgniteBiFunction<K, V, Vector> xExtractor;
43
44 /** Extractor of Y vector value. */
45 private final IgniteBiFunction<K, V, Double> yExtractor;
46
47 /**
48 * Constructs a new instance of SVM partition data builder.
49 *
50 * @param xExtractor Extractor of X matrix row.
51 * @param yExtractor Extractor of Y vector value.
52 */
53 public LabeledDatasetPartitionDataBuilderOnHeap(IgniteBiFunction<K, V, Vector> xExtractor,
54 IgniteBiFunction<K, V, Double> yExtractor) {
55 this.xExtractor = xExtractor;
56 this.yExtractor = yExtractor;
57 }
58
59 /** {@inheritDoc} */
60 @Override public LabeledDataset<Double, LabeledVector> build(Iterator<UpstreamEntry<K, V>> upstreamData,
61 long upstreamDataSize, C ctx) {
62 int xCols = -1;
63 double[][] x = null;
64 double[] y = new double[Math.toIntExact(upstreamDataSize)];
65
66 int ptr = 0;
67
68 while (upstreamData.hasNext()) {
69 UpstreamEntry<K, V> entry = upstreamData.next();
70 Vector row = xExtractor.apply(entry.getKey(), entry.getValue());
71
72 if (xCols < 0) {
73 xCols = row.size();
74 x = new double[Math.toIntExact(upstreamDataSize)][xCols];
75 }
76 else
77 assert row.size() == xCols : "X extractor must return exactly " + xCols + " columns";
78
79 x[ptr] = row.asArray();
80
81 y[ptr] = yExtractor.apply(entry.getKey(), entry.getValue());
82
83 ptr++;
84 }
85 return new LabeledDataset<>(x, y);
86 }
87 }