use of org.dmg.pmml.DataField in project jpmml-r by jpmml.
the class GBMConverter method encodeSchema.
@Override
public void encodeSchema(RExpEncoder encoder) {
RGenericVector gbm = getObject();
RGenericVector distribution = (RGenericVector) gbm.getValue("distribution");
RStringVector response_name = (RStringVector) gbm.getValue("response.name", true);
RGenericVector var_levels = (RGenericVector) gbm.getValue("var.levels");
RStringVector var_names = (RStringVector) gbm.getValue("var.names");
RNumberVector<?> var_type = (RNumberVector<?>) gbm.getValue("var.type");
RStringVector classes = (RStringVector) gbm.getValue("classes", true);
// Dependent variable
{
FieldName responseName;
if (response_name != null) {
responseName = FieldName.create(response_name.asScalar());
} else {
responseName = FieldName.create("y");
}
DataField dataField;
RStringVector distributionName = (RStringVector) distribution.getValue("name");
switch(distributionName.asScalar()) {
case "gaussian":
dataField = encoder.createDataField(responseName, OpType.CONTINUOUS, DataType.DOUBLE);
break;
case "adaboost":
case "bernoulli":
dataField = encoder.createDataField(responseName, OpType.CATEGORICAL, DataType.STRING, GBMConverter.BINARY_CLASSES);
break;
case "multinomial":
dataField = encoder.createDataField(responseName, OpType.CATEGORICAL, DataType.STRING, classes.getValues());
break;
default:
throw new IllegalArgumentException();
}
encoder.setLabel(dataField);
}
// Independent variables
for (int i = 0; i < var_names.size(); i++) {
FieldName varName = FieldName.create(var_names.getValue(i));
DataField dataField;
boolean categorical = (ValueUtil.asInt(var_type.getValue(i)) > 0);
if (categorical) {
RStringVector var_level = (RStringVector) var_levels.getValue(i);
dataField = encoder.createDataField(varName, OpType.CATEGORICAL, DataType.STRING, var_level.getValues());
} else {
dataField = encoder.createDataField(varName, OpType.CONTINUOUS, DataType.DOUBLE);
}
encoder.addFeature(dataField);
}
}
use of org.dmg.pmml.DataField in project jpmml-r by jpmml.
the class IForestConverter method encodeSchema.
@Override
public void encodeSchema(RExpEncoder encoder) {
RGenericVector iForest = getObject();
RStringVector xcols = (RStringVector) iForest.getValue("xcols");
RBooleanVector colisfactor = (RBooleanVector) iForest.getValue("colisfactor");
if (xcols.size() != colisfactor.size()) {
throw new IllegalArgumentException();
}
boolean hasFactors = false;
for (int i = 0; i < colisfactor.size(); i++) {
hasFactors |= colisfactor.getValue(i);
}
if (hasFactors) {
throw new IllegalArgumentException();
}
// Dependent variable
{
DataField dataField = encoder.createDataField(FieldName.create("pathLength"), OpType.CONTINUOUS, DataType.DOUBLE);
encoder.setLabel(dataField);
}
// Independent variables
for (int i = 0; i < xcols.size(); i++) {
String xcol = xcols.getValue(i);
DataField dataField = encoder.createDataField(FieldName.create(xcol), OpType.CONTINUOUS, DataType.DOUBLE);
encoder.addFeature(dataField);
}
}
use of org.dmg.pmml.DataField in project jpmml-r by jpmml.
the class LMConverter method encodeSchema.
public void encodeSchema(FormulaContext context, RExp terms, RExpEncoder encoder) {
RIntegerVector response = (RIntegerVector) terms.getAttributeValue("response");
Formula formula = FormulaUtil.createFormula(terms, context, encoder);
// Dependent variable
int responseIndex = response.asScalar();
if (responseIndex != 0) {
DataField dataField = (DataField) formula.getField(responseIndex - 1);
encoder.setLabel(dataField);
} else {
throw new IllegalArgumentException();
}
String interceptName = getInterceptName();
// Independent variables
List<String> coefficientNames = getCoefficientNames();
for (String coefficientName : coefficientNames) {
if ((interceptName).equals(coefficientName)) {
continue;
}
Feature feature = formula.resolveFeature(coefficientName);
encoder.addFeature(feature);
}
this.formula = formula;
}
use of org.dmg.pmml.DataField in project jpmml-r by jpmml.
the class LRMConverter method encodeSchema.
@Override
public void encodeSchema(RExpEncoder encoder) {
RGenericVector lrm = getObject();
RIntegerVector freq = (RIntegerVector) lrm.getValue("freq");
RStringVector freqNames = freq.dimnames(0);
super.encodeSchema(encoder);
Label label = encoder.getLabel();
DataField dataField = (DataField) encoder.toCategorical(label.getName(), freqNames.getValues());
encoder.setLabel(dataField);
}
use of org.dmg.pmml.DataField in project jpmml-r by jpmml.
the class MVRConverter method encodeSchema.
@Override
public void encodeSchema(RExpEncoder encoder) {
RGenericVector mvr = getObject();
RDoubleVector coefficients = (RDoubleVector) mvr.getValue("coefficients");
RDoubleVector scale = (RDoubleVector) mvr.getValue("scale", true);
RExp terms = mvr.getValue("terms");
final RGenericVector model = (RGenericVector) mvr.getValue("model");
RStringVector rowNames = coefficients.dimnames(0);
RStringVector columnNames = coefficients.dimnames(1);
FormulaContext context = new ModelFrameFormulaContext(model);
Formula formula = FormulaUtil.createFormula(terms, context, encoder);
// Dependent variable
{
FieldName name = FieldName.create(columnNames.asScalar());
DataField dataField = (DataField) encoder.getField(name);
encoder.setLabel(dataField);
}
// Independent variables
for (int i = 0; i < rowNames.size(); i++) {
String rowName = rowNames.getValue(i);
Feature feature = formula.resolveFeature(rowName);
if (scale != null) {
feature = feature.toContinuousFeature();
Apply apply = PMMLUtil.createApply("/", feature.ref(), PMMLUtil.createConstant(scale.getValue(i)));
DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("scale", feature), OpType.CONTINUOUS, DataType.DOUBLE, apply);
feature = new ContinuousFeature(encoder, derivedField);
}
encoder.addFeature(feature);
}
}
Aggregations