Java Code Examples for weka.core.Instance#classIsMissing()

The following examples show how to use weka.core.Instance#classIsMissing() . 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: NaiveBayes.java    From tsml with GNU General Public License v3.0 6 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.
  */
 public void updateClassifier(Instance instance) throws Exception {

   if (!instance.classIsMissing()) {
     Enumeration enumAtts = m_Instances.enumerateAttributes();
     int attIndex = 0;
     while (enumAtts.hasMoreElements()) {
Attribute attribute = (Attribute) enumAtts.nextElement();
if (!instance.isMissing(attribute)) {
  m_Distributions[attIndex][(int)instance.classValue()].
           addValue(instance.value(attribute), instance.weight());
}
attIndex++;
     }
     m_ClassDistribution.addValue(instance.classValue(),
                                  instance.weight());
   }
 }
 
Example 2
Source File: IB1.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
  * Classifies the given test instance.
  *
  * @param instance the instance to be classified
  * @return the predicted class for the instance 
  * @throws Exception if the instance can't be classified
  */
 public double classifyInstance(Instance instance) throws Exception {
   
   if (m_Train.numInstances() == 0) {
     throw new Exception("No training instances!");
   }

   double distance, minDistance = Double.MAX_VALUE, classValue = 0;
   updateMinMax(instance);
   Enumeration enu = m_Train.enumerateInstances();
   while (enu.hasMoreElements()) {
     Instance trainInstance = (Instance) enu.nextElement();
     if (!trainInstance.classIsMissing()) {
distance = distance(instance, trainInstance);
if (distance < minDistance) {
  minDistance = distance;
  classValue = trainInstance.classValue();
}
     }
   }

   return classValue;
 }
 
Example 3
Source File: HNode.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Update the class frequency distribution with the supplied instance
 * 
 * @param inst the instance to update with
 */
public void updateDistribution(Instance inst) {
  if (inst.classIsMissing()) {
    return;
  }
  String classVal = inst.stringValue(inst.classAttribute());

  WeightMass m = m_classDistribution.get(classVal);
  if (m == null) {
    m = new WeightMass();
    m.m_weight = 1.0;

    m_classDistribution.put(classVal, m);
  }
  m.m_weight += inst.weight();
}
 
Example 4
Source File: CollectiveTree.java    From collective-classification-weka-package with GNU General Public License v3.0 6 votes vote down vote up
/**
 * determines the class distribution of the instances with a non-missing
 * value as class value.
 * @param data        the instances to work on
 * @param indices     the sorted indices
 * @return            the distribution
 */
protected double[] determineClassDistribution(Instances data, int[] indices) {
  double[]      result;
  int           i;
  Instance      inst;

  result = new double[data.numClasses()];

  for (i = 0; i < indices.length; i++) {
    inst = data.instance(indices[i]);
    if (inst.classIsMissing())
      break;
    result[(int) inst.classValue()] += inst.weight();
  }

  return result;
}
 
Example 5
Source File: NNge.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Performs the update of the classifier
 *
 * @param instance the new instance
 * @throws Exception if the update fails
 */
private void update(Instance instance) throws Exception {

  if (instance.classIsMissing()) {
    return;
  }

  instance.replaceMissingValues(m_MissingVector);
  m_Train.add(instance);

  /* Update the minimum and maximum for all the attributes */
  updateMinMax(instance);

  /* update the mutual information datas */
  updateMI(instance);

  /* Nearest Exemplar */
  Exemplar nearest = nearestExemplar(instance);
	
  /* Adjust */
  if(nearest == null){
    Exemplar newEx = new Exemplar(this, m_Train, 10, instance.classValue());
    newEx.generalise(instance);
    initWeight(newEx);
    addExemplar(newEx);
    return;
  }
  adjust(instance, nearest);

  /* Generalise */
  generalise(instance);
}
 
Example 6
Source File: HyperPipes.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Updates the classifier.
 *
 * @param instance the instance to be put into the classifier
 * @throws Exception if the instance could not be included successfully
 */
public void updateClassifier(Instance instance) throws Exception {

  if (instance.classIsMissing()) {
    return;
  }
  m_HyperPipes[(int) instance.classValue()].addInstance(instance);
}
 
