Java Code Examples for weka.core.Instances#sumOfWeights()

The following examples show how to use weka.core.Instances#sumOfWeights() . 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: MultiBoostAB.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Sets the weights for the next iteration.
  * 
  * @param training the data to train with
  * @param reweight the reweighting factor
  * @throws Exception in case of an error
  */
 protected void setWeights(Instances training, double reweight) 
   throws Exception {

   int subCmtySize = m_Classifiers.length / m_NumSubCmtys;

   if ((m_NumIterationsPerformed + 1) % subCmtySize == 0) {

     if (getDebug())
System.err.println(m_NumIterationsPerformed + " " + subCmtySize);

     double oldSumOfWeights = training.sumOfWeights();

     // Randomly set the weights of the training instances to the poisson distributon
     for (int i = 0; i < training.numInstances(); i++) {
training.instance(i).setWeight( - Math.log((m_Random.nextDouble() * 9999) / 10000) );
     }

     // Renormailise weights
     double sumProbs = training.sumOfWeights();
     for (int i = 0; i < training.numInstances(); i++) {
training.instance(i).setWeight(training.instance(i).weight() * oldSumOfWeights / sumProbs);
     }
   } else {
     super.setWeights(training, reweight);
   }
 }
 
Example 2
Source File: ADTree.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculates the Z-pure value for a particular set of instances.
 *
 * @param posInstances the positive-class instances
 * @param negInstances the negative-class instances
 * @return the Z-pure value
 */
private double calcZpure(Instances posInstances, Instances negInstances) {
  
  double posWeight = posInstances.sumOfWeights();
  double negWeight = negInstances.sumOfWeights();
  return (2.0 * (Math.sqrt(posWeight+1.0) + Math.sqrt(negWeight+1.0))) + 
    (m_trainTotalWeight - (posWeight + negWeight));
}
 
Example 3
Source File: ConjunctiveRule.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Private function to compute the squared error of
  * the specified data and the specified mean
  * 
  * @param data the data in question
  * @param mean the specified mean
  * @return the default mean-squared error
  */
 private double meanSquaredError(Instances data, double mean){ 
   if(Utils.eq(data.sumOfWeights(),0.0))
     return 0;

   double mSqErr=0, sum = data.sumOfWeights();
   for(int i=0; i < data.numInstances(); i++){
     Instance datum = data.instance(i);
     mSqErr += datum.weight()*
(datum.classValue() - mean)*
(datum.classValue() - mean);
   }	 

   return (mSqErr / sum);
 }
 
Example 4
Source File: Ridor.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
    * Build one rule using the growing data
    *
    * @param data the growing data used to build the rule
    */    
   private void grow(Instances data){
     Instances growData = new Instances(data);
    
     m_AccuG = computeDefAccu(growData);
     m_CoverG = growData.sumOfWeights();
     /* Compute the default accurate rate of the growing data */
     double defAcRt= m_AccuG / m_CoverG; 
    
     /* Keep the record of which attributes have already been used*/    
     boolean[] used=new boolean [growData.numAttributes()];
     for (int k=0; k<used.length; k++)
used[k]=false;
     int numUnused=used.length;
    
     double maxInfoGain;
     boolean isContinue = true; // The stopping criterion of this rule
    
     while (isContinue){   
maxInfoGain = 0;       // We require that infoGain be positive
	
/* Build a list of antecedents */
Antd oneAntd=null;
Instances coverData = null;
Enumeration enumAttr=growData.enumerateAttributes();	    
int index=-1;  
	
/* Build one condition based on all attributes not used yet*/
while (enumAttr.hasMoreElements()){
  Attribute att= (Attribute)(enumAttr.nextElement());
  index++;
	    
  Antd antd =null;	
  if(att.isNumeric())
    antd = new NumericAntd(att);
  else
    antd = new NominalAntd(att);
	    
  if(!used[index]){
    /* Compute the best information gain for each attribute,
       it's stored in the antecedent formed by this attribute.
       This procedure returns the data covered by the antecedent*/
    Instances coveredData = computeInfoGain(growData, defAcRt, antd);
    if(coveredData != null){
      double infoGain = antd.getMaxInfoGain();			
      if(Utils.gr(infoGain, maxInfoGain)){
	oneAntd=antd;
	coverData = coveredData;  
	maxInfoGain = infoGain;
      }		    
    }
  }
}
	
if(oneAntd == null)	 return;
	
//Numeric attributes can be used more than once
if(!oneAntd.getAttr().isNumeric()){ 
  used[oneAntd.getAttr().index()]=true;
  numUnused--;
}
	
m_Antds.addElement((Object)oneAntd);
growData = coverData;// Grow data size is shrinking 
	
defAcRt = oneAntd.getAccuRate();
	
/* Stop if no more data, rule perfect, no more attributes */
if(Utils.eq(growData.sumOfWeights(), 0.0) || Utils.eq(defAcRt, 1.0) || (numUnused == 0))
  isContinue = false;
     }
   }
 
