weka.classifiers.UpdateableClassifier Java Examples

The following examples show how to use weka.classifiers.UpdateableClassifier. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CRUpdateable.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void updateClassifier(Instance x) throws Exception {

	int L = x.classIndex();

	if(getDebug()) System.out.print("-: Updating "+L+" models");

	for(int j = 0; j < L; j++) {
		Instance x_j = (Instance)x.copy();
		x_j.setDataset(null);
		x_j = MLUtils.keepAttributesAt(x_j,new int[]{j},L);
		x_j.setDataset(m_Templates[j]);
		((UpdateableClassifier)m_MultiClassifiers[j]).updateClassifier(x_j);
	}

	if(getDebug()) System.out.println(":- ");
}
 
Example #2
Source File: BaggingMLUpdateable.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void updateClassifier(Instance x) throws Exception {

	for(int i = 0; i < m_NumIterations; i++) {
		// Oza-Bag style
		int k = poisson(1.0, random);
		if (m_BagSizePercent == 100) {
			// Train on all instances
			k = 1;
		}
		if (k > 0) {
			// Train on this instance only if k > 0
			Instance x_weighted = (Instance) x.copy();
			x_weighted.setWeight(x.weight() * (double)k);
			((UpdateableClassifier)m_Classifiers[i]).updateClassifier(x_weighted);
		}
	}
}
 
Example #3
Source File: CCUpdateable.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
protected void update(Instance x) throws Exception {

			Instance x_ = (Instance)x.copy();
			x_.setDataset(null);

			// delete all except one (leaving a binary problem)
			// delete all the attributes (and track where our index ends up)
			int c_index = this.value;
			for(int i = excld.length-1; i >= 0; i--) {
				x_.deleteAttributeAt(excld[i]);
				if (excld[i] < this.index)
					c_index--; 
			}
			x_.setDataset(this._template);

			((UpdateableClassifier)this.classifier).updateClassifier(x_);

			if (next != null)
				next.update(x);
		}
 
Example #4
Source File: BRUpdateable.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void updateClassifier(Instance x) throws Exception {

	int L = x.classIndex();

	if(getDebug()) System.out.print("-: Updating "+L+" models");

	for(int j = 0; j < L; j++) {
		Instance x_j = (Instance)x.copy();
		x_j.setDataset(null);
		x_j = MLUtils.keepAttributesAt(x_j,new int[]{j},L);
		x_j.setDataset(m_InstancesTemplates[j]);
		((UpdateableClassifier)m_MultiClassifiers[j]).updateClassifier(x_j);
	}

	if(getDebug()) System.out.println(":- ");
}
 
Example #5
Source File: MultiClassClassifierUpdateable.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void buildClassifier(Instances insts) throws Exception {
  if (m_Classifier == null) {
    throw new Exception("No base classifier has been set!");
  }

  if (!(m_Classifier instanceof UpdateableClassifier)) {
    throw new Exception("Base classifier must be updateable!");
  }

  super.buildClassifier(insts);
}
 
Example #6
Source File: MultiClassClassifierUpdateable.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Updates the classifier with the given instance.
 * 
 * @param instance the new training instance to include in the model
 * @exception Exception if the instance could not be incorporated in the
 *              model.
 */
@Override
public void updateClassifier(Instance instance) throws Exception {
  if (!instance.classIsMissing()) {

    if (m_Classifiers.length == 1) {
      ((UpdateableClassifier) m_Classifiers[0]).updateClassifier(instance);
      return;
    }

    for (int i = 0; i < m_Classifiers.length; i++) {
      if (m_Classifiers[i] != null) {
        m_ClassFilters[i].input(instance);
        Instance converted = m_ClassFilters[i].output();
        if (converted != null) {
          converted.dataset().setClassIndex(m_ClassAttribute.index());
          ((UpdateableClassifier) m_Classifiers[i])
              .updateClassifier(converted);

          if (m_Method == METHOD_1_AGAINST_1) {
            m_SumOfWeights[i] += converted.weight();
          }
        }
      }
    }
  }
}
 
Example #7
Source File: RTUpdateable.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void updateClassifier(Instance x) throws Exception {

	int L = x.classIndex();

	for (int j = 0; j < L; j++) {
		if(x.value(j) > 0.0) {
			Instance x_j = convertInstance(x);
			x_j.setClassValue(j);
			((UpdateableClassifier)m_Classifier).updateClassifier(x_j);
		}
	}
}
 