Example 7
Source File: Winnow.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Actual update routine (balanced) for prefiltered instances
  *
  * @param inst the instance to update the classifier with
  * @throws Exception if something goes wrong
  */
 private void actualUpdateClassifierBalanced(Instance inst) throws Exception {
   
   double posmultiplier,negmultiplier;

   if (!inst.classIsMissing()) {
     double prediction = makePredictionBalanced(inst);
       
     if (prediction != inst.classValue()) {
m_Mistakes++;

if(prediction == 0) {
  /* false neg: promote positive, demote negative*/
  posmultiplier=m_Alpha;
  negmultiplier=m_Beta;
} else {
  /* false pos: demote positive, promote negative */
  posmultiplier=m_Beta;
  negmultiplier=m_Alpha;
}
int n1 = inst.numValues(); int classIndex = m_Train.classIndex();
for(int l = 0 ; l < n1 ; l++) {
  if(inst.index(l) != classIndex && inst.valueSparse(l)==1) {
    m_predPosVector[inst.index(l)]*=posmultiplier;
    m_predNegVector[inst.index(l)]*=negmultiplier;
  }
}
//Utils.normalize(m_predPosVector);
//Utils.normalize(m_predNegVector);
     }
   }
   else {
     System.out.println("CLASS MISSING");
   }
 }
 
Example 8
Source File: SpreadSubsample.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates an index containing the position where each class starts in 
 * the getInputFormat(). m_InputFormat must be sorted on the class attribute.
 * 
 * @return the positions
 */
private int[] getClassIndices() {

  // Create an index of where each class value starts
  int [] classIndices = new int [getInputFormat().numClasses() + 1];
  int currentClass = 0;
  classIndices[currentClass] = 0;
  for (int i = 0; i < getInputFormat().numInstances(); i++) {
    Instance current = getInputFormat().instance(i);
    if (current.classIsMissing()) {
      for (int j = currentClass + 1; j < classIndices.length; j++) {
        classIndices[j] = i;
      }
      break;
    } else if (current.classValue() != currentClass) {
      for (int j = currentClass + 1; j <= current.classValue(); j++) {
        classIndices[j] = i;
      }          
      currentClass = (int) current.classValue();
    }
  }
  if (currentClass <= getInputFormat().numClasses()) {
    for (int j = currentClass + 1; j < classIndices.length; j++) {
      classIndices[j] = getInputFormat().numInstances();
    }
  }
  return classIndices;
}
 
Example 9
Source File: Evaluation.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Updates the class prior probabilities or the mean respectively (when
 * incrementally training).
 * 
 * @param instance the new training instance seen
 * @throws Exception if the class of the instance is not set
 */
public void updatePriors(Instance instance) throws Exception {
  if (!instance.classIsMissing()) {
    if (!m_ClassIsNominal) {
      addNumericTrainClass(instance.classValue(), instance.weight());
      m_ClassPriors[0] += instance.classValue() * instance.weight();
      m_ClassPriorsSum += instance.weight();
    } else {
      m_ClassPriors[(int) instance.classValue()] += instance.weight();
      m_ClassPriorsSum += instance.weight();
    }
  }
}
 
Example 10
Source File: CollectiveTree.java    From collective-classification-weka-package with GNU General Public License v3.0 5 votes vote down vote up
/**
 * determines the class of the instance. I.e. if it's not missing it just
 * returns it, otherwise it returns a random class based on the class
 * distribution
 * @param inst        the instance to get the class for
 * @param classDist   the class distribution
 * @return            the class for the instance
 */
protected double determineClass(Instance inst, double[] classDist) {
  double        result;
  double        val;
  double        currVal;
  int           i;

  result = Utils.missingValue();

  if (inst.classIsMissing()) {
    val = m_RandomClass.nextDouble() * Utils.sum(classDist);
    // determine class the random number fits into
    currVal = 0;
    for (i = 0; i < classDist.length; i++) {
      if ( (val >= currVal) && (val < classDist[i]) ) {
        result = i;
        break;
      }
      currVal = classDist[i];
    }
  }
  else {
    result = inst.classValue();
  }

  return result;
}
 
Example 11
Source File: IB1.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Updates the classifier.
 *
 * @param instance the instance to be put into the classifier
 * @throws Exception if the instance could not be included successfully
 */
