This documentation is for Machine Learner 1.0.0. View documentation for the latest release.
Skip to end of metadata
Go to start of metadata


This guide demonstrates how to use a downloaded ML model generated using WSO2 ML in plain Java code. This guide uses the sample Java client implementation.


Before you run this sample, do the following.

  1. Download WSO2 ML, and start the server. For instructions, see Getting Started.
  2. Generate a model using WSO2 ML. This model will be used to make the predictions. For instructions on generating a model, see Generating Models. To build a model, see ML UI Workflow.
  3. Download the generated model.

You can stop the WSO2 ML server after generating the required model, since this process of using the model in a Java client does not require a running ML server.

Building the sample

To build this sample, a Java project should be created as follows.

  1. Create a Maven project.
  2. Include the following compile time dependencies in the pom.xml file of the project.

  3. Include the following runtime dependencies also in the pom.xml file of the project.

    <!--Dependencies required for MLModel deserialization-->
    <!--Dependencies required for predictions-->
  4. The downloaded model contains a serialized MLModel object. Deserialize the MLModel as follows.

    private MLModel deserializeMLModel() throws IOException, ClassNotFoundException, URISyntaxException {
      // Path to downloaded-ml-model
      String pathToDownloadedModel = “/tmp/downloaded-ml-model”;
      FileInputStream fileInputStream = new FileInputStream(pathToDownloadedModel);
      ObjectInputStream in = new ObjectInputStream(fileInputStream);
      MLModel mlModel = (MLModel) in.readObject();
 "Algorithm Type : " + mlModel.getAlgorithmClass());"Algorithm Name : " + mlModel.getAlgorithmName());"Response Variable : " + mlModel.getResponseVariable());"Features : " + mlModel.getFeatures());
      return mlModel;
  5. Use to make predictions using the downloaded ML Model. Predictor accepts a list of string arrays as feature values. The feature values should be in the order of the feature index inside the array as shown below.

    MLModelUsageSample modelUsageSample = new MLModelUsageSample();
    // Deserialize
    MLModel mlModel = modelUsageSample.deserializeMLModel();
    // Predict
    String[] featureValueArray1 = new String[] { "6", "148", "72", "35", "0", "33.6", "0.627", "50" };
    String[] featureValueArray2 = new String[] { "0", "101", "80", "40", "0", "26", "0.5", "33" };
    ArrayList<String[]> list = new ArrayList<String[]>();
    modelUsageSample.predict(list, mlModel);
    public void predict(List<String[]> featureValueLists, MLModel mlModel) throws MLModelHandlerException {
    	Predictor predictor = new Predictor(0, mlModel, featureValueLists);
    	List<?> predictions = predictor.predict();"Predictions : " + predictions);

The complete project created above can be found at  To build this, execute the following command from the model-usage directory.

mvn clean install

Executing the sample

To execute this sample, execute the following command from the model-usage directory.

mvn exec:java

Analysing the output

The following output is displayed in the ML console once you execute the sample.

INFO  [MLModelUsageSample] - Algorithm Type : Classification
INFO  [MLModelUsageSample] - Algorithm Name : LOGISTIC_REGRESSION
INFO  [MLModelUsageSample] - Response Variable : Class
INFO  [MLModelUsageSample] - Features : [Feature [name=Age, index=7, type=NUMERICAL, imputeOption=DISCARD, include=true], Feature [name=BMI, index=5, type=NUMERICAL, imputeOption=DISCARD, include=true], Feature [name=DBP, index=2, type=NUMERICAL, imputeOption=DISCARD, include=true], Feature [name=DPF, index=6, type=NUMERICAL, imputeOption=DISCARD, include=true], Feature [name=NumPregnancies, index=0, type=NUMERICAL, imputeOption=DISCARD, include=true], Feature [name=PG2, index=1, type=NUMERICAL, imputeOption=DISCARD, include=true], Feature [name=SI2, index=4, type=NUMERICAL, imputeOption=DISCARD, include=true], Feature [name=TSFT, index=3, type=NUMERICAL, imputeOption=DISCARD, include=true]]
WARN  [] - Metric Service is not available!
Sep 24, 2015 3:06:28 PM com.github.fommil.netlib.BLAS <clinit>
WARNING: Failed to load implementation from: com.github.fommil.netlib.NativeSystemBLAS
Sep 24, 2015 3:06:28 PM com.github.fommil.netlib.BLAS <clinit>
WARNING: Failed to load implementation from: com.github.fommil.netlib.NativeRefBLAS
INFO  [MLModelUsageSample] - Feature values list 1 : [2, 84, 0, 0, 0, 0.0, 0.304, 21]
INFO  [MLModelUsageSample] - Prediction : 1
INFO  [MLModelUsageSample] - Feature values list 2 : [0, 101, 80, 40, 0, 26, 0.5, 33]
INFO  [MLModelUsageSample] - Prediction : 0

If you package this project as a JAR file, add the following JAR files that are found in the <ML_HOME>/repository/components/plugins/ directory in the classpath when executing it.

  • arpack_combined_0.1.0.wso2v1.jar
  • breeze_2.10_0.11.1.wso2v1.jar
  • commons-logging-1.2.jar
  • core_1.1.2.wso2v1.jar
  • org.scala-lang.scala-library_2.10.4.v20140209-180020-VFINAL-b66a39653b.jar
  • org.wso2.carbon.metrics.manager_1.1.0.jar
  • slf4j.api_1.7.5.jar
  • spark-core_2.10_1.4.1.wso2v1.jar
  • spark-mllib_2.10_1.4.1.wso2v1.jar
  • No labels