Skip to content

Latest commit

 

History

History

superml-pmml

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

SuperML PMML Module

Overview

The SuperML PMML (Predictive Model Markup Language) module provides comprehensive functionality to export trained SuperML models to the standard PMML format. This enables model deployment and interoperability across different ML platforms and systems.

Features

✅ Supported Model Types

  • LinearRegression - Ordinary Least Squares regression
  • LogisticRegression - Binary and multiclass classification
  • Ridge - L2 regularized linear regression
  • Lasso - L1 regularized linear regression
  • DecisionTree - Classification and regression trees
  • RandomForest - Ensemble of decision trees

✅ PMML Capabilities

  • Full PMML 4.4 compliance with proper schema validation
  • Comprehensive metadata including headers, timestamps, and model provenance
  • Feature name mapping with custom field names and data types
  • Model parameter extraction including coefficients, tree structures, and ensemble configurations
  • Validation functionality to ensure PMML correctness
  • Cross-platform compatibility for deployment to PMML consumers

Quick Start

Basic Usage

import org.superml.pmml.PMMLConverter;
import org.superml.linear_model.LinearRegression;

// Train a model
LinearRegression model = new LinearRegression();
model.fit(X, y);

// Convert to PMML
PMMLConverter converter = new PMMLConverter();
String pmmlXml = converter.convertToXML(model);

// Validate the PMML
boolean isValid = converter.validatePMML(pmmlXml);
System.out.println("PMML is valid: " + isValid);

Advanced Usage with Custom Features

// Custom feature names and target
String[] featureNames = {"age", "income", "education"};
String targetName = "salary";

String pmmlXml = converter.convertToXML(model, featureNames, targetName);

// Save to file for deployment
Files.write(Paths.get("model.pmml"), pmmlXml.getBytes());

API Reference

PMMLConverter Class

Methods

String convertToXML(Object model)

  • Converts a SuperML model to PMML XML format with default feature names
  • Parameters: model - Trained SuperML model (must extend BaseEstimator)
  • Returns: PMML XML string
  • Throws: IllegalArgumentException for unsupported models

String convertToXML(Object model, String[] featureNames, String targetName)

  • Converts a SuperML model to PMML XML with custom field names
  • Parameters:
    • model - Trained SuperML model
    • featureNames - Array of feature names (null for default)
    • targetName - Target variable name
  • Returns: PMML XML string

boolean validatePMML(String pmmlXml)

  • Validates PMML XML against schema requirements
  • Parameters: pmmlXml - PMML XML string to validate
  • Returns: true if valid, false otherwise

Object convertFromXML(String pmmlXml)

  • Converts PMML XML back to SuperML model (Not yet implemented)
  • Throws: UnsupportedOperationException

Model-Specific PMML Mapping

Linear Models (LinearRegression, Ridge, Lasso)

<RegressionModel functionName="regression">
  <RegressionTable intercept="1.23">
    <NumericPredictor name="feature_0" coefficient="2.45"/>
    <NumericPredictor name="feature_1" coefficient="-0.67"/>
  </RegressionTable>
</RegressionModel>

Key Features:

  • Extracts coefficients via model.getCoefficients()
  • Includes intercept via model.getIntercept()
  • Lasso automatically excludes zero coefficients
  • Ridge includes regularization metadata

Logistic Regression

<RegressionModel functionName="classification" normalizationMethod="logit">
  <RegressionTable intercept="0.5" targetCategory="1">
    <NumericPredictor name="age" coefficient="0.123"/>
    <NumericPredictor name="income" coefficient="-0.045"/>
  </RegressionTable>
</RegressionModel>

Key Features:

  • Supports binary and multiclass classification
  • Includes class labels from model.getClasses()
  • Uses logit normalization for proper probability interpretation

Decision Trees

<TreeModel functionName="regression">
  <Node>
    <Node>
      <SimplePredicate field="age" operator="lessOrEqual" value="35.0"/>
      <Node score="45000"/>
    </Node>
    <Node>
      <SimplePredicate field="age" operator="greaterThan" value="35.0"/>
      <Node score="65000"/>
    </Node>
  </Node>
</TreeModel>

Key Features:

  • Recursive tree structure conversion from model.getTree()
  • Split conditions with feature indices and thresholds
  • Leaf nodes with prediction values
  • Supports both classification and regression

Random Forest