public void updateClassifier(Instance instance) throws Exception {

  if (m_Train.equalHeaders(instance.dataset()) == false) {
    throw new Exception("Incompatible instance types");
  }
  if (instance.classIsMissing()) {
    return;
  }
  m_Train.add(instance);
  updateMinMax(instance);
}
 
Example 12
Source File: NominalToBinary.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/** Computes average class values for each attribute and value */
 private void computeAverageClassValues() {

   double totalCounts, sum;
   Instance instance;
   double [] counts;

   double [][] avgClassValues = new double[getInputFormat().numAttributes()][0];
   m_Indices = new int[getInputFormat().numAttributes()][0];
   for (int j = 0; j < getInputFormat().numAttributes(); j++) {
     Attribute att = getInputFormat().attribute(j);
     if (att.isNominal()) {
avgClassValues[j] = new double [att.numValues()];
counts = new double [att.numValues()];
for (int i = 0; i < getInputFormat().numInstances(); i++) {
  instance = getInputFormat().instance(i);
  if (!instance.classIsMissing() && 
      (!instance.isMissing(j))) {
    counts[(int)instance.value(j)] += instance.weight();
    avgClassValues[j][(int)instance.value(j)] += 
      instance.weight() * instance.classValue();
  }
}
sum = Utils.sum(avgClassValues[j]);
totalCounts = Utils.sum(counts);
if (Utils.gr(totalCounts, 0)) {
  for (int k = 0; k < att.numValues(); k++) {
    if (Utils.gr(counts[k], 0)) {
      avgClassValues[j][k] /= (double)counts[k];
    } else {
      avgClassValues[j][k] = sum / (double)totalCounts;
    }
  }
}
m_Indices[j] = Utils.sort(avgClassValues[j]);
     }
   }
 }
 
Example 13
Source File: LWL.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Adds the supplied instance to the training set.
 *
 * @param instance the instance to add
 * @throws Exception if instance could not be incorporated
 * successfully
 */
public void updateClassifier(Instance instance) throws Exception {

  if (m_Train == null) {
    throw new Exception("No training instance structure set!");
  }
  else if (m_Train.equalHeaders(instance.dataset()) == false) {
    throw new Exception("Incompatible instance types\n" + m_Train.equalHeadersMsg(instance.dataset()));
  }
  if (!instance.classIsMissing()) {
    m_NNSearch.update(instance);
    m_Train.add(instance);
  }
}
 
Example 14
Source File: PlainText.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Store the prediction made by the classifier as a string.
 * 
 * @param dist        the distribution to use
 * @param inst        the instance to generate text from
 * @param index       the index in the dataset
 * @throws Exception  if something goes wrong
 */
protected void doPrintClassification(double[] dist, Instance inst, int index) throws Exception {
  int width = 7 + m_NumDecimals;
  int prec = m_NumDecimals;
  
  Instance withMissing = (Instance)inst.copy();
  withMissing.setDataset(inst.dataset());
  
  double predValue = 0;
  if (Utils.sum(dist) == 0) {
    predValue = Utils.missingValue();
  } else {
    if (inst.classAttribute().isNominal()) {
      predValue = Utils.maxIndex(dist);
    } else {
      predValue = dist[0];                         
    }
  }
  
  // index
  append(Utils.padLeft("" + (index+1), 6));

  if (inst.dataset().classAttribute().isNumeric()) {
    // actual
    if (inst.classIsMissing())
      append(" " + Utils.padLeft("?", width));
    else
      append(" " + Utils.doubleToString(inst.classValue(), width, prec));
    // predicted
    if (Utils.isMissingValue(predValue))
      append(" " + Utils.padLeft("?", width));
    else
      append(" " + Utils.doubleToString(predValue, width, prec));
    // error
    if (Utils.isMissingValue(predValue) || inst.classIsMissing())
      append(" " + Utils.padLeft("?", width));
    else
      append(" " + Utils.doubleToString(predValue - inst.classValue(), width, prec));
  } else {
    // actual
    append(" " + Utils.padLeft(((int) inst.classValue()+1) + ":" + inst.toString(inst.classIndex()), width));
    // predicted
    if (Utils.isMissingValue(predValue))
      append(" " + Utils.padLeft("?", width));
    else
      append(" " + Utils.padLeft(((int) predValue+1) + ":" + inst.dataset().classAttribute().value((int)predValue), width));
    // error?
    if (!Utils.isMissingValue(predValue) && !inst.classIsMissing() && ((int) predValue+1 != (int) inst.classValue()+1))
      append(" " + "  +  ");
    else
      append(" " + "     ");
    // prediction/distribution
    if (m_OutputDistribution) {
      if (Utils.isMissingValue(predValue)) {
        append(" " + "?");
      }
      else {
        append(" ");
        for (int n = 0; n < dist.length; n++) {
          if (n > 0)
            append(",");
          if (n == (int) predValue)
            append("*");
          append(Utils.doubleToString(dist[n], prec));
        }
      }
    }
    else {
      if (Utils.isMissingValue(predValue))
        append(" " + "?");
      else
        append(" " + Utils.doubleToString(dist[(int)predValue], prec));
    }
  }

  // attributes
  append(" " + attributeValuesString(withMissing) + "\n");
  
}
 
