org.jpmml.converter.ContinuousFeature Java Examples
The following examples show how to use
org.jpmml.converter.ContinuousFeature.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PowerFunctionTransformer.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 6 votes |
@Override public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){ Integer power = getPower(); List<Feature> result = new ArrayList<>(); for(Feature feature : features){ if(feature instanceof BinaryFeature){ BinaryFeature binaryFeature = (BinaryFeature)feature; result.add(binaryFeature); } else { ContinuousFeature continuousFeature = feature.toContinuousFeature(); result.add(new PowerFeature(encoder, continuousFeature, power)); } } return result; }
Example #2
Source File: Aggregator.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 6 votes |
@Override public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){ String function = getFunction(); if(features.size() <= 1){ return features; } Apply apply = PMMLUtil.createApply(translateFunction(function)); for(Feature feature : features){ apply.addExpressions(feature.ref()); } FieldName name = FeatureUtil.createName(function, features); DerivedField derivedField = encoder.createDerivedField(name, OpType.CONTINUOUS, DataType.DOUBLE, apply); return Collections.singletonList(new ContinuousFeature(encoder, derivedField)); }
Example #3
Source File: FunctionTransformer.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 6 votes |
@Override public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){ UFunc func = getFunc(); if(func == null){ return features; } List<Feature> result = new ArrayList<>(); for(int i = 0; i < features.size(); i++){ ContinuousFeature continuousFeature = (features.get(i)).toContinuousFeature(); DerivedField derivedField = encoder.ensureDerivedField(FeatureUtil.createName(func.getName(), continuousFeature), OpType.CONTINUOUS, DataType.DOUBLE, () -> UFuncUtil.encodeUFunc(func, Collections.singletonList(continuousFeature.ref()))); result.add(new ContinuousFeature(encoder, derivedField)); } return result; }
Example #4
Source File: TreeUtil.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 6 votes |
static private Schema toTreeModelSchema(DataType dataType, Schema schema){ Function<Feature, Feature> function = new Function<Feature, Feature>(){ @Override public Feature apply(Feature feature){ if(feature instanceof BinaryFeature){ BinaryFeature binaryFeature = (BinaryFeature)feature; return binaryFeature; } else { ContinuousFeature continuousFeature = feature.toContinuousFeature(dataType); return continuousFeature; } } }; return schema.toTransformedSchema(function); }
Example #5
Source File: EncoderUtil.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 5 votes |
static public Feature encodeIndexFeature(Feature feature, List<?> categories, List<? extends Number> indexCategories, Number mapMissingTo, Number defaultValue, DataType dataType, SkLearnEncoder encoder){ ClassDictUtil.checkSize(categories, indexCategories); encoder.toCategorical(feature.getName(), categories); Supplier<MapValues> mapValuesSupplier = () -> { MapValues mapValues = PMMLUtil.createMapValues(feature.getName(), categories, indexCategories) .setMapMissingTo(mapMissingTo) .setDefaultValue(defaultValue); return mapValues; }; DerivedField derivedField = encoder.ensureDerivedField(FeatureUtil.createName("encoder", feature), OpType.CATEGORICAL, dataType, mapValuesSupplier); Feature encodedFeature = new IndexFeature(encoder, derivedField, indexCategories); Feature result = new CategoricalFeature(encoder, feature, categories){ @Override public ContinuousFeature toContinuousFeature(){ return encodedFeature.toContinuousFeature(); } }; return result; }
Example #6
Source File: RegressionTree.java From pyramid with Apache License 2.0 | 5 votes |
static private Predicate encodePredicate(Feature feature, Node node, boolean left){ FieldName name = feature.getName(); SimplePredicate.Operator operator; String value; if(feature instanceof BinaryFeature){ BinaryFeature binaryFeature = (BinaryFeature)feature; operator = (left ? SimplePredicate.Operator.NOT_EQUAL : SimplePredicate.Operator.EQUAL); value = binaryFeature.getValue(); } else { ContinuousFeature continuousFeature = feature.toContinuousFeature(); Number splitValue = node.getThreshold(); DataType dataType = continuousFeature.getDataType(); switch(dataType){ case INTEGER: splitValue = (int)(splitValue.floatValue() + 1f); break; case FLOAT: break; default: throw new IllegalArgumentException(); } operator = (left ? SimplePredicate.Operator.LESS_OR_EQUAL : SimplePredicate.Operator.GREATER_THAN); value = ValueUtil.formatValue(splitValue); } SimplePredicate simplePredicate = new SimplePredicate(name, operator) .setValue(value); return simplePredicate; }
Example #7
Source File: MiningModelUtil.java From pyramid with Apache License 2.0 | 5 votes |
@Override public Feature apply(Model model){ Output output = model.getOutput(); if(output == null || !output.hasOutputFields()){ throw new IllegalArgumentException(); } OutputField outputField = Iterables.getLast(output.getOutputFields()); return new ContinuousFeature(null, outputField.getName(), outputField.getDataType()); }
Example #8
Source File: Formula.java From jpmml-r with GNU Affero General Public License v3.0 | 5 votes |
public void addField(Field<?> field){ RExpEncoder encoder = getEncoder(); Feature feature = new ContinuousFeature(encoder, field); if(field instanceof DerivedField){ DerivedField derivedField = (DerivedField)field; Expression expression = derivedField.getExpression(); if(expression instanceof Apply){ Apply apply = (Apply)expression; if(checkApply(apply, PMMLFunctions.POW, FieldRef.class, Constant.class)){ List<Expression> expressions = apply.getExpressions(); FieldRef fieldRef = (FieldRef)expressions.get(0); Constant constant = (Constant)expressions.get(1); try { String string = ValueUtil.asString(constant.getValue()); int power = Integer.parseInt(string); feature = new PowerFeature(encoder, fieldRef.getField(), DataType.DOUBLE, power); } catch(NumberFormatException nfe){ // Ignored } } } } putFeature(field.getName(), feature); this.fields.add(field); }
Example #9
Source File: MVRConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 5 votes |
private void scaleFeatures(RExpEncoder encoder){ RGenericVector mvr = getObject(); RDoubleVector scale = mvr.getDoubleElement("scale", false); if(scale == null){ return; } List<Feature> features = encoder.getFeatures(); if(scale.size() != features.size()){ throw new IllegalArgumentException(); } for(int i = 0; i < features.size(); i++){ Feature feature = features.get(i); Double factor = scale.getValue(i); if(ValueUtil.isOne(factor)){ continue; } ContinuousFeature continuousFeature = feature.toContinuousFeature(); Apply apply = PMMLUtil.createApply(PMMLFunctions.DIVIDE, continuousFeature.ref(), PMMLUtil.createConstant(factor)); DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("scale", feature), OpType.CONTINUOUS, DataType.DOUBLE, apply); features.set(i, new ContinuousFeature(encoder, derivedField)); } }
Example #10
Source File: ExpressionTransformer.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 5 votes |
@Override public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){ Object dtype = getDType(); String expr = getExpr(); Scope scope = new DataFrameScope(FieldName.create("X"), features); Expression expression = ExpressionTranslator.translate(expr, scope); DataType dataType; if(dtype != null){ dataType = TransformerUtil.getDataType(dtype); } else { if(ExpressionTranslator.isString(expression, scope)){ dataType = DataType.STRING; } else { dataType = DataType.DOUBLE; } } OpType opType = TransformerUtil.getOpType(dataType); DerivedField derivedField = encoder.createDerivedField(FieldName.create("eval(" + expr + ")"), opType, dataType, expression); return Collections.singletonList(new ContinuousFeature(encoder, derivedField)); }
Example #11
Source File: BoosterUtil.java From jpmml-sparkml-xgboost with GNU Affero General Public License v3.0 | 5 votes |
static public <C extends ModelConverter<?> & HasXGBoostOptions> MiningModel encodeBooster(C converter, Booster booster, Schema schema){ byte[] bytes = booster.toByteArray(); Learner learner; try(InputStream is = new ByteArrayInputStream(bytes)){ learner = XGBoostUtil.loadLearner(is); } catch(IOException ioe){ throw new RuntimeException(ioe); } Function<Feature, Feature> function = new Function<Feature, Feature>(){ @Override public Feature apply(Feature feature){ if(feature instanceof BinaryFeature){ BinaryFeature binaryFeature = (BinaryFeature)feature; return binaryFeature; } else { ContinuousFeature continuousFeature = feature.toContinuousFeature(DataType.FLOAT); return continuousFeature; } } }; Map<String, Object> options = new LinkedHashMap<>(); options.put(HasXGBoostOptions.OPTION_COMPACT, converter.getOption(HasXGBoostOptions.OPTION_COMPACT, false)); options.put(HasXGBoostOptions.OPTION_NTREE_LIMIT, converter.getOption(HasXGBoostOptions.OPTION_NTREE_LIMIT, null)); Schema xgbSchema = schema.toTransformedSchema(function); return learner.encodeMiningModel(options, xgbSchema); }
Example #12
Source File: Learner.java From jpmml-xgboost with GNU Affero General Public License v3.0 | 5 votes |
public Schema toXGBoostSchema(Schema schema){ Function<Feature, Feature> function = new Function<Feature, Feature>(){ @Override public Feature apply(Feature feature){ if(feature instanceof BinaryFeature){ BinaryFeature binaryFeature = (BinaryFeature)feature; return binaryFeature; } else { ContinuousFeature continuousFeature = feature.toContinuousFeature(); DataType dataType = continuousFeature.getDataType(); switch(dataType){ case INTEGER: case FLOAT: break; case DOUBLE: continuousFeature = continuousFeature.toContinuousFeature(DataType.FLOAT); break; default: throw new IllegalArgumentException("Expected integer, float or double data type, got " + dataType.value() + " data type"); } return continuousFeature; } } }; return schema.toTransformedSchema(function); }
Example #13
Source File: Binarizer.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
@Override public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){ Number threshold = getThreshold(); List<Feature> result = new ArrayList<>(); for(int i = 0; i < features.size(); i++){ Feature feature = features.get(i); ContinuousFeature continuousFeature = feature.toContinuousFeature(); // "($name <= threshold) ? 0 : 1" Apply apply = PMMLUtil.createApply(PMMLFunctions.THRESHOLD, continuousFeature.ref(), PMMLUtil.createConstant(threshold)); DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("binarizer", continuousFeature), apply); result.add(new ContinuousFeature(encoder, derivedField)); } return result; }
Example #14
Source File: TensorFlowEncoder.java From jpmml-tensorflow with GNU Affero General Public License v3.0 | 4 votes |
public ContinuousFeature createContinuousFeature(SavedModel savedModel, NodeDef placeholder){ NodeDef cast = null; if(("Cast").equals(placeholder.getOp())){ cast = placeholder; placeholder = savedModel.getNodeDef(placeholder.getInput(0)); } DataField dataField = ensureContinuousDataField(savedModel, placeholder); ContinuousFeature result = new ContinuousFeature(this, dataField); if(cast != null){ Operation operation = savedModel.getOperation(cast.getName()); Output output = operation.output(0); result = result.toContinuousFeature(TypeUtil.getDataType(output)); } return result; }
Example #15
Source File: BucketizerConverter.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 4 votes |
@Override public List<Feature> encodeFeatures(SparkMLEncoder encoder){ Bucketizer transformer = getTransformer(); InOutMode inputMode = getInputMode(); String[] inputCols; double[][] splitsArray; if((InOutMode.SINGLE).equals(inputMode)){ inputCols = inputMode.getInputCols(transformer); splitsArray = new double[][]{transformer.getSplits()}; } else if((InOutMode.MULTIPLE).equals(inputMode)){ inputCols = inputMode.getInputCols(transformer); splitsArray = transformer.getSplitsArray(); } else { throw new IllegalArgumentException(); } List<Feature> result = new ArrayList<>(); for(int i = 0; i < inputCols.length; i++){ String inputCol = inputCols[i]; double[] splits = splitsArray[i]; Feature feature = encoder.getOnlyFeature(inputCol); ContinuousFeature continuousFeature = feature.toContinuousFeature(); Discretize discretize = new Discretize(continuousFeature.getName()) .setDataType(DataType.INTEGER); List<Integer> categories = new ArrayList<>(); for(int j = 0; j < (splits.length - 1); j++){ Integer category = j; categories.add(category); Interval interval = new Interval((j < (splits.length - 2)) ? Interval.Closure.CLOSED_OPEN : Interval.Closure.CLOSED_CLOSED) .setLeftMargin(formatMargin(splits[j])) .setRightMargin(formatMargin(splits[j + 1])); DiscretizeBin discretizeBin = new DiscretizeBin(category, interval); discretize.addDiscretizeBins(discretizeBin); } DerivedField derivedField = encoder.createDerivedField(formatName(transformer, i), OpType.CATEGORICAL, DataType.INTEGER, discretize); result.add(new IndexFeature(encoder, derivedField, categories)); } return result; }
Example #16
Source File: StandardScalerModelConverter.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 4 votes |
@Override public List<Feature> encodeFeatures(SparkMLEncoder encoder){ StandardScalerModel transformer = getTransformer(); Vector mean = transformer.mean(); Vector std = transformer.std(); boolean withMean = transformer.getWithMean(); boolean withStd = transformer.getWithStd(); List<Feature> features = encoder.getFeatures(transformer.getInputCol()); if(withMean){ SchemaUtil.checkSize(mean.size(), features); } // End if if(withStd){ SchemaUtil.checkSize(std.size(), features); } List<Feature> result = new ArrayList<>(); for(int i = 0, length = features.size(); i < length; i++){ Feature feature = features.get(i); FieldName name = formatName(transformer, i, length); Expression expression = null; if(withMean){ double meanValue = mean.apply(i); if(!ValueUtil.isZero(meanValue)){ ContinuousFeature continuousFeature = feature.toContinuousFeature(); expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, continuousFeature.ref(), PMMLUtil.createConstant(meanValue)); } } // End if if(withStd){ double stdValue = std.apply(i); if(!ValueUtil.isOne(stdValue)){ Double factor = (1d / stdValue); if(expression != null){ expression = PMMLUtil.createApply(PMMLFunctions.MULTIPLY, expression, PMMLUtil.createConstant(factor)); } else { feature = new ProductFeature(encoder, feature, factor){ @Override public ContinuousFeature toContinuousFeature(){ Supplier<Apply> applySupplier = () -> { Feature feature = getFeature(); Number factor = getFactor(); return PMMLUtil.createApply(PMMLFunctions.MULTIPLY, (feature.toContinuousFeature()).ref(), PMMLUtil.createConstant(factor)); }; return toContinuousFeature(name, DataType.DOUBLE, applySupplier); } }; } } } // End if if(expression != null){ DerivedField derivedField = encoder.createDerivedField(name, OpType.CONTINUOUS, DataType.DOUBLE, expression); result.add(new ContinuousFeature(encoder, derivedField)); } else { result.add(feature); } } return result; }
Example #17
Source File: SVMConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 4 votes |
private void scaleFeatures(RExpEncoder encoder){ RGenericVector svm = getObject(); RDoubleVector sv = svm.getDoubleElement("SV"); RBooleanVector scaled = svm.getBooleanElement("scaled"); RGenericVector xScale = svm.getGenericElement("x.scale"); RStringVector rowNames = sv.dimnames(0); RStringVector columnNames = sv.dimnames(1); List<Feature> features = encoder.getFeatures(); if((scaled.size() != columnNames.size()) || (scaled.size() != features.size())){ throw new IllegalArgumentException(); } RDoubleVector xScaledCenter = xScale.getDoubleElement("scaled:center"); RDoubleVector xScaledScale = xScale.getDoubleElement("scaled:scale"); for(int i = 0; i < columnNames.size(); i++){ String columnName = columnNames.getValue(i); if(!scaled.getValue(i)){ continue; } Feature feature = features.get(i); Double center = xScaledCenter.getElement(columnName); Double scale = xScaledScale.getElement(columnName); if(ValueUtil.isZero(center) && ValueUtil.isOne(scale)){ continue; } ContinuousFeature continuousFeature = feature.toContinuousFeature(); Expression expression = continuousFeature.ref(); if(!ValueUtil.isZero(center)){ expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, expression, PMMLUtil.createConstant(center)); } // End if if(!ValueUtil.isOne(scale)){ expression = PMMLUtil.createApply(PMMLFunctions.DIVIDE, expression, PMMLUtil.createConstant(scale)); } DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("scale", feature), OpType.CONTINUOUS, DataType.DOUBLE, expression); features.set(i, new ContinuousFeature(encoder, derivedField)); } }
Example #18
Source File: VectorIndexerModelConverter.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 4 votes |
@Override public List<Feature> encodeFeatures(SparkMLEncoder encoder){ VectorIndexerModel transformer = getTransformer(); int numFeatures = transformer.numFeatures(); List<Feature> features = encoder.getFeatures(transformer.getInputCol()); SchemaUtil.checkSize(numFeatures, features); Map<Integer, Map<Double, Integer>> categoryMaps = transformer.javaCategoryMaps(); List<Feature> result = new ArrayList<>(); for(int i = 0, length = numFeatures; i < length; i++){ Feature feature = features.get(i); Map<Double, Integer> categoryMap = categoryMaps.get(i); if(categoryMap != null){ List<Double> categories = new ArrayList<>(); List<Integer> values = new ArrayList<>(); List<Map.Entry<Double, Integer>> entries = new ArrayList<>(categoryMap.entrySet()); Collections.sort(entries, VectorIndexerModelConverter.COMPARATOR); for(Map.Entry<Double, Integer> entry : entries){ Double category = entry.getKey(); Integer value = entry.getValue(); categories.add(category); values.add(value); } encoder.toCategorical(feature.getName(), categories); MapValues mapValues = PMMLUtil.createMapValues(feature.getName(), categories, values) .setDataType(DataType.INTEGER); DerivedField derivedField = encoder.createDerivedField(formatName(transformer, i, length), OpType.CATEGORICAL, DataType.INTEGER, mapValues); result.add(new CategoricalFeature(encoder, derivedField, values)); } else { result.add((ContinuousFeature)feature); } } return result; }
Example #19
Source File: BSplineTransformer.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
@Override public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){ BSpline bspline = getBSpline(); ClassDictUtil.checkSize(1, features); Feature feature = features.get(0); ContinuousFeature continuousFeature = feature.toContinuousFeature(); DefineFunction defineFunction = createBSplineFunction(bspline, encoder); Apply apply = PMMLUtil.createApply(defineFunction.getName()) .addExpressions(continuousFeature.ref()); DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("bspline", feature), apply); return Collections.singletonList(new ContinuousFeature(encoder, derivedField)); }
Example #20
Source File: SecondsSinceMidnightTransformer.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
@Override public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){ List<Feature> result = new ArrayList<>(); for(int i = 0; i < features.size(); i++){ ObjectFeature objectFeature = (ObjectFeature)features.get(i); FieldName name = FieldName.create("seconds_since_midnight(" + (FeatureUtil.getName(objectFeature)).getValue() + ")"); DerivedField derivedField = encoder.ensureDerivedField(name, OpType.CONTINUOUS, DataType.INTEGER, () -> PMMLUtil.createApply(PMMLFunctions.DATESECONDSSINCEMIDNIGHT, objectFeature.ref())); result.add(new ContinuousFeature(encoder, derivedField)); } return result; }
Example #21
Source File: TermFeature.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 4 votes |
@Override public ContinuousFeature toContinuousFeature(){ return toContinuousFeature(getName(), getDataType(), () -> createApply()); }
Example #22
Source File: RegressionTableUtil.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 4 votes |
static private MapValues createMapValues(FieldName name, Object identifier, List<Feature> features, List<Double> coefficients){ ListIterator<Feature> featureIt = features.listIterator(); ListIterator<Double> coefficientIt = coefficients.listIterator(); PMMLEncoder encoder = null; List<Object> inputValues = new ArrayList<>(); List<Double> outputValues = new ArrayList<>(); while(featureIt.hasNext()){ Feature feature = featureIt.next(); Double coefficient = coefficientIt.next(); if(!(feature instanceof BinaryFeature)){ continue; } BinaryFeature binaryFeature = (BinaryFeature)feature; if(!(name).equals(binaryFeature.getName())){ continue; } featureIt.remove(); coefficientIt.remove(); if(encoder == null){ encoder = binaryFeature.getEncoder(); } inputValues.add(binaryFeature.getValue()); outputValues.add(coefficient); } MapValues mapValues = PMMLUtil.createMapValues(name, inputValues, outputValues) .setDefaultValue(0d) .setDataType(DataType.DOUBLE); DerivedField derivedField = encoder.createDerivedField(FieldName.create("lookup(" + name.getValue() + (identifier != null ? (", " + identifier) : "") + ")"), OpType.CONTINUOUS, DataType.DOUBLE, mapValues); featureIt.add(new ContinuousFeature(encoder, derivedField)); coefficientIt.add(1d); return mapValues; }
Example #23
Source File: StandardScaler.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
@Override public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){ Boolean withMean = getWithMean(); Boolean withStd = getWithStd(); List<? extends Number> mean = (withMean ? getMean() : null); List<? extends Number> std = (withStd ? getStd() : null); if(mean == null && std == null){ return features; } ClassDictUtil.checkSize(features, mean, std); List<Feature> result = new ArrayList<>(); for(int i = 0; i < features.size(); i++){ Feature feature = features.get(i); Number meanValue = (withMean ? mean.get(i) : 0d); Number stdValue = (withStd ? std.get(i) : 1d); if(ValueUtil.isZero(meanValue) && ValueUtil.isOne(stdValue)){ result.add(feature); continue; } ContinuousFeature continuousFeature = feature.toContinuousFeature(); // "($name - mean) / std" Expression expression = continuousFeature.ref(); if(!ValueUtil.isZero(meanValue)){ expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, expression, PMMLUtil.createConstant(meanValue)); } // End if if(!ValueUtil.isOne(stdValue)){ expression = PMMLUtil.createApply(PMMLFunctions.DIVIDE, expression, PMMLUtil.createConstant(stdValue)); } DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("standard_scaler", continuousFeature), expression); result.add(new ContinuousFeature(encoder, derivedField)); } return result; }
Example #24
Source File: MinMaxScaler.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
@Override public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){ List<? extends Number> min = getMin(); List<? extends Number> scale = getScale(); ClassDictUtil.checkSize(features, min, scale); List<Feature> result = new ArrayList<>(); for(int i = 0; i < features.size(); i++){ Feature feature = features.get(i); Number minValue = min.get(i); Number scaleValue = scale.get(i); if(ValueUtil.isOne(scaleValue) && ValueUtil.isZero(minValue)){ result.add(feature); continue; } ContinuousFeature continuousFeature = feature.toContinuousFeature(); // "($name * scale) + min" Expression expression = continuousFeature.ref(); if(!ValueUtil.isOne(scaleValue)){ expression = PMMLUtil.createApply(PMMLFunctions.MULTIPLY, expression, PMMLUtil.createConstant(scaleValue)); } // End if if(!ValueUtil.isZero(minValue)){ expression = PMMLUtil.createApply(PMMLFunctions.ADD, expression, PMMLUtil.createConstant(minValue)); } DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("mix_max_scaler", continuousFeature), expression); result.add(new ContinuousFeature(encoder, derivedField)); } return result; }
Example #25
Source File: RobustScaler.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
@Override public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){ Boolean withCentering = getWithCentering(); Boolean withScaling = getWithScaling(); List<? extends Number> center = (withCentering ? getCenter() : null); List<? extends Number> scale = (withScaling ? getScale() : null); if(center == null && scale == null){ return features; } ClassDictUtil.checkSize(features, center, scale); List<Feature> result = new ArrayList<>(); for(int i = 0; i < features.size(); i++){ Feature feature = features.get(i); Number centerValue = (withCentering ? center.get(i) : 0d); Number scaleValue = (withScaling ? scale.get(i) : 1d); if(ValueUtil.isZero(centerValue) && ValueUtil.isOne(scaleValue)){ result.add(feature); continue; } ContinuousFeature continuousFeature = feature.toContinuousFeature(); // "($name - center) / scale" Expression expression = continuousFeature.ref(); if(!ValueUtil.isZero(centerValue)){ expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, expression, PMMLUtil.createConstant(centerValue)); } // End if if(!ValueUtil.isOne(scaleValue)){ expression = PMMLUtil.createApply(PMMLFunctions.DIVIDE, expression, PMMLUtil.createConstant(scaleValue)); } DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("robust_scaler", continuousFeature), expression); result.add(new ContinuousFeature(encoder, derivedField)); } return result; }
Example #26
Source File: ImputerUtil.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
static public Feature encodeFeature(Feature feature, Boolean addIndicator, Object missingValue, Object replacementValue, MissingValueTreatmentMethod missingValueTreatmentMethod, SkLearnEncoder encoder){ Field<?> field = feature.getField(); if(field instanceof DataField && !addIndicator){ DataField dataField = (DataField)field; encoder.addDecorator(dataField, new MissingValueDecorator(missingValueTreatmentMethod, replacementValue)); if(missingValue != null){ PMMLUtil.addValues(dataField, Collections.singletonList(missingValue), Value.Property.MISSING); } return feature; } // End if if((field instanceof DataField) || (field instanceof DerivedField)){ Expression expression = feature.ref(); if(missingValue != null){ expression = PMMLUtil.createApply(PMMLFunctions.EQUAL, expression, PMMLUtil.createConstant(missingValue, feature.getDataType())); } else { expression = PMMLUtil.createApply(PMMLFunctions.ISMISSING, expression); } expression = PMMLUtil.createApply(PMMLFunctions.IF) .addExpressions(expression) .addExpressions(PMMLUtil.createConstant(replacementValue, feature.getDataType()), feature.ref()); DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("imputer", feature), field.getOpType(), field.getDataType(), expression); DataType dataType = derivedField.getDataType(); switch(dataType){ case INTEGER: case FLOAT: case DOUBLE: return new ContinuousFeature(encoder, derivedField); case STRING: return new StringFeature(encoder, derivedField); default: return new ObjectFeature(encoder, derivedField.getName(), derivedField.getDataType()); } } else { throw new IllegalArgumentException(); } }
Example #27
Source File: RegressionModelConverter.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 4 votes |
@Override public List<OutputField> registerOutputFields(Label label, Model pmmlModel, SparkMLEncoder encoder){ T model = getTransformer(); String predictionCol = model.getPredictionCol(); Boolean keepPredictionCol = (Boolean)getOption(HasPredictionModelOptions.OPTION_KEEP_PREDICTIONCOL, Boolean.TRUE); OutputField predictedOutputField = ModelUtil.createPredictedField(FieldName.create(predictionCol), OpType.CONTINUOUS, label.getDataType()); DerivedOutputField predictedField = encoder.createDerivedField(pmmlModel, predictedOutputField, keepPredictionCol); encoder.putOnlyFeature(predictionCol, new ContinuousFeature(encoder, predictedField)); return Collections.emptyList(); }
Example #28
Source File: BinarizedCategoricalFeature.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 4 votes |
@Override public ContinuousFeature toContinuousFeature(){ throw new UnsupportedOperationException(); }
Example #29
Source File: MaxAbsScaler.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 3 votes |
@Override public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){ List<? extends Number> scale = getScale(); ClassDictUtil.checkSize(features, scale); List<Feature> result = new ArrayList<>(); for(int i = 0; i < features.size(); i++){ Feature feature = features.get(i); Number value = scale.get(i); if(ValueUtil.isOne(value)){ result.add(feature); continue; } ContinuousFeature continuousFeature = feature.toContinuousFeature(); // "$name / scale" Apply apply = PMMLUtil.createApply(PMMLFunctions.DIVIDE, continuousFeature.ref(), PMMLUtil.createConstant(value)); DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("max_abs_scaler", continuousFeature), apply); result.add(new ContinuousFeature(encoder, derivedField)); } return result; }
Example #30
Source File: DurationTransformer.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 3 votes |
@Override public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){ GregorianCalendar epoch = getEpoch(); String function = getFunction(); LocalDateTime epochDateTime = CalendarUtil.toLocalDateTime(epoch); if(epochDateTime.getMonthValue() != 1 || epochDateTime.getDayOfMonth() != 1){ throw new IllegalArgumentException(String.valueOf(epochDateTime)); } int year = epochDateTime.getYear(); String dateFunction = function; if(dateFunction.startsWith("date")){ dateFunction = dateFunction.substring("date".length(), dateFunction.length()); } dateFunction = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, dateFunction); List<Feature> result = new ArrayList<>(); for(int i = 0; i < features.size(); i++){ ObjectFeature objectFeature = (ObjectFeature)features.get(i); FieldName name = FieldName.create(dateFunction + "(" + (FeatureUtil.getName(objectFeature)).getValue() + ", " + year + ")"); DerivedField derivedField = encoder.ensureDerivedField(name, OpType.CONTINUOUS, DataType.INTEGER, () -> PMMLUtil.createApply(function, objectFeature.ref(), PMMLUtil.createConstant(year, DataType.INTEGER))); result.add(new ContinuousFeature(encoder, derivedField)); } return result; }