All Downloads are FREE. Search and download functionalities are using the official Maven repository.

moa.classifiers.trees.AdaHoeffdingOptionTree Maven / Gradle / Ivy

Go to download

Massive On-line Analysis is an environment for massive data mining. MOA provides a framework for data stream mining and includes tools for evaluation and a collection of machine learning algorithms. Related to the WEKA project, also written in Java, while scaling to more demanding problems.

There is a newer version: 2024.07.0
Show newest version
/*
 *    AdaHoeffdingOptionTree.java
 *    Copyright (C) 2008 University of Waikato, Hamilton, New Zealand
 *    @author Albert Bifet (abifet at cs dot waikato dot ac dot nz)
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program. If not, see .
 *    
 */
package moa.classifiers.trees;

import moa.classifiers.bayes.NaiveBayes;
import weka.core.Instance;
import weka.core.Utils;

/**
 * Adaptive decision option tree for streaming data with adaptive Naive
 * Bayes classification at leaves.
 * An Adaptive Hoeffding Option Tree is a Hoeffding Option Tree with the
 * following improvement: each leaf stores an estimation of the current error.
 * It uses an EWMA estimator with alpha = .2. The weight of each node in the
 * voting process is proportional to the square of the inverse of the error.
 * 

* Example:
* AdaHoeffdingOptionTree -o 50 * Parameters:
    *
  • Same parameters as HoeffdingOptionTreeNB
* * @author Albert Bifet (abifet at cs dot waikato dot ac dot nz) * @version $Revision: 7 $ */ public class AdaHoeffdingOptionTree extends HoeffdingOptionTree { private static final long serialVersionUID = 1L; @Override public String getPurposeString() { return "Adaptive decision option tree for streaming data with adaptive Naive Bayes classification at leaves."; } public static class AdaLearningNode extends LearningNodeNB { private static final long serialVersionUID = 1L; protected double mcCorrectWeight = 0.0; protected double nbCorrectWeight = 0.0; protected double CorrectWeight = 0.0; protected double alpha = 0.2; public AdaLearningNode(double[] initialClassObservations) { super(initialClassObservations); } @Override public void learnFromInstance(Instance inst, HoeffdingOptionTree hot) { int trueClass = (int) inst.classValue(); boolean blCorrect = false; if (this.observedClassDistribution.maxIndex() == trueClass) { this.mcCorrectWeight += inst.weight(); if (this.mcCorrectWeight > this.nbCorrectWeight) { blCorrect = true; } } if (Utils.maxIndex(NaiveBayes.doNaiveBayesPrediction(inst, this.observedClassDistribution, this.attributeObservers)) == trueClass) { this.nbCorrectWeight += inst.weight(); if (this.mcCorrectWeight <= this.nbCorrectWeight) { blCorrect = true; } } if (blCorrect == true) { this.CorrectWeight += alpha * (1.0 - this.CorrectWeight); //EWMA } else { this.CorrectWeight -= alpha * this.CorrectWeight; //EWMA } super.learnFromInstance(inst, hot); } @Override public double[] getClassVotes(Instance inst, HoeffdingOptionTree ht) { double[] dist; if (this.mcCorrectWeight > this.nbCorrectWeight) { dist = this.observedClassDistribution.getArrayCopy(); } else { dist = NaiveBayes.doNaiveBayesPrediction(inst, this.observedClassDistribution, this.attributeObservers); } double distSum = Utils.sum(dist); if (distSum * (1.0 - this.CorrectWeight) * (1.0 - this.CorrectWeight) > 0.0) { Utils.normalize(dist, distSum * (1.0 - this.CorrectWeight) * (1.0 - this.CorrectWeight)); //Adding weight } return dist; } } @Override protected LearningNode newLearningNode(double[] initialClassObservations) { return new AdaLearningNode(initialClassObservations); } }