use of org.dmg.pmml.Model in project pyramid by cheng-li.
the class MiningModelUtil method createModelChain.
public static MiningModel createModelChain(List<? extends Model> models, Schema schema) {
if (models.size() < 1) {
throw new IllegalArgumentException();
}
Segmentation segmentation = createSegmentation(Segmentation.MultipleModelMethod.MODEL_CHAIN, models);
Model lastModel = Iterables.getLast(models);
MiningModel miningModel = new MiningModel(lastModel.getMiningFunction(), ModelUtil.createMiningSchema(schema.getLabel())).setMathContext(ModelUtil.simplifyMathContext(lastModel.getMathContext())).setSegmentation(segmentation);
return miningModel;
}
use of org.dmg.pmml.Model in project pyramid by cheng-li.
the class MiningModelUtil method createSegmentation.
public static Segmentation createSegmentation(Segmentation.MultipleModelMethod multipleModelMethod, List<? extends Model> models, List<? extends Number> weights) {
if ((weights != null) && (models.size() != weights.size())) {
throw new IllegalArgumentException();
}
List<Segment> segments = new ArrayList<>();
for (int i = 0; i < models.size(); i++) {
Model model = models.get(i);
Number weight = (weights != null ? weights.get(i) : null);
Segment segment = new Segment().setId(String.valueOf(i + 1)).setPredicate(new True()).setModel(model);
if (weight != null && !ValueUtil.isOne(weight)) {
segment.setWeight(ValueUtil.asDouble(weight));
}
segments.add(segment);
}
return new Segmentation(multipleModelMethod, segments);
}
use of org.dmg.pmml.Model 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);
}
use of org.dmg.pmml.Model 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;
}
use of org.dmg.pmml.Model in project shifu by ShifuML.
the class RegressionPmmlModelCreator method build.
@Override
public Model build(BasicML basicML) {
Model model = new RegressionModel();
model.setTargets(createTargets());
return model;
}
Aggregations