Example 5
Source File: Ridor.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Builds a ripple-down manner rule learner.
 *
 * @param instances the training data
 * @throws Exception if classifier can't be built successfully
 */
public void buildClassifier(Instances instances) throws Exception {

  // can classifier handle the data?
  getCapabilities().testWithFail(instances);

  // remove instances with missing class
  Instances data = new Instances(instances);
  data.deleteWithMissingClass();
  
  int numCl = data.numClasses();
  m_Root = new Ridor_node();
  m_Class = instances.classAttribute();     // The original class label
	
  int index = data.classIndex();
  m_Cover = data.sumOfWeights();

  m_Random = new Random(m_Seed);
	
  /* Create a binary attribute */
  FastVector binary_values = new FastVector(2);
  binary_values.addElement("otherClasses");
  binary_values.addElement("defClass");
  Attribute attr = new Attribute ("newClass", binary_values);
  data.insertAttributeAt(attr, index);	
  data.setClassIndex(index);                 // The new class label

  /* Partition the data into bags according to their original class values */
  Instances[] dataByClass = new Instances[numCl];
  for(int i=0; i < numCl; i++)
    dataByClass[i] = new Instances(data, data.numInstances()); // Empty bags
  for(int i=0; i < data.numInstances(); i++){ // Partitioning
    Instance inst = data.instance(i);
    inst.setClassValue(0);           // Set new class vaue to be 0
    dataByClass[(int)inst.value(index+1)].add(inst); 
  }	
	
  for(int i=0; i < numCl; i++)    
    dataByClass[i].deleteAttributeAt(index+1);   // Delete original class
	
  m_Root.findRules(dataByClass, 0);
  
}
 
Example 6
Source File: PruneableDecList.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Builds the partial tree with hold out set
  *
  * @throws Exception if something goes wrong
  */
 public void buildDecList(Instances train, Instances test, 
		   boolean leaf) throws Exception {
   
   Instances [] localTrain,localTest;
   int index,ind;
   int i,j;
   double sumOfWeights;
   NoSplit noSplit;
   
   m_train = null;
   m_isLeaf = false;
   m_isEmpty = false;
   m_sons = null;
   indeX = 0;
   sumOfWeights = train.sumOfWeights();
   noSplit = new NoSplit (new Distribution((Instances)train));
   if (leaf)
     m_localModel = noSplit;
   else
     m_localModel = m_toSelectModel.selectModel(train, test);
   m_test = new Distribution(test, m_localModel);
   if (m_localModel.numSubsets() > 1) {
     localTrain = m_localModel.split(train);
     localTest = m_localModel.split(test);
     train = null;
     test = null;
     m_sons = new ClassifierDecList [m_localModel.numSubsets()];
     i = 0;
     do {
i++;
ind = chooseIndex();
if (ind == -1) {
  for (j = 0; j < m_sons.length; j++) 
    if (m_sons[j] == null)
      m_sons[j] = getNewDecList(localTrain[j],localTest[j],true);
  if (i < 2) {
    m_localModel = noSplit;
    m_isLeaf = true;
    m_sons = null;
    if (Utils.eq(sumOfWeights,0))
      m_isEmpty = true;
    return;
  }
  ind = 0;
  break;
} else 
  m_sons[ind] = getNewDecList(localTrain[ind],localTest[ind],false);
     } while ((i < m_sons.length) && (m_sons[ind].m_isLeaf));
     
     // Check if all successors are leaves
     for (j = 0; j < m_sons.length; j++) 
if ((m_sons[j] == null) || (!m_sons[j].m_isLeaf))
  break;
     if (j == m_sons.length) {
pruneEnd();
if (!m_isLeaf) 
  indeX = chooseLastIndex();
     }else 
indeX = chooseLastIndex();
   }else{
     m_isLeaf = true;
     if (Utils.eq(sumOfWeights, 0))
m_isEmpty = true;
   }
 }
 
Example 7
Source File: C45PruneableDecList.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Builds the partial tree without hold out set.
  *
  * @exception Exception if something goes wrong
  */
 public void buildDecList(Instances data, boolean leaf) throws Exception {
   
   Instances [] localInstances,localPruneInstances;
   int index,ind;
   int i,j;
   double sumOfWeights;
   NoSplit noSplit;
   
   m_train = null;
   m_test = null;
   m_isLeaf = false;
   m_isEmpty = false;
   m_sons = null;
   indeX = 0;
   sumOfWeights = data.sumOfWeights();
   noSplit = new NoSplit (new Distribution((Instances)data));
   if (leaf)
     m_localModel = noSplit;
   else
     m_localModel = m_toSelectModel.selectModel(data);
   if (m_localModel.numSubsets() > 1) {
     localInstances = m_localModel.split(data);
     data = null;
     m_sons = new ClassifierDecList [m_localModel.numSubsets()];
     i = 0;
     do {
i++;
ind = chooseIndex();
if (ind == -1) {
  for (j = 0; j < m_sons.length; j++) 
    if (m_sons[j] == null)
      m_sons[j] = getNewDecList(localInstances[j],true);
  if (i < 2) {
    m_localModel = noSplit;
    m_isLeaf = true;
    m_sons = null;
    if (Utils.eq(sumOfWeights,0))
      m_isEmpty = true;
    return;
  }
  ind = 0;
  break;
} else 
  m_sons[ind] = getNewDecList(localInstances[ind],false);
     } while ((i < m_sons.length) && (m_sons[ind].m_isLeaf));
     
     // Check if all successors are leaves
     for (j = 0; j < m_sons.length; j++) 
if ((m_sons[j] == null) || (!m_sons[j].m_isLeaf))
  break;
     if (j == m_sons.length) {
pruneEnd();
if (!m_isLeaf) 
  indeX = chooseLastIndex();
     }else 
indeX = chooseLastIndex();
   }else{
     m_isLeaf = true;
     if (Utils.eq(sumOfWeights, 0))
m_isEmpty = true;
   }
 }
 
Example 8
Source File: ClassifierDecList.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Builds the partial tree without hold out set.
  *
  * @exception Exception if something goes wrong
  */
 public void buildDecList(Instances data, boolean leaf) throws Exception {
   
   Instances [] localInstances,localPruneInstances;
   int index,ind;
   int i,j;
   double sumOfWeights;
   NoSplit noSplit;
   
   m_train = null;
   m_test = null;
   m_isLeaf = false;
   m_isEmpty = false;
   m_sons = null;
   indeX = 0;
   sumOfWeights = data.sumOfWeights();
   noSplit = new NoSplit (new Distribution((Instances)data));
   if (leaf)
     m_localModel = noSplit;
   else
     m_localModel = m_toSelectModel.selectModel(data);
   if (m_localModel.numSubsets() > 1) {
     localInstances = m_localModel.split(data);
     data = null;
     m_sons = new ClassifierDecList [m_localModel.numSubsets()];
     i = 0;
     do {
i++;
ind = chooseIndex();
if (ind == -1) {
  for (j = 0; j < m_sons.length; j++) 
    if (m_sons[j] == null)
      m_sons[j] = getNewDecList(localInstances[j],true);
  if (i < 2) {
    m_localModel = noSplit;
    m_isLeaf = true;
    m_sons = null;
    if (Utils.eq(sumOfWeights,0))
      m_isEmpty = true;
    return;
  }
  ind = 0;
  break;
} else 
  m_sons[ind] = getNewDecList(localInstances[ind],false);
     } while ((i < m_sons.length) && (m_sons[ind].m_isLeaf));
     
     // Choose rule
     indeX = chooseLastIndex();
   }else{
     m_isLeaf = true;
     if (Utils.eq(sumOfWeights, 0))
m_isEmpty = true;
   }
 }