Example 15
Source File: HTML.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
protected void doPrintClassification(double[] dist, Instance inst, int index) throws Exception {
  int prec = m_NumDecimals;
  
  Instance withMissing = (Instance)inst.copy();
  withMissing.setDataset(inst.dataset());
  
  double predValue = 0;
  if (Utils.sum(dist) == 0) {
    predValue = Utils.missingValue();
  } else {
    if (inst.classAttribute().isNominal()) {
      predValue = Utils.maxIndex(dist);
    } else {
      predValue = dist[0];                         
    }
  }
  
  // index
  append("<tr>");
  append("<td>" + (index+1) + "</td>");

  if (inst.dataset().classAttribute().isNumeric()) {
    // actual
    if (inst.classIsMissing())
      append("<td align=\"right\">" + "?" + "</td>");
    else
      append("<td align=\"right\">" + Utils.doubleToString(inst.classValue(), prec) + "</td>");
    // predicted
    if (Utils.isMissingValue(predValue))
      append("<td align=\"right\">" + "?" + "</td>");
    else
      append("<td align=\"right\">" + Utils.doubleToString(predValue, prec) + "</td>");
    // error
    if (Utils.isMissingValue(predValue) || inst.classIsMissing())
      append("<td align=\"right\">" + "?" + "</td>");
    else
      append("<td align=\"right\">" + Utils.doubleToString(predValue - inst.classValue(), prec) + "</td>");
  } else {
    // actual
    append("<td>" + ((int) inst.classValue()+1) + ":" + sanitize(inst.toString(inst.classIndex())) + "</td>");
    // predicted
    if (Utils.isMissingValue(predValue))
      append("<td>" + "?" + "</td>");
    else
      append("<td>" + ((int) predValue+1) + ":" + sanitize(inst.dataset().classAttribute().value((int)predValue)) + "</td>");
    // error?
    if (!Utils.isMissingValue(predValue) && !inst.classIsMissing() && ((int) predValue+1 != (int) inst.classValue()+1))
      append("<td>" + "+" + "</td>");
    else
      append("<td>" + "&nbsp;" + "</td>");
    // prediction/distribution
    if (m_OutputDistribution) {
      if (Utils.isMissingValue(predValue)) {
        append("<td>" + "?" + "</td>");
      }
      else {
        append("<td align=\"right\">");
        for (int n = 0; n < dist.length; n++) {
          if (n > 0)
            append("</td><td align=\"right\">");
          if (n == (int) predValue)
            append("*");
          append(Utils.doubleToString(dist[n], prec));
        }
        append("</td>");
      }
    }
    else {
      if (Utils.isMissingValue(predValue))
        append("<td align=\"right\">" + "?" + "</td>");
      else
        append("<td align=\"right\">" + Utils.doubleToString(dist[(int)predValue], prec) + "</td>");
    }
  }

  // attributes
  append(attributeValuesString(withMissing) + "</tr>\n");    
}
 
Example 16
Source File: ARAMNetwork.java    From meka with GNU General Public License v3.0 4 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.
 */
