AODE aode = new AODE(data.getAttributes()) .setWindowSize(100) .setClassName(classVarName); aode.updateModel(data); for (DataOnMemory<DataInstance> batch : data.iterableOverBatches(100)) { aode.updateModel(batch); System.out.println(aode.getModel()); System.out.println(aode.getDAG()); double realValue = d.getValue(aode.getClassVar()); double predValue; d.setValue(aode.getClassVar(), Utils.missingValue()); Multinomial posteriorProb = aode.predict(d);
/** * Constructor of a classifier which is initialized with the default arguments: * the last variable in attributes is the class variable and importance sampling * is the inference algorithm for making the predictions. * * @param attributes list of attributes of the classifier (i.e. its variables) * @throws WrongConfigurationException is thrown when the attributes passed are not suitable * for such classifier */ public AODE(Attributes attributes) throws WrongConfigurationException { super(attributes); this.setLearningAlgorithm(new ParallelMaximumLikelihood()); classVariables = new ArrayList<>(); }
@Override public boolean isValidConfiguration() { boolean isValid = true; long numFinite = vars.getListOfVariables().stream() .filter( v -> v.getStateSpaceTypeEnum().equals(StateSpaceTypeEnum.FINITE_SET)) .count(); long numReal = vars.getListOfVariables().stream() .filter( v -> v.getStateSpaceTypeEnum().equals(StateSpaceTypeEnum.REAL)) .count(); if(numFinite > 1 && numReal > 0) { isValid = false; String errorMsg = "Invalid configuration: There should be at least 2 discrete variables (root and class)"; this.setErrorMessage(errorMsg); } return isValid; }
@Override public Multinomial predict(DataInstance instance) { if (!Utils.isMissingValue(instance.getValue(classVar))) System.out.println("Class Variable can not be set."); inferenceAlgoPredict.setModel(this.getModel()); this.inferenceAlgoPredict.setEvidence(instance); System.out.println(instance); this.inferenceAlgoPredict.runInference(); List<Multinomial> posteriors = new ArrayList<>(); for (Variable classVariable : classVariables) { posteriors.add(this.inferenceAlgoPredict.getPosterior(classVariable)); } double[] vectorPosteriors = new double[classVar.getNumberOfStates()]; for (Multinomial posterior : posteriors) { for (int i = 0; i < classVar.getNumberOfStates(); i++) { vectorPosteriors[i] += posterior.getParameters()[i]; } } posteriors.get(0).setProbabilities(Utils.normalize(vectorPosteriors)); return posteriors.get(0); }