Search in sources :

Example 61 with Feature

use of org.jpmml.converter.Feature in project jpmml-r by jpmml.

the class RExpEncoder method addFeature.

public void addFeature(Field<?> field) {
    Feature feature;
    OpType opType = field.requireOpType();
    switch(opType) {
        case CATEGORICAL:
            feature = new CategoricalFeature(this, (DataField) field);
            break;
        case CONTINUOUS:
            feature = new ContinuousFeature(this, field);
            break;
        default:
            throw new IllegalArgumentException();
    }
    addFeature(feature);
}
Also used : ContinuousFeature(org.jpmml.converter.ContinuousFeature) DataField(org.dmg.pmml.DataField) OpType(org.dmg.pmml.OpType) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature)

Example 62 with Feature

use of org.jpmml.converter.Feature in project jpmml-r by jpmml.

the class SVMConverter method scaleFeatures.

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));
        }
        if (!ValueUtil.isOne(scale)) {
            expression = PMMLUtil.createApply(PMMLFunctions.DIVIDE, expression, PMMLUtil.createConstant(scale));
        }
        DerivedField derivedField = encoder.createDerivedField(FieldNameUtil.create("scale", continuousFeature), OpType.CONTINUOUS, DataType.DOUBLE, expression);
        features.set(i, new ContinuousFeature(encoder, derivedField));
    }
}
Also used : ContinuousFeature(org.jpmml.converter.ContinuousFeature) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Expression(org.dmg.pmml.Expression) DerivedField(org.dmg.pmml.DerivedField)

Example 63 with Feature

use of org.jpmml.converter.Feature in project jpmml-r by jpmml.

the class MVRConverter method scaleFeatures.

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(FieldNameUtil.create("scale", continuousFeature), OpType.CONTINUOUS, DataType.DOUBLE, apply);
        features.set(i, new ContinuousFeature(encoder, derivedField));
    }
}
Also used : ContinuousFeature(org.jpmml.converter.ContinuousFeature) Apply(org.dmg.pmml.Apply) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) DerivedField(org.dmg.pmml.DerivedField)

Example 64 with Feature

use of org.jpmml.converter.Feature in project jpmml-r by jpmml.

the class RecipeEncoder method createSchema.

@Override
public Schema createSchema() {
    RGenericVector recipe = getObject();
    Label label = getLabel();
    List<? extends Feature> features = getFeatures();
    RGenericVector steps = recipe.getGenericElement("steps");
    List<String> outcomeNames = this.termRoles.entrySet().stream().filter(entry -> (entry.getValue() == Role.OUTCOME)).map(entry -> entry.getKey()).collect(Collectors.toList());
    if (outcomeNames.size() == 1) {
        String outcomeName = outcomeNames.get(0);
        renameDataField(label.getName(), outcomeName);
        label = label.toRenamedLabel(outcomeName);
    } else if (outcomeNames.size() >= 2) {
        throw new IllegalArgumentException();
    }
    if (steps != null) {
        throw new IllegalArgumentException();
    }
    return new Schema(this, label, features);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) DataField(org.dmg.pmml.DataField) List(java.util.List) Feature(org.jpmml.converter.Feature) Schema(org.jpmml.converter.Schema) Map(java.util.Map) Label(org.jpmml.converter.Label) Collections(java.util.Collections) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Schema(org.jpmml.converter.Schema) Label(org.jpmml.converter.Label)

Example 65 with Feature

use of org.jpmml.converter.Feature in project jpmml-r by jpmml.

the class ScorecardConverter method encodeModel.