<MiningModel functionName="classification" multipleModelMethod="majorityVote">
  <Segmentation multipleModelMethod="majorityVote">
    <Segment id="tree_0">
      <TreeModel functionName="classification">
        <!-- Individual tree structure -->
      </TreeModel>
    </Segment>
    <!-- Additional tree segments -->
  </Segmentation>
</MiningModel>

Key Features:

  • Ensemble representation using MiningModel
  • Individual trees as segments
  • Majority vote for classification, averaging for regression
  • Bootstrap sampling metadata (when available)

PMML Structure

Generated PMML includes:

  1. Header Section

    <Header>
      <Application name="SuperML Java Framework" version="3.1.2"/>
      <Timestamp>2024-01-15 10:30:00</Timestamp>
    </Header>
  2. Data Dictionary

    <DataDictionary>
      <DataField name="feature_0" optype="continuous" dataType="double"/>
      <DataField name="target" optype="continuous" dataType="double"/>
    </DataDictionary>
  3. Mining Schema

    <MiningSchema>
      <MiningField name="feature_0" usageType="active"/>
      <MiningField name="target" usageType="target"/>
    </MiningSchema>
  4. Model-Specific Elements (varies by algorithm)

Dependencies

The PMML module uses the following dependencies (defined in pom.xml):

<dependency>
    <groupId>org.jpmml</groupId>
    <artifactId>pmml-model</artifactId>
    <version>1.6.4</version>
</dependency>
<dependency>
    <groupId>org.jpmml</groupId>
    <artifactId>pmml-evaluator</artifactId>
    <version>1.6.4</version>
</dependency>
<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>3.0.2</version>
    <scope>runtime</scope>
</dependency>

Examples and Testing

PMML Conversion Examples

For comprehensive usage examples, see the SuperML Examples module:

  • File: superml-examples/src/main/java/org/superml/examples/PMMLConversionExample.java
  • Features: Business and technical model examples, deployment scenarios, validation demonstrations

Testing and Validation

For testing and validation demos, see the SuperML Test Cases module:

  • Files: superml-testcases/src/test/java/org/superml/pmml/
    • PMMLConverterDemo.java - Basic functionality demonstration
    • PMMLIntegrationTest.java - Integration testing with mock models

Building and Testing

Build the Module

# From the superml-pmml directory
mvn clean compile

# Or build all modules including dependencies
mvn clean compile -pl superml-core,superml-linear-models,superml-tree-models,superml-pmml

Run the Demo

# From the superml-testcases directory
cd ../superml-testcases
mvn compile test-compile

# Run the PMML converter demo
java -cp "target/test-classes:../superml-pmml/target/classes:../superml-pmml/target/dependency/*" \
     org.superml.pmml.PMMLConverterDemo

Run Examples

# From the superml-examples directory  
cd ../superml-examples
mvn compile

# Run the PMML conversion example
java -cp "target/classes:../superml-pmml/target/classes:../superml-pmml/target/dependency/*" \
     org.superml.examples.PMMLConversionExample

Expected Demo Output (from superml-testcases)

=== SuperML PMML Converter Demo ===

1. Testing PMML Validation
===========================
✓ null input: INVALID
✓ empty string: INVALID
✓ whitespace only: INVALID
✓ malformed XML: INVALID
✓ valid XML but not PMML: INVALID
✓ sample PMML structure: VALID

2. Testing Error Handling
==========================
✓ Null model: Correctly threw IllegalArgumentException
✓ Unsupported model: Correctly threw IllegalArgumentException
✓ convertFromXML: Correctly threw UnsupportedOperationException

3. Testing Method Availability
===============================
✓ convertToXML(Object) method available
✓ convertToXML(Object, String[], String) method available
✓ convertFromXML(String) method available
✓ validatePMML(String) method available
✓ All required methods are available

=== Demo Complete ===

Expected Example Output (from superml-examples)

======================================================================
        SuperML PMML Conversion Examples
======================================================================

🔄 1. Basic PMML Conversion
========================================
📈 Linear Regression to PMML:
   ✓ PMML generated: 2,450 characters
   ✓ Validation result: VALID
   ✓ Model type: Linear Regression

🎯 Logistic Regression to PMML:
   ✓ PMML generated: 2,650 characters  
   ✓ Validation result: VALID
   ✓ Model type: Logistic Regression

