Evaluation evaluation = new Evaluation(trainingDataset); evaluation.evaluateModel(targetFunction, validationDataset); System.out.println(evaluation.toSummaryString("Results", false));
The examples above uses linear regression, which predicts a numeric-valued output such as a house price based on input values. Linear regression supports the prediction of continuous, numeric values. To predict binary Yes/No values or classifiers, you could use a machine learning algorithm such as decision tree, neural network, or logistic regression:
// using logistic regression Classifier targetFunction = new Logistic(); targetFunction.buildClassifier(trainingSet);
You might use one of these learning algorithms to predict whether an email was spam or ham, or to predict whether a house for sale could be a top-seller or not. If you wanted to train your algorithm to predict whether a house is likely to sell quickly, you would need to label your example records with a new classifying label such as
// using topseller label attribute instead price label attribute ArrayList<String> classVal = new ArrayList<>(); classVal.add("true"); classVal.add("false"); Attribute topsellerAttribute = new Attribute("topsellerLabel", classVal); attributes.add(topsellerAttribute);
This training set could be used to train a new prediction classifier:
topseller. Once trained, the prediction call will return the class label index, which can be used to get the predicted value:
int idx = (int) targetFunction.classifyInstance(unlabeledInstances.get(0)); String prediction = classVal.get(idx);
Although machine learning is closely related to statistics and uses many mathematical concepts, machine learning tools make it possible to start integrating machine learning into your programs without knowing a great deal about mathematics. That said, the better you understand the inner working of machine learning algorithms such as linear regression, which we explored in this article, the more you will be able to choose the right algorithm and configure it for optimal performance.
Sign up for Computerworld eNewsletters.