Search in sources :

Example 1 with MathContext

use of org.dmg.pmml.MathContext in project pyramid by cheng-li.

the class MiningModelUtil method createClassification.

public static MiningModel createClassification(List<? extends Model> models, RegressionModel.NormalizationMethod normalizationMethod, boolean hasProbabilityDistribution, Schema schema) {
    CategoricalLabel categoricalLabel = (CategoricalLabel) schema.getLabel();
    // modified here
    if (categoricalLabel.size() != models.size()) {
        throw new IllegalArgumentException();
    }
    if (normalizationMethod != null) {
        switch(normalizationMethod) {
            case NONE:
            case SIMPLEMAX:
            case SOFTMAX:
                break;
            default:
                throw new IllegalArgumentException();
        }
    }
    MathContext mathContext = null;
    List<RegressionTable> regressionTables = new ArrayList<>();
    for (int i = 0; i < categoricalLabel.size(); i++) {
        Model model = models.get(i);
        MathContext modelMathContext = model.getMathContext();
        if (modelMathContext == null) {
            modelMathContext = MathContext.DOUBLE;
        }
        if (mathContext == null) {
            mathContext = modelMathContext;
        } else {
            if (!Objects.equals(mathContext, modelMathContext)) {
                throw new IllegalArgumentException();
            }
        }
        Feature feature = MODEL_PREDICTION.apply(model);
        RegressionTable regressionTable = RegressionModelUtil.createRegressionTable(Collections.singletonList(feature), Collections.singletonList(1d), null).setTargetCategory(categoricalLabel.getValue(i));
        regressionTables.add(regressionTable);
    }
    RegressionModel regressionModel = new RegressionModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel), regressionTables).setNormalizationMethod(normalizationMethod).setMathContext(ModelUtil.simplifyMathContext(mathContext)).setOutput(hasProbabilityDistribution ? ModelUtil.createProbabilityOutput(mathContext, categoricalLabel) : null);
    List<Model> segmentationModels = new ArrayList<>(models);
    segmentationModels.add(regressionModel);
    return createModelChain(segmentationModels, schema);
}
Also used : CategoricalLabel(org.jpmml.converter.CategoricalLabel) ArrayList(java.util.ArrayList) Model(org.dmg.pmml.Model) MiningModel(org.dmg.pmml.mining.MiningModel) RegressionModel(org.dmg.pmml.regression.RegressionModel) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Feature(org.jpmml.converter.Feature) MathContext(org.dmg.pmml.MathContext) RegressionTable(org.dmg.pmml.regression.RegressionTable) RegressionModel(org.dmg.pmml.regression.RegressionModel)

Example 2 with MathContext

use of org.dmg.pmml.MathContext in project drools by kiegroup.

the class KiePMMLUtilTest method getTargetDataType.

@Test
public void getTargetDataType() {
    MiningFunction miningFunction = MiningFunction.REGRESSION;
    MathContext mathContext = MathContext.DOUBLE;
    DataType retrieved = KiePMMLUtil.getTargetDataType(miningFunction, mathContext);
    assertEquals(DataType.DOUBLE, retrieved);
    mathContext = MathContext.FLOAT;
    retrieved = KiePMMLUtil.getTargetDataType(miningFunction, mathContext);
    assertEquals(DataType.FLOAT, retrieved);
    miningFunction = MiningFunction.CLASSIFICATION;
    retrieved = KiePMMLUtil.getTargetDataType(miningFunction, mathContext);
    assertEquals(DataType.STRING, retrieved);
    miningFunction = MiningFunction.CLUSTERING;
    retrieved = KiePMMLUtil.getTargetDataType(miningFunction, mathContext);
    assertEquals(DataType.STRING, retrieved);
    List<MiningFunction> notMappedMiningFunctions = Arrays.asList(MiningFunction.ASSOCIATION_RULES, MiningFunction.MIXED, MiningFunction.SEQUENCES, MiningFunction.TIME_SERIES);
    notMappedMiningFunctions.forEach(minFun -> assertNull(KiePMMLUtil.getTargetDataType(minFun, MathContext.DOUBLE)));
}
Also used : DataType(org.dmg.pmml.DataType) MiningFunction(org.dmg.pmml.MiningFunction) MathContext(org.dmg.pmml.MathContext) Test(org.junit.Test)

Aggregations

MathContext (org.dmg.pmml.MathContext)2 ArrayList (java.util.ArrayList)1 DataType (org.dmg.pmml.DataType)1 MiningFunction (org.dmg.pmml.MiningFunction)1 Model (org.dmg.pmml.Model)1 MiningModel (org.dmg.pmml.mining.MiningModel)1 RegressionModel (org.dmg.pmml.regression.RegressionModel)1 RegressionTable (org.dmg.pmml.regression.RegressionTable)1 CategoricalLabel (org.jpmml.converter.CategoricalLabel)1 ContinuousFeature (org.jpmml.converter.ContinuousFeature)1 Feature (org.jpmml.converter.Feature)1 Test (org.junit.Test)1