Search in sources :

Example 1 with AbstractSpecifCreator

use of ml.shifu.shifu.core.pmml.builder.creator.AbstractSpecifCreator in project shifu by ShifuML.

the class PMMLTranslator method build.

public PMML build(List<BasicML> basicMLs) {
    if (basicMLs == null || basicMLs.size() == 0) {
        throw new IllegalArgumentException("Input ml model list is empty.");
    }
    PMML pmml = new PMML();
    // create and add header
    Header header = new Header();
    pmml.setHeader(header);
    header.setCopyright(" Copyright [2013-2018] PayPal Software Foundation\n" + "\n" + " Licensed under the Apache License, Version 2.0 (the \"License\");\n" + " you may not use this file except in compliance with the License.\n" + " You may obtain a copy of the License at\n" + "\n" + "    http://www.apache.org/licenses/LICENSE-2.0\n" + "\n" + " Unless required by applicable law or agreed to in writing, software\n" + " distributed under the License is distributed on an \"AS IS\" BASIS,\n" + " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + " See the License for the specific language governing permissions and\n" + " limitations under the License.\n");
    Application application = new Application();
    header.setApplication(application);
    application.setName("shifu");
    String findContainingJar = JarManager.findContainingJar(TreeEnsemblePMMLTranslator.class);
    JarFile jar = null;
    try {
        jar = new JarFile(findContainingJar);
        final Manifest manifest = jar.getManifest();
        String version = manifest.getMainAttributes().getValue("version");
        application.setVersion(version);
    } catch (Exception e) {
        LOG.warn(e.getMessage());
    } finally {
        if (jar != null) {
            try {
                jar.close();
            } catch (IOException e) {
                LOG.warn(e.getMessage());
            }
        }
    }
    // create and set data dictionary for all bagging models
    pmml.setDataDictionary(this.dataDictionaryCreator.build(null));
    if (isOutBaggingToOne) {
        MiningModel miningModel = new MiningModel();
        miningModel.setMiningSchema(this.miningSchemaCreator.build(null));
        miningModel.setMiningFunction(MiningFunction.fromValue("regression"));
        miningModel.setTargets(((NNPmmlModelCreator) this.modelCreator).createTargets());
        AbstractSpecifCreator minningModelCreator = new MiningModelPmmlCreator(this.specifCreator.getModelConfig(), this.specifCreator.getColumnConfigList());
        minningModelCreator.build(null, miningModel);
        Segmentation seg = new Segmentation();
        miningModel.setSegmentation(seg);
        seg.setMultipleModelMethod(MultipleModelMethod.AVERAGE);
        List<Segment> list = seg.getSegments();
        int idCount = 0;
        for (BasicML basicML : basicMLs) {
            // create model element
            Model tmpmodel = this.modelCreator.build(basicML);
            // create mining schema
            tmpmodel.setMiningSchema(this.miningSchemaCreator.build(basicML));
            // create variable statistical info
            tmpmodel.setModelStats(this.modelStatsCreator.build(basicML));
            // create variable transform
            tmpmodel.setLocalTransformations(this.localTransformationsCreator.build(basicML));
            this.specifCreator.build(basicML, tmpmodel, idCount);
            Segment segment = new Segment();
            segment.setId("Segement" + String.valueOf(idCount));
            segment.setPredicate(new True());
            segment.setModel(tmpmodel);
            list.add(segment);
            idCount++;
        }
        List<Model> models = pmml.getModels();
        models.add(miningModel);
    } else {
        BasicML basicML = basicMLs.get(0);
        // create model element
        Model model = this.modelCreator.build(basicML);
        // create mining schema
        model.setMiningSchema(this.miningSchemaCreator.build(basicML));
        // create variable statistical info
        model.setModelStats(this.modelStatsCreator.build(basicML));
        // create variable transform
        model.setLocalTransformations(this.localTransformationsCreator.build(basicML));
        this.specifCreator.build(basicML, model);
        pmml.addModels(model);
    }
    return pmml;
}
Also used : Segmentation(org.dmg.pmml.mining.Segmentation) AbstractSpecifCreator(ml.shifu.shifu.core.pmml.builder.creator.AbstractSpecifCreator) True(org.dmg.pmml.True) BasicML(org.encog.ml.BasicML) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) IOException(java.io.IOException) Segment(org.dmg.pmml.mining.Segment) MiningModelPmmlCreator(ml.shifu.shifu.core.pmml.builder.impl.MiningModelPmmlCreator) Header(org.dmg.pmml.Header) MiningModel(org.dmg.pmml.mining.MiningModel) Model(org.dmg.pmml.Model) MiningModel(org.dmg.pmml.mining.MiningModel) PMML(org.dmg.pmml.PMML) Application(org.dmg.pmml.Application)

Example 2 with AbstractSpecifCreator

use of ml.shifu.shifu.core.pmml.builder.creator.AbstractSpecifCreator in project shifu by ShifuML.

the class PMMLConstructorFactory method produce.