public void updateClassifier(Instance instance) throws Exception {
 //called once for each instance.
 if(!learningphase){
	return;
}

int num_classes=(int) (0.5 * numClasses);
int num_features=(int) (0.5 * numFeatures);
double[] data = new double[numFeatures];
double[] labels = new double[numClasses];
int numChanges = 0;

  if (!instance.classIsMissing()) {
   //Do the weight updates using the instance.

	for (int j = 0; j <num_features; j++) {
		data[j] = instance.value(num_classes+j);
		data[j+num_features] = 1 - data[j];
		//if (data[j]<0 || data[j]>1){
		//	System.out.println("Data not normalized, this will cause error!");
		//}
		
	}
	for (int j = 0; j < num_classes ; j++) {
		labels[j] = instance.value(j);
		labels[j+num_classes] = 1 - labels[j];
	}
	SortPair[] cateacti = ARTActivateCategories(data);
	java.util.Arrays.sort(cateacti);
	boolean resonance = false;
	int currentSortedIndex = 0;
	int currentCategory = -1;
	double matchA = 0;
	double matchB = 0;

	while (!resonance) {

		currentCategory = cateacti[currentSortedIndex]
				.getOriginalIndex();
		matchA = ART_Calculate_Match(data, weightsA[currentCategory]);
		if (sumArray(weightsB[currentCategory]) == 0) {
			matchB = 1;
		} else {
			matchB = ART_Calculate_Match(labels,
					weightsB[currentCategory]);

		}
		if (matchA >= roa && matchB >= rob) {
			if (currentCategory == numCategories -1) {

				if (currentSortedIndex == maxNumCategories) {
					System.out
							.println("WARNING: The maximum number of categories has been reached.");
					resonance = true;
				} else {
					// Add a new category
					for (int j = 0; j < data.length; j++) {
						weightsA[currentCategory][j] = data[j];
					}

					for (int j = 0; j < weightsB[currentCategory].length; j++) {
						weightsB[currentCategory][j] = labels[j];
					}
					ARAMm_Add_New_Category();
					// fprintf(FileID,'Add a new category of %d\n',
					// network.numCategories);
					// Increment the number of changes since we added a
					// new category.
					numChanges = numChanges + 1;
					resonance = true;
				}
			} else {
				// % Update weights
				double weightChange = ARAMm_Update_Weights(data,
						labels, currentCategory);
				if (weightChange == 1) {
					numChanges += 1;
				}

				resonance = true;
			}
		} else {
			currentSortedIndex += 1;
			resonance = false;
		}

	}
    }
}
 
Example 17
Source File: XML.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Store the prediction made by the classifier as a string.
 * 
 * @param dist        the distribution to use
 * @param inst        the instance to generate text from
 * @param index       the index in the dataset
 * @throws Exception  if something goes wrong
 */
