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

com.davidbracewell.apollo.ml.preprocess.transform.RealToBinaryTransform Maven / Gradle / Ivy

package com.davidbracewell.apollo.ml.preprocess.transform;

import com.davidbracewell.apollo.ml.Feature;
import com.davidbracewell.apollo.ml.Instance;
import com.davidbracewell.apollo.ml.preprocess.RestrictedInstancePreprocessor;
import com.davidbracewell.io.structured.ElementType;
import com.davidbracewell.io.structured.StructuredReader;
import com.davidbracewell.io.structured.StructuredWriter;
import com.davidbracewell.stream.MStream;
import com.davidbracewell.string.StringUtils;
import lombok.NonNull;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Stream;

/**
 * 

Converts a real value feature into a binary feature by converting values greater than or equal to a given * threshold "true" and others "false"

* * @author David B. Bracewell */ public class RealToBinaryTransform extends RestrictedInstancePreprocessor implements TransformProcessor, Serializable { private static final long serialVersionUID = 1L; private double threshold; /** * Instantiates a new Real to binary transform with no feature restriction. * * @param threshold the threshold with which a feature value must be >= to become a binary "true" */ public RealToBinaryTransform(double threshold) { this.threshold = threshold; } /** * Instantiates a new Real to binary transform. * * @param featureNamePrefix the feature name prefix to restrict * @param threshold the threshold with which a feature value must be >= to become a binary * "true" */ public RealToBinaryTransform(@NonNull String featureNamePrefix, double threshold) { super(featureNamePrefix); this.threshold = threshold; } protected RealToBinaryTransform() { this(StringUtils.EMPTY, 0); } @Override public void reset() { } @Override public String describe() { if (applyToAll()) { return "RealToBinaryTransform{threshold=" + threshold + "}"; } return "RealToBinaryTransform[" + getRestriction() + "]{threshold=" + threshold + "}"; } @Override protected void restrictedFitImpl(MStream> stream) { } @Override public boolean requiresFit() { return false; } @Override protected Stream restrictedProcessImpl(Stream featureStream, Instance originalExample) { return featureStream.filter(f -> f.getValue() >= threshold).map(feature -> Feature.TRUE(feature.getName())); } @Override public void write(@NonNull StructuredWriter writer) throws IOException { if (!applyToAll()) { writer.writeKeyValue("restriction", getRestriction()); } writer.writeKeyValue("threshold", threshold); } @Override public void read(@NonNull StructuredReader reader) throws IOException { reset(); while (reader.peek() != ElementType.END_OBJECT) { switch (reader.peekName()) { case "restriction": setRestriction(reader.nextKeyValue().v2.asString()); break; case "threshold": this.threshold = reader.nextKeyValue().v2.asDoubleValue(); break; } } } }// END OF RealToBinaryTransform




© 2015 - 2025 Weber Informatics LLC | Privacy Policy