public static PMMLTranslator produce(ModelConfig modelConfig, List<ColumnConfig> columnConfigList, boolean isConcise, boolean isOutBaggingToOne) {
    AbstractPmmlElementCreator<Model> modelCreator = null;
    AbstractSpecifCreator specifCreator = null;
    if (ModelTrainConf.ALGORITHM.NN.name().equalsIgnoreCase(modelConfig.getTrain().getAlgorithm())) {
        modelCreator = new NNPmmlModelCreator(modelConfig, columnConfigList, isConcise);
        specifCreator = new NNSpecifCreator(modelConfig, columnConfigList);
    } else if (ModelTrainConf.ALGORITHM.LR.name().equalsIgnoreCase(modelConfig.getTrain().getAlgorithm())) {
        modelCreator = new RegressionPmmlModelCreator(modelConfig, columnConfigList, isConcise);
        specifCreator = new RegressionSpecifCreator(modelConfig, columnConfigList);
    } else if (ModelTrainConf.ALGORITHM.GBT.name().equalsIgnoreCase(modelConfig.getTrain().getAlgorithm()) || ModelTrainConf.ALGORITHM.RF.name().equalsIgnoreCase(modelConfig.getTrain().getAlgorithm())) {
        TreeEnsemblePmmlCreator gbtmodelCreator = new TreeEnsemblePmmlCreator(modelConfig, columnConfigList);
        AbstractPmmlElementCreator<DataDictionary> dataDictionaryCreator = new DataDictionaryCreator(modelConfig, columnConfigList);
        AbstractPmmlElementCreator<MiningSchema> miningSchemaCreator = new TreeModelMiningSchemaCreator(modelConfig, columnConfigList);
        return new TreeEnsemblePMMLTranslator(gbtmodelCreator, dataDictionaryCreator, miningSchemaCreator);
    } else {
        throw new RuntimeException("Model not supported: " + modelConfig.getTrain().getAlgorithm());
    }
    AbstractPmmlElementCreator<DataDictionary> dataDictionaryCreator = new DataDictionaryCreator(modelConfig, columnConfigList, isConcise);
    AbstractPmmlElementCreator<MiningSchema> miningSchemaCreator = new MiningSchemaCreator(modelConfig, columnConfigList, isConcise);
    AbstractPmmlElementCreator<ModelStats> modelStatsCreator = new ModelStatsCreator(modelConfig, columnConfigList, isConcise);
    AbstractPmmlElementCreator<LocalTransformations> localTransformationsCreator = null;
    ModelNormalizeConf.NormType normType = modelConfig.getNormalizeType();
    if (normType.equals(ModelNormalizeConf.NormType.WOE) || normType.equals(ModelNormalizeConf.NormType.WEIGHT_WOE)) {
        localTransformationsCreator = new WoeLocalTransformCreator(modelConfig, columnConfigList, isConcise);
    } else if (normType == ModelNormalizeConf.NormType.WOE_ZSCORE || normType == ModelNormalizeConf.NormType.WOE_ZSCALE) {
        localTransformationsCreator = new WoeZscoreLocalTransformCreator(modelConfig, columnConfigList, isConcise, false);
    } else if (normType == ModelNormalizeConf.NormType.WEIGHT_WOE_ZSCORE || normType == ModelNormalizeConf.NormType.WEIGHT_WOE_ZSCALE) {
        localTransformationsCreator = new WoeZscoreLocalTransformCreator(modelConfig, columnConfigList, isConcise, true);
    } else if (normType == ModelNormalizeConf.NormType.ZSCALE_ONEHOT) {
        localTransformationsCreator = new ZscoreOneHotLocalTransformCreator(modelConfig, columnConfigList, isConcise);
    } else {
        localTransformationsCreator = new ZscoreLocalTransformCreator(modelConfig, columnConfigList, isConcise);
    }
    return new PMMLTranslator(modelCreator, dataDictionaryCreator, miningSchemaCreator, modelStatsCreator, localTransformationsCreator, specifCreator, isOutBaggingToOne);
}
Also used : AbstractSpecifCreator(ml.shifu.shifu.core.pmml.builder.creator.AbstractSpecifCreator) TreeEnsemblePMMLTranslator(ml.shifu.shifu.core.pmml.TreeEnsemblePMMLTranslator) ModelNormalizeConf(ml.shifu.shifu.container.obj.ModelNormalizeConf) DataDictionary(org.dmg.pmml.DataDictionary) PMMLTranslator(ml.shifu.shifu.core.pmml.PMMLTranslator) TreeEnsemblePMMLTranslator(ml.shifu.shifu.core.pmml.TreeEnsemblePMMLTranslator) LocalTransformations(org.dmg.pmml.LocalTransformations) MiningSchema(org.dmg.pmml.MiningSchema) ModelStats(org.dmg.pmml.ModelStats) Model(org.dmg.pmml.Model)

Aggregations

AbstractSpecifCreator (ml.shifu.shifu.core.pmml.builder.creator.AbstractSpecifCreator)2 Model (org.dmg.pmml.Model)2 IOException (java.io.IOException)1 JarFile (java.util.jar.JarFile)1 Manifest (java.util.jar.Manifest)1 ModelNormalizeConf (ml.shifu.shifu.container.obj.ModelNormalizeConf)1 PMMLTranslator (ml.shifu.shifu.core.pmml.PMMLTranslator)1 TreeEnsemblePMMLTranslator (ml.shifu.shifu.core.pmml.TreeEnsemblePMMLTranslator)1 MiningModelPmmlCreator (ml.shifu.shifu.core.pmml.builder.impl.MiningModelPmmlCreator)1 Application (org.dmg.pmml.Application)1 DataDictionary (org.dmg.pmml.DataDictionary)1 Header (org.dmg.pmml.Header)1 LocalTransformations (org.dmg.pmml.LocalTransformations)1 MiningSchema (org.dmg.pmml.MiningSchema)1 ModelStats (org.dmg.pmml.ModelStats)1 PMML (org.dmg.pmml.PMML)1 True (org.dmg.pmml.True)1 MiningModel (org.dmg.pmml.mining.MiningModel)1 Segment (org.dmg.pmml.mining.Segment)1 Segmentation (org.dmg.pmml.mining.Segmentation)1