All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.chen0040.mlp.functions.RangeScaler Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version
package com.github.chen0040.mlp.functions;


import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by xschen on 30/5/2017.
 */
@Getter
@Setter
public class RangeScaler implements Cloneable {

   private final Map minValue = new HashMap<>();
   private final Map maxValue = new HashMap<>();


   public RangeScaler(List targets) {
      for(int i = 0; i < targets.size(); ++i){
         double[] values = targets.get(i);
         for(int j=0; j < values.length; ++j) {
            minValue.put(j, Math.min(minValue.getOrDefault(j, Double.MAX_VALUE), values[j]));
            maxValue.put(j, Math.max(maxValue.getOrDefault(j, Double.NEGATIVE_INFINITY), values[j]));
         }
      }
   }




   @Override
   public Object clone() throws CloneNotSupportedException {
      RangeScaler obj = (RangeScaler)super.clone();
      obj.getMaxValue().putAll(maxValue);
      obj.getMinValue().putAll(minValue);
      return obj;
   }


   public double[] standardize(double[] target) {
      double[] result = new double[target.length];
      for(int i=0; i < result.length; ++i){
         result[i] = (target[i] - minValue.get(i)) / (maxValue.get(i) - minValue.get(i));
      }
      return result;
   }


   public double[] revert(double[] target) {
      double[] result = new double[target.length];
      for(int i=0; i < result.length; ++i){
         result[i] = minValue.get(i) + target[i] * (maxValue.get(i) - minValue.get(i));
      }
      return result;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy