/** * Constructor of the 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 NaiveBayesClassifier(Attributes attributes) throws WrongConfigurationException { super(attributes); this.setLearningAlgorithm(new ParallelMLMissingData()); }
/** * Method to set the class variable. Note that it should be multinomial * @param className String with the name of the class variable * @throws WrongConfigurationException is thrown when the variable is not a multinomial. */ public T setClassName(String className) throws WrongConfigurationException { setClassVar(vars.getVariableByName(className)); return ((T) this); }
/** * 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 HODE(Attributes attributes) throws WrongConfigurationException { super(attributes); this.setLearningAlgorithm(new ParallelMaximumLikelihood()); }
new GaussianDiscriminantAnalysis(data.getAttributes()) .setDiagonal(false) .setClassName("DiscreteVar0") .setWindowSize(100); gda.updateModel(data); for (DataOnMemory<DataInstance> batch : data.iterableOverBatches(100)) { gda.updateModel(batch); System.out.println(gda.getModel()); System.out.println(gda.getDAG()); d.setValue(gda.getClassVar(), Utils.missingValue()); Multinomial posteriorProb = gda.predict(d); System.out.println(posteriorProb.toString());
Model model = new LatentClassificationModel(data.getAttributes()) .setClassName("codrna_Y") .setNumContinuousHidden(1) .setNumStatesHidden(2) .setWindowSize(1000);
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);
NaiveBayesClassifier model = new NaiveBayesClassifier(data.getAttributes()); model.setClassName(data.getAttributes().getFullListOfAttributes().get(data.getAttributes().getFullListOfAttributes().size() - 1).getName()); model.updateModel(data); BayesianNetwork nbClassifier = model.getModel(); System.out.println(nbClassifier.toString());
public static void main(String[] args) throws WrongConfigurationException { int seed=6236; int nSamples=5000; int nDiscreteVars=5; int nContinuousVars=10; DataStream<DataInstance> data = DataSetGenerator.generate(seed,nSamples,nDiscreteVars,nContinuousVars); String classVarName="DiscreteVar0"; String rootVarName="DiscreteVar1"; TAN model = new TAN(data.getAttributes()); model.setClassName(classVarName); model.setRootVarName(rootVarName); model.updateModel(data); System.out.println(model.getDAG()); System.out.println(); System.out.println(model.getModel()); }
HODE hode = new HODE(data.getAttributes()); hode.setClassName(classVarName); hode.updateModel(data); for (DataOnMemory<DataInstance> batch : data.iterableOverBatches(100)) { hode.updateModel(batch); System.out.println(hode.getDAG()); System.out.println(hode.getModel()); double realValue = d.getValue(hode.getClassVar()); double predValue; d.setValue(hode.getClassVar(), Utils.missingValue()); Multinomial posteriorProb = hode.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<>(); }
/** * Method to set the class variable. Note that it should be multinomial * @param classVar object of the type {@link Variable} indicating which is the class variable * @throws WrongConfigurationException is thrown when the variable is not a multinomial. */ public T setClassVar(Variable classVar) throws WrongConfigurationException { if(!classVar.isMultinomial()) { setErrorMessage("class variable is not a multinomial"); throw new WrongConfigurationException(errorMessage); } this.classVar = classVar; dag = null; return ((T) this); }
@Override public boolean isValidConfiguration() { boolean isValid = true; long numFinite = vars.getListOfVariables().stream() .filter( v -> v.getStateSpaceTypeEnum().equals(StateSpaceTypeEnum.FINITE_SET)) .count(); if(numFinite <2) { isValid = false; String errorMsg = "Invalid configuration: There should be at least 2 discrete variables (root and class)"; this.setErrorMessage(errorMsg); } return isValid; }
/** * tests if the attributes passed as an argument in the constructor are suitable for this classifier * @return boolean value with the result of the test. */ @Override public boolean isValidConfiguration(){ boolean isValid = true; long numFinite = vars.getListOfVariables().stream() .filter( v -> v.getStateSpaceTypeEnum().equals(StateSpaceTypeEnum.FINITE_SET)) .count(); if(numFinite == 0) { isValid = false; String errorMsg = "It should contain at least 1 discrete variable and the rest shoud be real"; this.setErrorMessage(errorMsg); } return isValid; }
@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; }
public boolean isValidConfiguration(){ boolean isValid = true; long numReal = vars.getListOfVariables().stream() .filter( v -> v.getStateSpaceTypeEnum().equals(StateSpaceTypeEnum.REAL)) .count(); long numFinite = vars.getListOfVariables().stream() .filter( v -> v.getStateSpaceTypeEnum().equals(StateSpaceTypeEnum.FINITE_SET)) .count(); if(numFinite != 1 || numReal != vars.getNumberOfVars()-1) { isValid = false; String errorMsg = "Invalid configuration: wrong number types of variables domains. It should contain 1 discrete variable and the rest shoud be real"; this.setErrorMessage(errorMsg); } return isValid; }
@Override public boolean isValidConfiguration(){ boolean isValid = true; long numReal = vars.getListOfVariables().stream() .filter( v -> v.getStateSpaceTypeEnum().equals(StateSpaceTypeEnum.REAL)) .count(); long numFinite = vars.getListOfVariables().stream() .filter( v -> v.getStateSpaceTypeEnum().equals(StateSpaceTypeEnum.FINITE_SET)) .count(); if(numFinite != 1 || numReal != vars.getNumberOfVars()-1) { isValid = false; String errorMsg = "Invalid configuration: wrong number types of variables domains. It should contain 1 discrete variable and the rest shoud be real"; this.setErrorMessage(errorMsg); } return isValid; }
/** * Builds the DAG over the set of variables given with the naive Bayes structure */ @Override protected void buildDAG() { //We create a standard naive Bayes dag = new DAG(vars); dag.getParentSets().stream().filter(w -> !w.getMainVar().equals(classVar)).forEach(w -> w.addParent(classVar)); // if it is not diagonal add the links between the attributes (features) if(!isDiagonal()) { List<Variable> attrVars = vars.getListOfVariables().stream().filter(v -> !v.equals(classVar)).collect(Collectors.toList()); for (int i=0; i<attrVars.size()-1; i++){ for(int j=i+1; j<attrVars.size(); j++) { // Add the links dag.getParentSet(attrVars.get(i)).addParent(attrVars.get(j)); } } } } /*
/** * Predicts the class membership probabilities for a given instance. * @param instance the data instance to be classified. The value associated to the class variable must be * a missing value (i.e. a NaN) * @return the posterior probability of the class variable */ 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(); return this.inferenceAlgoPredict.getPosterior(classVar); }
@Override protected void buildDAG() { Variable superParentVar = vars.newMultinomialVariable("superParentVar",getNumStates()); dag = new DAG(vars); dag.getParentSets() .stream() .filter(w -> !w.getMainVar().equals(classVar)) .filter(w -> !w.getMainVar().equals(superParentVar)) .forEach(w -> { w.addParent(classVar); w.addParent(superParentVar); }); dag.getParentSet(superParentVar).addParent(classVar); }
@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); }