⚡ 2. Advanced PMML with Custom Features
========================================
💼 Business Model with Custom Features:
   ✓ Features: customer_age, annual_income, credit_score, debt_to_income_ratio
   ✓ Target: loan_approval_probability
   ✓ PMML size: 2,850 characters
   ✓ Validation: PASSED

🚀 4. Production Deployment Scenarios
========================================
⚡ Spark MLlib Deployment:
   ✓ Model: Random Forest
   ✓ Features: 4
   ✓ Ready for Spark deployment: YES
   📋 Usage: Load in Spark MLlib using JPMML-SparkML

======================================================================
✅ All PMML conversion examples completed successfully!
======================================================================

Deployment Scenarios

1. Apache Spark MLlib

import org.apache.spark.ml.Pipeline
import org.jpmml.sparkml.PMMLBuilder

// Load PMML in Spark
val pmmlModel = PMMLBuilder.load("model.pmml")

2. Python scikit-learn

from sklearn2pmml import sklearn2pmml
from jpmml_evaluator import make_evaluator

# Load SuperML PMML in Python
evaluator = make_evaluator("model.pmml")
predictions = evaluator.evaluate(data)

3. R Environment

library(pmml)

# Load and evaluate PMML
model <- pmml::loadPMML("model.pmml")
predictions <- predict(model, newdata)

4. Enterprise Platforms

  • SAS Enterprise Miner - Direct PMML import
  • IBM SPSS - PMML model scoring
  • Amazon SageMaker - PMML container deployment
  • Microsoft Azure ML - PMML endpoint hosting

Limitations and Future Work

Current Limitations

  • convertFromXML not yet implemented (PMML → SuperML)
  • RandomForest uses placeholder structure (individual trees not accessible)
  • LogisticRegression coefficients not directly exposed (uses approximation)
  • Neural networks not yet supported (interfaces exist but limited implementation)

Planned Enhancements

  • Full bidirectional PMML conversion (import from PMML)
  • Support for preprocessing transformations (StandardScaler, etc.)
  • Enhanced Random Forest with actual tree extraction
  • Neural network PMML support (MLPClassifier, CNNClassifier)
  • Custom PMML extensions for SuperML-specific features
  • Performance optimization for large models

Examples and Use Cases

Example 1: Model Pipeline Deployment

// Train a complete ML pipeline
Pipeline pipeline = new Pipeline()
    .addStep("scaler", new StandardScaler())
    .addStep("classifier", new LogisticRegression());
pipeline.fit(X, y);

// Export individual components to PMML
// (Full pipeline PMML support planned for future versions)
LogisticRegression model = (LogisticRegression) pipeline.getStep("classifier");
String pmml = converter.convertToXML(model, featureNames, "outcome");

Example 2: A/B Testing with PMML

// Train multiple models for comparison
LinearRegression baseModel = new LinearRegression();
Ridge ridgeModel = new Ridge().setAlpha(0.1);

baseModel.fit(X, y);
ridgeModel.fit(X, y);

// Export both models to PMML for parallel deployment
String basePMML = converter.convertToXML(baseModel, features, "prediction");
String ridgePMML = converter.convertToXML(ridgeModel, features, "prediction");

// Deploy both models for A/B testing
savePMMLToFile(basePMML, "base_model.pmml");
savePMMLToFile(ridgePMML, "ridge_model.pmml");

Example 3: Cross-Platform Model Sharing

// Train in SuperML Java
DecisionTree javaModel = new DecisionTree().setMaxDepth(5);
javaModel.fit(trainingData, labels);

// Export to PMML
String pmml = converter.convertToXML(javaModel);

// Deploy to various platforms:
// - Python: Use jpmml-evaluator
// - R: Use pmml package  
// - Spark: Use pmml-sparkml
// - Cloud: Deploy to MLaaS platforms

Contributing

Contributions to the PMML module are welcome! Areas of particular interest:

  1. PMML Import - Implementing convertFromXML functionality
  2. Model Support - Adding support for additional SuperML algorithms
  3. Preprocessing - PMML export for data transformation steps
  4. Optimization - Performance improvements for large models
  5. Testing - Comprehensive test coverage and validation

Please follow the SuperML contribution guidelines and ensure all changes maintain PMML 4.4 compliance.

License

This module is licensed under the Apache License 2.0, consistent with the broader SuperML Java framework.