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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.ignite
.ml
.preprocessing
.minmaxscaling
;
20 import org
.apache
.ignite
.ml
.math
.Vector
;
21 import org
.apache
.ignite
.ml
.math
.functions
.IgniteBiFunction
;
24 * Preprocessing function that makes minmaxscaling. From mathematical point of view it's the following function which
25 * is applied to every element in dataset:
27 * {@code a_i = (a_i - min_i) / (max_i - min_i) for all i},
29 * where {@code i} is a number of column, {@code max_i} is the value of the maximum element in this columns,
30 * {@code min_i} is the value of the minimal element in this column.
32 * @param <K> Type of a key in {@code upstream} data.
33 * @param <V> Type of a value in {@code upstream} data.
35 public class MinMaxScalerPreprocessor
<K
, V
> implements IgniteBiFunction
<K
, V
, Vector
> {
37 private static final long serialVersionUID
= 6997800576392623469L;
39 /** Minimal values. */
40 private final double[] min
;
42 /** Maximum values. */
43 private final double[] max
;
45 /** Base preprocessor. */
46 private final IgniteBiFunction
<K
, V
, Vector
> basePreprocessor
;
49 * Constructs a new instance of minmaxscaling preprocessor.
51 * @param min Minimal values.
52 * @param max Maximum values.
53 * @param basePreprocessor Base preprocessor.
55 public MinMaxScalerPreprocessor(double[] min
, double[] max
, IgniteBiFunction
<K
, V
, Vector
> basePreprocessor
) {
58 this.basePreprocessor
= basePreprocessor
;
62 * Applies this preprocessor.
66 * @return Preprocessed row.
68 @Override public Vector
apply(K k
, V v
) {
69 Vector res
= basePreprocessor
.apply(k
, v
);
71 assert res
.size() == min
.length
;
72 assert res
.size() == max
.length
;
74 for (int i
= 0; i
< res
.size(); i
++)
75 res
.set(i
, (res
.get(i
) - min
[i
]) / (max
[i
] - min
[i
]));
81 public double[] getMin() {
86 public double[] getMax() {