@Override
public Scorecard encodeModel(Schema schema) {
    RGenericVector glm = getObject();
    RDoubleVector coefficients = glm.getDoubleElement("coefficients");
    RGenericVector family = glm.getGenericElement("family");
    RGenericVector scConf = DecorationUtil.getGenericElement(glm, "sc.conf");
    Double intercept = coefficients.getElement(LMConverter.INTERCEPT, false);
    List<? extends Feature> features = schema.getFeatures();
    SchemaUtil.checkSize(coefficients.size() - (intercept != null ? 1 : 0), features);
    RNumberVector<?> odds = scConf.getNumericElement("odds");
    RNumberVector<?> basePoints = scConf.getNumericElement("base_points");
    RNumberVector<?> pdo = scConf.getNumericElement("pdo");
    double factor = (pdo.asScalar()).doubleValue() / Math.log(2);
    Map<String, Characteristic> fieldCharacteristics = new LinkedHashMap<>();
    for (Feature feature : features) {
        String name = feature.getName();
        if (!(feature instanceof BinaryFeature)) {
            throw new IllegalArgumentException();
        }
        Double coefficient = getFeatureCoefficient(feature, coefficients);
        Characteristic characteristic = fieldCharacteristics.get(name);
        if (characteristic == null) {
            characteristic = new Characteristic().setName("score(" + FeatureUtil.getName(feature) + ")");
            fieldCharacteristics.put(name, characteristic);
        }
        BinaryFeature binaryFeature = (BinaryFeature) feature;
        SimplePredicate simplePredicate = new SimplePredicate(binaryFeature.getName(), SimplePredicate.Operator.EQUAL, binaryFeature.getValue());
        Attribute attribute = new Attribute(simplePredicate).setPartialScore(formatScore(-1d * coefficient * factor));
        characteristic.addAttributes(attribute);
    }
    Characteristics characteristics = new Characteristics();
    Collection<Map.Entry<String, Characteristic>> entries = fieldCharacteristics.entrySet();
    for (Map.Entry<String, Characteristic> entry : entries) {
        Characteristic characteristic = entry.getValue();
        Attribute attribute = new Attribute(True.INSTANCE).setPartialScore(0d);
        characteristic.addAttributes(attribute);
        characteristics.addCharacteristics(characteristic);
    }
    Scorecard scorecard = new Scorecard(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()), characteristics).setInitialScore(formatScore((basePoints.asScalar()).doubleValue() - Math.log((odds.asScalar()).doubleValue()) * factor - (intercept != null ? intercept * factor : 0))).setUseReasonCodes(false);
    return scorecard;
}
Also used : Attribute(org.dmg.pmml.scorecard.Attribute) Characteristic(org.dmg.pmml.scorecard.Characteristic) BinaryFeature(org.jpmml.converter.BinaryFeature) Feature(org.jpmml.converter.Feature) BinaryFeature(org.jpmml.converter.BinaryFeature) SimplePredicate(org.dmg.pmml.SimplePredicate) LinkedHashMap(java.util.LinkedHashMap) Characteristics(org.dmg.pmml.scorecard.Characteristics) Scorecard(org.dmg.pmml.scorecard.Scorecard) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Feature (org.jpmml.converter.Feature)69 ContinuousFeature (org.jpmml.converter.ContinuousFeature)39 ArrayList (java.util.ArrayList)30 CategoricalFeature (org.jpmml.converter.CategoricalFeature)29 DerivedField (org.dmg.pmml.DerivedField)16 DataField (org.dmg.pmml.DataField)12 SimplePredicate (org.dmg.pmml.SimplePredicate)12 List (java.util.List)11 Predicate (org.dmg.pmml.Predicate)11 Node (org.dmg.pmml.tree.Node)11 BooleanFeature (org.jpmml.converter.BooleanFeature)11 Apply (org.dmg.pmml.Apply)9 BinaryFeature (org.jpmml.converter.BinaryFeature)9 Expression (org.dmg.pmml.Expression)7 FieldName (org.dmg.pmml.FieldName)7 CategoricalLabel (org.jpmml.converter.CategoricalLabel)7 InteractionFeature (org.jpmml.converter.InteractionFeature)6 Vector (org.apache.spark.ml.linalg.Vector)5 BranchNode (org.dmg.pmml.tree.BranchNode)5 LeafNode (org.dmg.pmml.tree.LeafNode)5