Skip to content

How to use Machine Learning Libraries (aiUtils)

imonacom edited this page Jul 19, 2018 · 1 revision

1- buildAndSaveClassificationModel(String sql, String optionString, String[] preprocessArray, double trainDataSizePercent, int randSeed, String descriptionKey)
Creates machine learning model and saves it to imona_weka_model table.

Parameters:
sql - Native query to get training dataset from Org database.
optionString - weka options string describes the algorithm and its parameters.
preprocessArray - preprocessing array
description - unique key description for the model.
Returns:
Instance from WekaBuildClassifierResult class.
public class WekaBuildClassifierResult {
private Long modelId;
private WekaClassifier classifier;
private String resultDescription;
private Integer resultCode;
//getter methods ...

}

public class WekaClassifier implements Serializable{
private Classifier model;
private weka.classifiers.Evaluation evaluation;
private String message;
private int totalNumberOfInstances;
private int numberOfTrainInstances;
private int numberOfTestInstances;
private int numberOfAttributes;
private double trainDataSizePercent;
private int seed;
private ArrayList<Attribute> attributes;
private Instances sampleInstances;
//getter methods ...
}

------------------------------------------------------------------------------
2- wekaClassifyInput(Long modelId, Object bean)
Classifies input based on already trained model.

Parameters:
modelId - id of the model saved in imona_weka_model table.
bean - represents an entity containing the input parameters in its fields.
Returns:
Double value represents the classification result.

------------------------------------------------------------------------------
3- wekaGetDistributionForInput(Long modelId, Object bean)
Returns distribution based on already trained model.

Parameters:
modelId - id of the model saved in imona_weka_model table.
bean - represents an entity containing the input parameters in its fields.
Returns:
Double value represents the distribution result.

------------------------------------------------------------------------------
4- wekaGetClassifier(Long modelId)
returns WekaClassifier object from imona_weka_model table.

Parameters:
modelId - id of model from imona_weka_model table.
Returns:
Instance of WekaClassifier class.

------------------------------MVEL Script Sample-------------------------

1- Build your training data-set :
var trainSetObj = nil;
for(i = 0 ; i < 100 ; i = i + 1){
trainSetObj = entityUtils().create("train_set");
trainSetObj.x = Math.random();
trainSetObj.z = Math.random();
trainSetObj.y = (2 * (trainSetObj.x) + 3 * (trainSetObj.z)) - 2;
entityUtils().save(trainSetObj);
}

2- Build you Machine-Learning Model :

var sql = "select x, z, y from train_set";
var optionString = "weka.classifiers.functions.LinearRegression -S 0 -R 1.0E-8 -num-decimal-places 8";
var preprocessArray = {};
var trainDataSizePercent = 0.66;
var randSeed = 0;
var descriptionKey = "model_key_desc";
var result = aiUtils().buildAndSaveClassificationModel(sql, optionString, preprocessArray, trainDataSizePercent, randSeed, descriptionKey);
debugUtils().debug(result.modelId);
debugUtils().debug(result.classifier);
debugUtils().debug(result.resultDescription);
debugUtils().debug(result.resultCode); // 0 : false - 1 : true

2- Classify your new input :
var inputParams = entityUtils().create("model_input_entity");
inputParams.x = 5.0;
inputParams.z = -2.0;
var myClass= aiUtils().wekaClassifyInput(Long.valueOf(1), inputParams);
debugUtils().debug(myClass);

3- Retrieve your Model object :
var wekaClass = aiUtils().wekaGetClassifier(Long.valueOf(1));
var myModel = (weka.classifiers.functions.LinearRegression)wekaClass.getModel();
var coefficients= wekaClass.coefficients();
for(c : coefficients){
debugUtils().debug(c);
}

l

Clone this wiki locally