Java Code Examples for org.dmg.pmml.Visitor#applyTo()

The following examples show how to use org.dmg.pmml.Visitor#applyTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RandomForestConverter.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
private <P extends Number> TreeModel encodeTreeModel(MiningFunction miningFunction, ScoreEncoder<P> scoreEncoder, List<? extends Number> leftDaughter, List<? extends Number> rightDaughter, List<P> nodepred, List<? extends Number> bestvar, List<Double> xbestsplit, Schema schema){
	RGenericVector randomForest = getObject();

	Node root = encodeNode(True.INSTANCE, 0, scoreEncoder, leftDaughter, rightDaughter, bestvar, xbestsplit, nodepred, new CategoryManager(), schema);

	TreeModel treeModel = new TreeModel(miningFunction, ModelUtil.createMiningSchema(schema.getLabel()), root)
		.setMissingValueStrategy(TreeModel.MissingValueStrategy.NULL_PREDICTION)
		.setSplitCharacteristic(TreeModel.SplitCharacteristic.BINARY_SPLIT);

	if(this.compact){
		Visitor visitor = new RandomForestCompactor();

		visitor.applyTo(treeModel);
	}

	return treeModel;
}
 
Example 2
Source File: GBDT.java    From jpmml-lightgbm with GNU Affero General Public License v3.0 5 votes vote down vote up
public MiningModel encodeMiningModel(Map<String, ?> options, Schema schema){
	Boolean compact = (Boolean)options.get(HasLightGBMOptions.OPTION_COMPACT);
	Integer numIterations = (Integer)options.get(HasLightGBMOptions.OPTION_NUM_ITERATION);

	MiningModel miningModel = this.object_function_.encodeMiningModel(Arrays.asList(this.models_), numIterations, schema)
		.setAlgorithmName("LightGBM");

	if((Boolean.TRUE).equals(compact)){
		Visitor visitor = new TreeModelCompactor();

		visitor.applyTo(miningModel);
	}

	return miningModel;
}
 
Example 3
Source File: Learner.java    From jpmml-xgboost with GNU Affero General Public License v3.0 5 votes vote down vote up
public MiningModel encodeMiningModel(Map<String, ?> options, Schema schema){
	Boolean compact = (Boolean)options.get(HasXGBoostOptions.OPTION_COMPACT);
	Integer ntreeLimit = (Integer)options.get(HasXGBoostOptions.OPTION_NTREE_LIMIT);

	MiningModel miningModel = this.gbtree.encodeMiningModel(this.obj, this.base_score, ntreeLimit, schema)
		.setAlgorithmName("XGBoost (" + this.gbtree.getAlgorithmName() + ")");

	if((Boolean.TRUE).equals(compact)){
		Visitor visitor = new TreeModelCompactor();

		visitor.applyTo(miningModel);
	}

	return miningModel;
}
 
Example 4
Source File: VisitorBattery.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void applyTo(Visitable visitable){
	List<Class<? extends Visitor>> visitorClazzes = this;

	for(Class<? extends Visitor> visitorClazz : visitorClazzes){
		Visitor visitor;

		try {
			visitor = visitorClazz.newInstance();
		} catch(ReflectiveOperationException roe){
			throw new RuntimeException(roe);
		}

		visitor.applyTo(visitable);
	}
}
 
Example 5
Source File: CopyExample.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public PMML transform(PMML pmml) throws Exception {

	if(this.summary){
		printSummary(pmml);
	}

	List<String> visitorClasses = this.visitorClasses;
	for(String visitorClass : visitorClasses){
		Class<?> clazz = Class.forName(visitorClass);

		long begin = System.currentTimeMillis();

		Visitor visitor = (Visitor)clazz.newInstance();
		visitor.applyTo(pmml);

		long end = System.currentTimeMillis();

		System.out.println("Applied " + clazz.getName() + " in " + (end - begin) + " ms.");

		if(this.summary){
			printSummary(pmml);
		}
	}

	return pmml;
}
 
Example 6
Source File: LoadingModelEvaluatorBuilder.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
public LoadingModelEvaluatorBuilder load(InputStream is, String modelName) throws SAXException, JAXBException {
	Schema schema = getSchema();
	ValidationEventHandler validationEventHandler = getValidationEventHandler();
	List<? extends XMLFilter> filters = getFilters();
	boolean locatable = getLocatable();
	VisitorBattery visitors = getVisitors();

	Unmarshaller unmarshaller = JAXBUtil.createUnmarshaller();
	unmarshaller.setSchema(schema);
	unmarshaller.setEventHandler(validationEventHandler);

	if(filters == null){
		filters = Collections.singletonList(new ImportFilter());
	}

	Source source = SAXUtil.createFilteredSource(is, filters.toArray(new XMLFilter[filters.size()]));

	PMML pmml = (PMML)unmarshaller.unmarshal(source);

	Visitor locatorHandler = (locatable ? new LocatorTransformer() : new LocatorNullifier());

	locatorHandler.applyTo(pmml);

	if(visitors != null && !visitors.isEmpty()){
		visitors.applyTo(pmml);
	}

	Model model = PMMLUtil.findModel(pmml, modelName);

	setPMML(pmml);
	setModel(model);

	return this;
}
 
Example 7
Source File: IntegrationTestBatch.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void validatePMML(PMML pmml) throws Exception {
	List<Visitor> visitors = Arrays.<Visitor>asList(
		new UnsupportedMarkupInspector(),
		new InvalidMarkupInspector(){

			@Override
			public VisitorAction visit(Application application){
				String name = application.getName();

				if(name == null){
					return VisitorAction.SKIP;
				}

				return super.visit(application);
			}

			@Override
			public VisitorAction visit(MiningSchema miningSchema){

				if(!miningSchema.hasMiningFields()){
					return VisitorAction.SKIP;
				}

				return super.visit(miningSchema);
			}
		}
	);

	for(Visitor visitor : visitors){
		visitor.applyTo(pmml);
	}
}
 
Example 8
Source File: GBDT.java    From jpmml-lightgbm with GNU Affero General Public License v3.0 4 votes vote down vote up
public PMML encodePMML(Map<String, ?> options, FieldName targetField, List<String> targetCategories){
	LightGBMEncoder encoder = new LightGBMEncoder();

	Boolean nanAsMissing = (Boolean)options.get(HasLightGBMOptions.OPTION_NAN_AS_MISSING);

	Schema schema = encodeSchema(targetField, targetCategories, encoder);

	MiningModel miningModel = encodeMiningModel(options, schema);

	PMML pmml = encoder.encodePMML(miningModel);

	if((Boolean.TRUE).equals(nanAsMissing)){
		Visitor visitor = new NaNAsMissingDecorator();

		visitor.applyTo(pmml);
	}

	return pmml;
}