protected void doPrintClassification(double[] dist, Instance inst, int index) throws Exception {
  int prec = m_NumDecimals;

  Instance withMissing = (Instance)inst.copy();
  withMissing.setDataset(inst.dataset());
  
  double predValue = 0;
  if (Utils.sum(dist) == 0) {
    predValue = Utils.missingValue();
  } else {
    if (inst.classAttribute().isNominal()) {
      predValue = Utils.maxIndex(dist);
    } else {
      predValue = dist[0];                         
    }
  }
  
  // opening tag
  append("  <" + TAG_PREDICTION + " " + ATT_INDEX + "=\"" + (index+1) + "\">\n");

  if (inst.dataset().classAttribute().isNumeric()) {
    // actual
    append("    <" + TAG_ACTUAL_VALUE + ">");
    if (inst.classIsMissing())
      append("?");
    else
      append(Utils.doubleToString(inst.classValue(), prec));
    append("</" + TAG_ACTUAL_VALUE + ">\n");
    // predicted
    append("    <" + TAG_PREDICTED_VALUE + ">");
    if (inst.classIsMissing())
      append("?");
    else
      append(Utils.doubleToString(predValue, prec));
    append("</" + TAG_PREDICTED_VALUE + ">\n");
    // error
    append("    <" + TAG_ERROR + ">");
    if (Utils.isMissingValue(predValue) || inst.classIsMissing())
      append("?");
    else
      append(Utils.doubleToString(predValue - inst.classValue(), prec));
    append("</" + TAG_ERROR + ">\n");
  } else {
    // actual
    append("    <" + TAG_ACTUAL_LABEL + " " + ATT_INDEX + "=\"" + ((int) inst.classValue()+1) + "\"" + ">");
    append(sanitize(inst.toString(inst.classIndex())));
    append("</" + TAG_ACTUAL_LABEL + ">\n");
    // predicted
    append("    <" + TAG_PREDICTED_LABEL + " " + ATT_INDEX + "=\"" + ((int) predValue+1) + "\"" + ">");
    if (Utils.isMissingValue(predValue))
      append("?");
    else
      append(sanitize(inst.dataset().classAttribute().value((int)predValue)));
    append("</" + TAG_PREDICTED_LABEL + ">\n");
    // error?
    append("    <" + TAG_ERROR + ">");
    if (!Utils.isMissingValue(predValue) && !inst.classIsMissing() && ((int) predValue+1 != (int) inst.classValue()+1))
      append(VAL_YES);
    else
      append(VAL_NO);
    append("</" + TAG_ERROR + ">\n");
    // prediction/distribution
    if (m_OutputDistribution) {
      append("    <" + TAG_DISTRIBUTION + ">\n");
      for (int n = 0; n < dist.length; n++) {
        append("      <" + TAG_CLASS_LABEL + " " + ATT_INDEX + "=\"" + (n+1) + "\"");
        if (!Utils.isMissingValue(predValue) && (n == (int) predValue))
          append(" " + ATT_PREDICTED + "=\"" + VAL_YES + "\"");
        append(">");
        append(Utils.doubleToString(dist[n], prec));
        append("</" + TAG_CLASS_LABEL + ">\n");
      }
      append("    </" + TAG_DISTRIBUTION + ">\n");
    }
    else {
      append("    <" + TAG_PREDICTION + ">");
      if (Utils.isMissingValue(predValue))
        append("?");
      else
        append(Utils.doubleToString(dist[(int)predValue], prec));
      append("</" + TAG_PREDICTION + ">\n");
    }
  }

  // attributes
  if (m_Attributes != null)
    append(attributeValuesString(withMissing));
  
  // closing tag
  append("  </" + TAG_PREDICTION + ">\n");
}
 
Example 18
Source File: WARAM.java    From meka with GNU General Public License v3.0 4 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.
 */
public void updateClassifier(Instance instance) throws Exception {
 //called once for each instance.

int num_classes=(int) (0.5 * numClasses);
int num_features=(int) (0.5 * numFeatures);
double[] data = new double[numFeatures];
double[] labels = new double[numClasses];
int numChanges = 0;
  if (!instance.classIsMissing()) {
   //Do the weight updates using the instance.

	for (int j = 0; j <num_features; j++) {
		data[j] = instance.value(num_classes+j);
		data[j+num_features] = 1 - data[j];
	}
	for (int j = 0; j < num_classes ; j++) {
		labels[j] = instance.value(j);
		labels[j+num_classes] = 1 - labels[j];
	}
	SortPair[] cateacti = ARTActivateCategories(data);java.util.Arrays.sort(cateacti);
	boolean resonance = false;
	int currentSortedIndex = 0;
	int currentCategory = -1;
	double matchA = 0;
	double matchB = 0;

	while (!resonance) {

		currentCategory = cateacti[currentSortedIndex]
				.getOriginalIndex();
		matchA = ART_Calculate_Match(data, weightsA[currentCategory]);
		if (sumArray(weightsB[currentCategory]) == 0) {
			matchB = 1;
		} else {
			matchB = ART_Calculate_Match(labels,
					weightsB[currentCategory]);

		}
		if (matchA >= roa && matchB >= rob) {
			if (currentCategory == numCategories -1) {

				if (currentSortedIndex == maxNumCategories) {
					System.out
							.println("WARNING: The maximum number of categories has been reached.");
					resonance = true;
				} else {
					// Add a new category
					for (int j = 0; j < data.length; j++) {
						weightsA[currentCategory][j] = data[j];
					}

					for (int j = 0; j < weightsB[currentCategory].length; j++) {
						weightsB[currentCategory][j] = labels[j];
					}
					ARAMm_Add_New_Category();
					// fprintf(FileID,'Add a new category of %d\n',
					// network.numCategories);
					// Increment the number of changes since we added a
					// new category.
					numChanges = numChanges + 1;
					resonance = true;
				}
			} else {
				// % Update weights
				double weightChange = ARAMm_Update_Weights(data,
						labels, currentCategory);
				if (weightChange == 1) {
					numChanges += 1;
				}

				resonance = true;
			}
		} else {
			currentSortedIndex += 1;
			resonance = false;
		}

	}
    }
}
 
