javamachine-learningweka

How to use a WEKA model in Java for a real time prediction task?


I have trained and loaded a WEKA model into my Java program and I am trying to use it for a real-time predictive task.

I have searched online and the resources/documentation I have found involve using a pre-generated set of test instances to pass into the model.

However, in my case the data is generated on the fly in my Java program and I was wondering how I would pass the feature set into the model in an acceptable format.


Solution

  • There are several sources on how to create dataset structures on-the-fly in Weka:

    At prediction time, you can simply create weka.core.Instance (weka.core.DenseInstance or weka.core.SparseInstance) objects and pass them into the model.

    However, these still need access to the dataset structure (ie the weka.core.Instances object), as the model may query the dataset structure. Simply assign the dataset structure that the model was trained with using the setDataset(Instances) method.

    Weka serializes both, model and dataset header, by default, so you can just reuse that header as your dataset structure when loading a serialized model.