Example #8
Source File: PSUpdateable.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void updateClassifier(Instance x) throws Exception {

	if (batch.numInstances() < getLimit() && mlu != null) {

		// store example
		batch.add(x);
		if (batch.numInstances() >= getLimit()) {
			// we have enough instances to bulid PS!
			combinations = PSUtils.countCombinationsSparse(batch,L);
			MLUtils.pruneCountHashMap(combinations,m_P);
			// { NEW (we don't want more than m_Support classes!) -- note, the while loop is a slow way to do this
			int p = m_P;
			while(combinations.size() > getSupport()) {
				m_P++;
				MLUtils.pruneCountHashMap(combinations,m_P);
			}
			super.buildClassifier(batch);
			m_P = p;
			// } NEW
			batch.clear();
			mlu = null;
		}
		else {
			// not enough instances in the batch yet, just update the majority-label classifier
			mlu.updateClassifier(x);
		}
	}
	else {
		// update PS ...
		for (Instance x_i : PSUtils.PSTransformation(x,L,combinations,m_N,super.m_InstancesTemplate)) {
			// update internal sl classifier (e.g. naive bayes)
			((UpdateableClassifier)m_Classifier).updateClassifier(x_i);
		}
	}
	
}
 
Example #9
Source File: WEKAClassifier.java    From moa with GNU General Public License v3.0 5 votes vote down vote up
public void buildClassifier() {
    try {
        if ((classifier instanceof UpdateableClassifier) == false) {
            Classifier auxclassifier = weka.classifiers.AbstractClassifier.makeCopy(classifier);
            auxclassifier.buildClassifier(instancesBuffer);
            classifier = auxclassifier;
            isBufferStoring = false;
        }
    } catch (Exception e) {
        System.err.println("Building WEKA Classifier: " + e.getMessage());
    }
}
 
Example #10
Source File: MEKAClassifier.java    From moa with GNU General Public License v3.0 5 votes vote down vote up
public void createWekaClassifier(String[] options) throws Exception {
       String classifierName = options[0];
       String[] newoptions = options.clone();
       newoptions[0] = "";
       this.classifier = weka.classifiers.AbstractClassifier.forName(classifierName, newoptions);
	if (! (this.classifier instanceof UpdateableClassifier)) {
		// TODO: Non-Updateable MEKA classifiers could also work here.
		System.err.println("[ERROR] You must use an Updateable Classifier");
		throw new Exception("Only Updateable MEKA classifiers can be used.");
	}
}
 
Example #11
Source File: WEKAClassifier.java    From moa with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void trainOnInstanceImpl(Instance samoaInstance) {
    weka.core.Instance inst = this.instanceConverter.wekaInstance(samoaInstance);
    try {
        if (numberInstances == 0) {
            this.instancesBuffer = new weka.core.Instances(inst.dataset());
            if (classifier instanceof UpdateableClassifier) {
                classifier.buildClassifier(instancesBuffer);
                this.isClassificationEnabled = true;
            } else {
                this.isBufferStoring = true;
            }
        }
        numberInstances++;

        if (classifier instanceof UpdateableClassifier) {
            if (numberInstances > 0) {
                ((UpdateableClassifier) classifier).updateClassifier(inst);
            }
        } else {
            if (numberInstances == widthInitOption.getValue()) {
                //Build first time Classifier
                buildClassifier();
                isClassificationEnabled = true;
                //Continue to store instances
                if (sampleFrequencyOption.getValue() != 0) {
                    isBufferStoring = true;
                }
            }
            if (widthOption.getValue() == 0) {
                //Used from SingleClassifierDrift
                if (isBufferStoring == true) {
                    instancesBuffer.add(inst);
                }
            } else {
                //Used form WekaClassifier without using SingleClassifierDrift
                int numInstances = numberInstances % sampleFrequencyOption.getValue();
                if (sampleFrequencyOption.getValue() == 0) {
                    numInstances = numberInstances;
                }
                if (numInstances == 0) {
                    //Begin to store instances
                    isBufferStoring = true;
                }
                if (isBufferStoring == true && numInstances <= widthOption.getValue()) {
                    //Store instances
                    instancesBuffer.add(inst);
                }
                if (numInstances == widthOption.getValue()) {
                    //Build Classifier
                    buildClassifier();
                    isClassificationEnabled = true;
                    this.instancesBuffer = new weka.core.Instances(inst.dataset());
                }
            }
        }
    } catch (Exception e) {
        System.err.println("Training: " + e.getMessage());
    }
}