Search in sources :

Example 1 with Scorecard

use of org.kie.dmg.pmml.pmml_4_2.descr.Scorecard in project drools by kiegroup.

the class PMML4Compiler method checkBuildingResources.

private static KieBase checkBuildingResources(PMML pmml) throws IOException {
    KieServices ks = KieServices.Factory.get();
    KieContainer kieContainer = ks.getKieClasspathContainer();
    if (registry == null) {
        initRegistry();
    }
    String chosenKieBase = null;
    for (Object o : pmml.getAssociationModelsAndBaselineModelsAndClusteringModels()) {
        if (o instanceof NaiveBayesModel) {
            if (!naiveBayesLoaded) {
                for (String ntempl : NAIVE_BAYES_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                naiveBayesLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "PMML-Bayes" : "PMML";
        }
        if (o instanceof NeuralNetwork) {
            if (!neuralLoaded) {
                for (String ntempl : NEURAL_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                neuralLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "PMML-Neural" : "PMML";
        }
        if (o instanceof ClusteringModel) {
            if (!clusteringLoaded) {
                for (String ntempl : CLUSTERING_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                clusteringLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "PMML-Cluster" : "PMML";
        }
        if (o instanceof SupportVectorMachineModel) {
            if (!svmLoaded) {
                for (String ntempl : SVM_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                svmLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "PMML-SVM" : "PMML";
        }
        if (o instanceof TreeModel) {
            if (!treeLoaded) {
                for (String ntempl : TREE_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                treeLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "PMML-Tree" : "PMML";
        }
        if (o instanceof RegressionModel) {
            if (!simpleRegLoaded) {
                for (String ntempl : SIMPLEREG_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                simpleRegLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "PMML-Regression" : "PMML";
        }
        if (o instanceof Scorecard) {
            if (!scorecardLoaded) {
                for (String ntempl : SCORECARD_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                scorecardLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "PMML-Scorecard" : "PMML";
        }
    }
    if (chosenKieBase == null) {
        chosenKieBase = "PMML-Base";
    }
    return kieContainer.getKieBase(chosenKieBase);
}
Also used : TreeModel(org.dmg.pmml.pmml_4_2.descr.TreeModel) KieServices(org.kie.api.KieServices) NaiveBayesModel(org.dmg.pmml.pmml_4_2.descr.NaiveBayesModel) NeuralNetwork(org.dmg.pmml.pmml_4_2.descr.NeuralNetwork) SupportVectorMachineModel(org.dmg.pmml.pmml_4_2.descr.SupportVectorMachineModel) Scorecard(org.dmg.pmml.pmml_4_2.descr.Scorecard) KieContainer(org.kie.api.runtime.KieContainer) ClusteringModel(org.dmg.pmml.pmml_4_2.descr.ClusteringModel) RegressionModel(org.dmg.pmml.pmml_4_2.descr.RegressionModel)

Example 2 with Scorecard

use of org.kie.dmg.pmml.pmml_4_2.descr.Scorecard in project drools by kiegroup.

the class ExternalObjectModelTest method testPMMLCustomOutput.

@Test
public void testPMMLCustomOutput() throws Exception {
    PMML pmmlDocument = null;
    String drl = null;
    if (scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_externalmodel.xls"))) {
        pmmlDocument = scorecardCompiler.getPMMLDocument();
        assertNotNull(pmmlDocument);
        PMML4Compiler.dumpModel(pmmlDocument, System.out);
        drl = scorecardCompiler.getDRL();
        assertTrue(drl != null && !drl.isEmpty());
    // System.out.println(drl);
    } else {
        fail("failed to parse scoremodel Excel.");
    }
    for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()) {
        if (serializable instanceof Scorecard) {
            Scorecard scorecard = (Scorecard) serializable;
            for (Object obj : scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) {
                if (obj instanceof Output) {
                    Output output = (Output) obj;
                    final List<OutputField> outputFields = output.getOutputFields();
                    assertEquals(1, outputFields.size());
                    final OutputField outputField = outputFields.get(0);
                    assertNotNull(outputField);
                    assertEquals("totalScore", outputField.getName());
                    assertEquals("Final Score", outputField.getDisplayName());
                    assertEquals("double", outputField.getDataType().value());
                    assertEquals("predictedValue", outputField.getFeature().value());
                    final Extension extension = ScorecardPMMLUtils.getExtension(outputField.getExtensions(), PMMLExtensionNames.EXTERNAL_CLASS);
                    assertNotNull(extension);
                    assertEquals("org.drools.scorecards.example.Applicant", extension.getValue());
                    return;
                }
            }
        }
    }
    fail();
}
Also used : Extension(org.dmg.pmml.pmml_4_2.descr.Extension) Output(org.dmg.pmml.pmml_4_2.descr.Output) PMML(org.dmg.pmml.pmml_4_2.descr.PMML) OutputField(org.dmg.pmml.pmml_4_2.descr.OutputField) Scorecard(org.dmg.pmml.pmml_4_2.descr.Scorecard) Test(org.junit.Test)

Example 3 with Scorecard

use of org.kie.dmg.pmml.pmml_4_2.descr.Scorecard in project drools by kiegroup.

the class ScorecardReasonCodeTest method testUseReasonCodes.

@Test
public void testUseReasonCodes() throws Exception {
    final ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
    boolean compileResult = scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_reasoncodes.xls"));
    if (!compileResult) {
        assertErrors(scorecardCompiler);
    }
    final PMML pmmlDocument = scorecardCompiler.getPMMLDocument();
    for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()) {
        if (serializable instanceof Scorecard) {
            assertTrue(((Scorecard) serializable).getUseReasonCodes());
            assertEquals(100.0, ((Scorecard) serializable).getInitialScore(), 0.0);
            assertEquals("pointsBelow", ((Scorecard) serializable).getReasonCodeAlgorithm());
        }
    }
}
Also used : PMML(org.dmg.pmml.pmml_4_2.descr.PMML) Scorecard(org.dmg.pmml.pmml_4_2.descr.Scorecard) Test(org.junit.Test)

Example 4 with Scorecard

use of org.kie.dmg.pmml.pmml_4_2.descr.Scorecard in project drools by kiegroup.

the class ScorecardReasonCodeTest method testBaselineScores.

@Test
public void testBaselineScores() throws Exception {
    ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
    boolean compileResult = scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_reasoncodes.xls"));
    if (!compileResult) {
        assertErrors(scorecardCompiler);
    }
    final PMML pmmlDocument = scorecardCompiler.getPMMLDocument();
    for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()) {
        if (serializable instanceof Scorecard) {
            for (Object obj : ((Scorecard) serializable).getExtensionsAndCharacteristicsAndMiningSchemas()) {
                if (obj instanceof Characteristics) {
                    Characteristics characteristics = (Characteristics) obj;
                    assertEquals(4, characteristics.getCharacteristics().size());
                    assertEquals(10.0, characteristics.getCharacteristics().get(0).getBaselineScore(), 0.0);
                    assertEquals(99.0, characteristics.getCharacteristics().get(1).getBaselineScore(), 0.0);
                    assertEquals(12.0, characteristics.getCharacteristics().get(2).getBaselineScore(), 0.0);
                    assertEquals(15.0, characteristics.getCharacteristics().get(3).getBaselineScore(), 0.0);
                    assertEquals(25.0, ((Scorecard) serializable).getBaselineScore(), 0.0);
                    return;
                }
            }
        }
    }
    fail();
}
Also used : Characteristics(org.dmg.pmml.pmml_4_2.descr.Characteristics) PMML(org.dmg.pmml.pmml_4_2.descr.PMML) Scorecard(org.dmg.pmml.pmml_4_2.descr.Scorecard) Test(org.junit.Test)

Example 5 with Scorecard

use of org.kie.dmg.pmml.pmml_4_2.descr.Scorecard in project drools by kiegroup.

the class ScorecardReasonCodeTest method testAbsenceOfReasonCodes.

@Test
public void testAbsenceOfReasonCodes() throws Exception {
    ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
    scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_c.xls"));
    PMML pmml = scorecardCompiler.getPMMLDocument();
    for (Object serializable : pmml.getAssociationModelsAndBaselineModelsAndClusteringModels()) {
        if (serializable instanceof Scorecard) {
            assertFalse(((Scorecard) serializable).getUseReasonCodes());
        }
    }
}
Also used : PMML(org.dmg.pmml.pmml_4_2.descr.PMML) Scorecard(org.dmg.pmml.pmml_4_2.descr.Scorecard) Test(org.junit.Test)

Aggregations

Scorecard (org.dmg.pmml.pmml_4_2.descr.Scorecard)8 PMML (org.dmg.pmml.pmml_4_2.descr.PMML)6 Test (org.junit.Test)6 Characteristics (org.dmg.pmml.pmml_4_2.descr.Characteristics)3 Extension (org.dmg.pmml.pmml_4_2.descr.Extension)3 RegressionModel (org.kie.dmg.pmml.pmml_4_2.descr.RegressionModel)3 Scorecard (org.kie.dmg.pmml.pmml_4_2.descr.Scorecard)3 TreeModel (org.kie.dmg.pmml.pmml_4_2.descr.TreeModel)3 ArrayList (java.util.ArrayList)2 Attribute (org.dmg.pmml.pmml_4_2.descr.Attribute)2 Characteristic (org.dmg.pmml.pmml_4_2.descr.Characteristic)2 Output (org.dmg.pmml.pmml_4_2.descr.Output)2 KieServices (org.kie.api.KieServices)2 KieContainer (org.kie.api.runtime.KieContainer)2 MiningModel (org.kie.dmg.pmml.pmml_4_2.descr.MiningModel)2 PMML4Model (org.kie.pmml.pmml_4_2.PMML4Model)2 ClusteringModel (org.dmg.pmml.pmml_4_2.descr.ClusteringModel)1 MiningField (org.dmg.pmml.pmml_4_2.descr.MiningField)1 MiningSchema (org.dmg.pmml.pmml_4_2.descr.MiningSchema)1 NaiveBayesModel (org.dmg.pmml.pmml_4_2.descr.NaiveBayesModel)1