Example 19
Source File: AODE.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/** 
   * Puts an instance's values into m_CondiCounts, m_ClassCounts and 
   * m_SumInstances.
   *
   * @param instance  the instance whose values are to be put into the counts
   *                  variables
   */
private void addToCounts(Instance instance) {
 
  double [] countsPointer;
 
  if(instance.classIsMissing())
     return;   // ignore instances with missing class

  int classVal = (int)instance.classValue();
  double weight = instance.weight();
 
  m_ClassCounts[classVal] += weight;
  m_SumInstances += weight;
 
  // store instance's att val indexes in an array, b/c accessing it 
  // in loop(s) is more efficient
  int [] attIndex = new int[m_NumAttributes];
  for(int i = 0; i < m_NumAttributes; i++) {
     if(i == m_ClassIndex)
        attIndex[i] = -1;  // we don't use the class attribute in counts
     else {
        if(instance.isMissing(i))
           attIndex[i] = m_StartAttIndex[i] + m_NumAttValues[i];
        else
           attIndex[i] = m_StartAttIndex[i] + (int)instance.value(i);
     }
  }

  for(int Att1 = 0; Att1 < m_NumAttributes; Att1++) {
     if(attIndex[Att1] == -1)
        continue;   // avoid pointless looping as Att1 is currently the class attribute

     m_Frequencies[attIndex[Att1]] += weight;
     
     // if this is a missing value, we don't want to increase sumforcounts
     if(!instance.isMissing(Att1))
        m_SumForCounts[classVal][Att1] += weight;

     // save time by referencing this now, rather than do it repeatedly in the loop
     countsPointer = m_CondiCounts[classVal][attIndex[Att1]];

     for(int Att2 = 0; Att2 < m_NumAttributes; Att2++) {
        if(attIndex[Att2] != -1) {
           countsPointer[attIndex[Att2]] += weight;
        }
     }
  }
}
 
Example 20
Source File: YATSI.java    From collective-classification-weka-package with GNU General Public License v3.0 4 votes vote down vote up
/**
 * internal function for determining the class distribution for an instance, 
 * will be overridden by derived classes. <br/>
 * 
 * @param instance	the instance to get the distribution for
 * @return		the distribution for the given instance
 * @throws Exception	if something goes wrong
 */
@Override
protected double[] getDistribution(Instance instance) throws Exception {
  int         index;
  int         i;
  double[]    result;
  Instances   neighbors;
  Instance    inst;
  double[]    count;
  double[]    countNum;
  int         labelIndex;

  result = null;

  // find instance
  index = m_Data.indexOf(instance);
  if (index > -1) {
    // get neighbors
    neighbors = m_NNSearch.kNearestNeighbours(
                  m_Data.get(index), m_KNNDetermined);

    // count class label
    count    = new double[neighbors.numClasses()];
    countNum = new double[neighbors.numClasses()];
    for (i = 0; i < neighbors.numInstances(); i++) {
      inst = neighbors.instance(i);
      if (!inst.classIsMissing()) {
        count[(int) inst.classValue()] += inst.weight();
        countNum[(int) inst.classValue()]++;
      }
    }

    // build result
    result = new double[instance.numClasses()];
    for (i = 0; i < result.length; i++)
      result[i] = count[i];
    if (Utils.gr(Utils.sum(result), 0))
      Utils.normalize(result);
    else
      System.out.println(
          "No summed up weights: " + instance 
          + ", counts=" + Utils.arrayToString(countNum));
    labelIndex = Utils.maxIndex(count);
    // is it a clear-cut distribution?
    if (!Utils.eq(Utils.sum(count) - count[labelIndex], 0))
      m_ClearCutDistribution++;
    // did the label change due to weights?
    if (Utils.maxIndex(countNum) != labelIndex)
      m_WeightFlips++;
  }
  else {
    throw new Exception("Cannot find instance: " + instance + "\n" 
        + " -> pos=" + index 
        + " = " + m_Data.get(StrictMath.abs(index)));
  }

  return result;
}