Java Code Examples for weka.core.Instance#isMissing()
The following examples show how to use
weka.core.Instance#isMissing() .
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 |
/** * 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: DecisionTableHashKey.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * Constructor for a hashKey * * @param t an instance from which to generate a key * @param numAtts the number of attributes * @param ignoreClass if true treat the class as a normal attribute * @throws Exception if something goes wrong */ public DecisionTableHashKey(Instance t, int numAtts, boolean ignoreClass) throws Exception { int i; int cindex = t.classIndex(); key = -999; attributes = new double [numAtts]; missing = new boolean [numAtts]; for (i=0;i<numAtts;i++) { if (i == cindex && !ignoreClass) { missing[i] = true; } else { if ((missing[i] = t.isMissing(i)) == false) { attributes[i] = t.value(i); } } } }
Example 3
Source File: MINND.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * Updates the minimum and maximum values for all the attributes * based on a new exemplar. * * @param ex the new exemplar */ private void updateMinMax(Instance ex) { Instances insts = ex.relationalValue(1); for (int j = 0;j < m_Dimension; j++) { if (insts.attribute(j).isNumeric()){ for(int k=0; k < insts.numInstances(); k++){ Instance ins = insts.instance(k); if(!ins.isMissing(j)){ if (Double.isNaN(m_MinArray[j])) { m_MinArray[j] = ins.value(j); m_MaxArray[j] = ins.value(j); } else { if (ins.value(j) < m_MinArray[j]) m_MinArray[j] = ins.value(j); else if (ins.value(j) > m_MaxArray[j]) m_MaxArray[j] = ins.value(j); } } } } } }
Example 4
Source File: MakeIndicator.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * Input an instance for filtering. The instance is processed * and made available for output immediately. * * @param instance the input instance * @return true if the filtered instance may now be * collected with output(). * @throws IllegalStateException if no input format has been set. */ public boolean input(Instance instance) { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (m_NewBatch) { resetQueue(); m_NewBatch = false; } Instance newInstance = (Instance)instance.copy(); if (!newInstance.isMissing(m_AttIndex.getIndex())) { if (m_ValIndex.isInRange((int)newInstance.value(m_AttIndex.getIndex()))) { newInstance.setValue(m_AttIndex.getIndex(), 1); } else { newInstance.setValue(m_AttIndex.getIndex(), 0); } } push(newInstance); return true; }
Example 5
Source File: EM.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * Updates the minimum and maximum values for all the attributes based on a * new instance. * * @param instance the new instance */ private void updateMinMax(Instance instance) { for (int j = 0; j < m_theInstances.numAttributes(); j++) { if (!instance.isMissing(j)) { if (Double.isNaN(m_minValues[j])) { m_minValues[j] = instance.value(j); m_maxValues[j] = instance.value(j); } else { if (instance.value(j) < m_minValues[j]) { m_minValues[j] = instance.value(j); } else { if (instance.value(j) > m_maxValues[j]) { m_maxValues[j] = instance.value(j); } } } } } }
Example 6
Source File: Element.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Checks if an Element is contained by a given Instance. * * @param instance the given Instance * @return true, if the Instance contains the Element, else false */ public boolean isContainedBy(Instance instance) { for (int i=0; i < instance.numAttributes(); i++) { if (m_Events[i] > -1) { if (instance.isMissing(i)) { return false; } if (m_Events[i] != (int) instance.value(i)) { return false; } } } return true; }
Example 7
Source File: PreConstructedLinearModel.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Predicts the class of the supplied instance using the linear model. * * @param inst the instance to make a prediction for * @return the prediction * @exception Exception if an error occurs */ public double classifyInstance(Instance inst) throws Exception { double result = 0; // System.out.println(inst); for (int i = 0; i < m_coefficients.length; i++) { if (i != inst.classIndex() && !inst.isMissing(i)) { // System.out.println(inst.value(i)+" "+m_coefficients[i]); result += m_coefficients[i] * inst.value(i); } } result += m_intercept; return result; }
Example 8
Source File: KStar.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Calculate the probability of the first instance transforming into the * second instance: * the probability is the product of the transformation probabilities of * the attributes normilized over the number of instances used. * * @param first the test instance * @param second the train instance * @return transformation probability value */ private double instanceTransformationProbability(Instance first, Instance second) { String debug = "(KStar.instanceTransformationProbability) "; double transProb = 1.0; int numMissAttr = 0; for (int i = 0; i < m_NumAttributes; i++) { if (i == m_Train.classIndex()) { continue; // ignore class attribute } if (first.isMissing(i)) { // test instance attribute value is missing numMissAttr++; continue; } transProb *= attrTransProb(first, second, i); // normilize for missing values if (numMissAttr != m_NumAttributes) { transProb = Math.pow(transProb, (double)m_NumAttributes / (m_NumAttributes - numMissAttr)); } else { // weird case! transProb = 0.0; } } // normilize for the train dataset return transProb / m_NumInstances; }
Example 9
Source File: KStarNominalAttribute.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Calculates the distribution, in the dataset, of the indexed nominal * attribute values. It also counts the actual number of training instances * that contributed (those with non-missing values) to calculate the * distribution. */ private void generateAttrDistribution() { String debug = "(KStarNominalAttribute.generateAttrDistribution)"; m_Distribution = new int[ m_TrainSet.attribute(m_AttrIndex).numValues() ]; int i; Instance train; for (i=0; i < m_NumInstances; i++) { train = m_TrainSet.instance(i); if ( !train.isMissing(m_AttrIndex) ) { m_TotalCount++; m_Distribution[(int)train.value(m_AttrIndex)]++; } } }
Example 10
Source File: UnivariateNumericBinarySplit.java From tsml with GNU General Public License v3.0 | 5 votes |
@Override public String branchForInstance(Instance inst) { Attribute att = inst.dataset().attribute(m_splitAttNames.get(0)); if (att == null || inst.isMissing(att)) { // TODO ------------- return null; } if (inst.value(att) <= m_splitPoint) { return "left"; } return "right"; }
Example 11
Source File: SimpleCart.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Computes class probabilities for instance using the decision tree. * * @param instance the instance for which class probabilities is to be computed * @return the class probabilities for the given instance * @throws Exception if something goes wrong */ public double[] distributionForInstance(Instance instance) throws Exception { if (!m_isLeaf) { // value of split attribute is missing if (instance.isMissing(m_Attribute)) { double[] returnedDist = new double[m_ClassProbs.length]; for (int i = 0; i < m_Successors.length; i++) { double[] help = m_Successors[i].distributionForInstance(instance); if (help != null) { for (int j = 0; j < help.length; j++) { returnedDist[j] += m_Props[i] * help[j]; } } } return returnedDist; } // split attribute is nonimal else if (m_Attribute.isNominal()) { if (m_SplitString.indexOf("(" + m_Attribute.value((int)instance.value(m_Attribute)) + ")")!=-1) return m_Successors[0].distributionForInstance(instance); else return m_Successors[1].distributionForInstance(instance); } // split attribute is numeric else { if (instance.value(m_Attribute) < m_SplitValue) return m_Successors[0].distributionForInstance(instance); else return m_Successors[1].distributionForInstance(instance); } } // leaf node else return m_ClassProbs; }
Example 12
Source File: DecisionTreeNode.java From collective-classification-weka-package with GNU General Public License v3.0 | 4 votes |
/** * Computes class distribution of an instance using the decision tree. * * @param instance the instance to compute the distribution for * @return the class distribution * @throws Exception if something goes wrong */ public double[] distributionForInstance(Instance instance) throws Exception { double[] returnedDist = null; if (getAttribute() > -1) { // Node is not a leaf if (instance.isMissing(getAttribute())) { if (getDebugLevel() > 0) System.out.println(toStringNode()); // Value is missing returnedDist = new double[getInformation().numClasses()]; // Split instance up for (int i = 0; i < getChildCount(); i++) { double[] help = getNodeAt(i).distributionForInstance(instance); if (getDebugLevel() > 1) System.out.println("help: " + Utils.arrayToString(help)); if (help != null) { for (int j = 0; j < help.length; j++) { returnedDist[j] += m_Prop[i] * help[j]; } } } if (getDebugLevel() > 1) System.out.println( "--> returnedDist: " + Utils.arrayToString(returnedDist)); } else if (getInformation().attribute(getAttribute()).isNominal()) { // For nominal attributes int branch = 0; // branch for each nominal value? if (getNominalSplit() == null) { branch = (int) instance.value(getAttribute()); } else { // determine the branch we have to go down for (int i = 0; i < getNominalSplit().length; i++) { for (int n = 0; n < getNominalSplit()[i].length; n++) { if (Utils.eq(instance.value(getAttribute()), getNominalSplit()[i][n])) { branch = i; break; } } } } returnedDist = getNodeAt(branch).distributionForInstance(instance); } else { // For numeric attributes if (Utils.sm(instance.value(getAttribute()), getSplitPoint())) { returnedDist = getNodeAt(0).distributionForInstance(instance); } else { returnedDist = getNodeAt(1).distributionForInstance(instance); } } } if ((getAttribute() == -1) || (returnedDist == null)) { // Node is a leaf or successor is empty return getClassProbabilities(); } else { return returnedDist; } }
Example 13
Source File: MergeManyValues.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Input an instance for filtering. The instance is processed * and made available for output immediately. * * @param instance the input instance * @return true if the filtered instance may now be * collected with output(). * @throws IllegalStateException if no input format has been set. */ public boolean input(Instance instance) { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (m_NewBatch) { resetQueue(); m_NewBatch = false; } Attribute att = getInputFormat().attribute(m_AttIndex.getIndex()); FastVector newVals = new FastVector(att.numValues() - 1); for (int i = 0; i < att.numValues(); i++) { boolean inMergeList = false; if(att.value(i).equalsIgnoreCase(m_Label)){ //don't want to add this one. inMergeList = true; }else{ inMergeList = m_MergeRange.isInRange(i); } if(!inMergeList){ //add it. newVals.addElement(att.value(i)); } } newVals.addElement(m_Label); Attribute temp = new Attribute(att.name(), newVals); Instance newInstance = (Instance)instance.copy(); if (!newInstance.isMissing(m_AttIndex.getIndex())) { String currValue = newInstance.stringValue(m_AttIndex.getIndex()); if(temp.indexOfValue(currValue) == -1) newInstance.setValue(m_AttIndex.getIndex(), temp.indexOfValue(m_Label)); else newInstance.setValue(m_AttIndex.getIndex(), temp.indexOfValue(currValue)); } push(newInstance); return true; }
Example 14
Source File: RemoveWithValues.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Input an instance for filtering. Ordinarily the instance is processed * and made available for output immediately. Some filters require all * instances be read before producing output. * * @param instance the input instance * @return true if the filtered instance may now be * collected with output(). * @throws IllegalStateException if no input format has been set. */ public boolean input(Instance instance) { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (m_NewBatch) { resetQueue(); m_NewBatch = false; } if (isFirstBatchDone() && m_dontFilterAfterFirstBatch) { push((Instance)instance.copy()); return true; } if (instance.isMissing(m_AttIndex.getIndex())) { if (!getMatchMissingValues()) { push((Instance)instance.copy()); return true; } else { return false; } } if (isNumeric()) { if (!m_Values.getInvert()) { if (instance.value(m_AttIndex.getIndex()) < m_Value) { push((Instance)instance.copy()); return true; } } else { if (instance.value(m_AttIndex.getIndex()) >= m_Value) { push((Instance)instance.copy()); return true; } } } if (isNominal()) { if (m_Values.isInRange((int)instance.value(m_AttIndex.getIndex()))) { Instance temp = (Instance)instance.copy(); if (getModifyHeader()) { temp.setValue(m_AttIndex.getIndex(), m_NominalMapping[(int)instance.value(m_AttIndex.getIndex())]); } push(temp); return true; } } return false; }
Example 15
Source File: CollectiveTree.java From collective-classification-weka-package with GNU General Public License v3.0 | 4 votes |
/** * determines the branch and index in that branch the given instance will go * into, according to the nominal split * @param inst the instance to distribute into a branch * @param att the attribute index * @param split the nominal split on the given attribute * @param attDist the attribute distribution (of instances with no * missing value at the attribute's position) * @return the branch (index 0) and the index in this branch * (index 1), -1 if an error happened */ protected int[] determineBranch( Instance inst, int att, double[][] split, double[] attDist) { int i; int n; int[] result; double val; double currVal; result = new int[2]; result[0] = -1; result[1] = -1; // missing? -> randomly, according to attribute distribution if (inst.isMissing(att)) { val = m_RandomAtt.nextDouble(); // determine branch the random number fits into currVal = 0; for (i = 0; i < attDist.length; i++) { if ( (val >= currVal) && (val < attDist[i]) ) { result[0] = i; result[1] = (int) currVal; break; } currVal = attDist[i]; } } // find value in the nominal splits else { for (i = 0; i < split.length; i++) { for (n = 0; n < split[i].length; n++) { // found? if (Utils.eq(inst.value(att), split[i][n])) { result[0] = i; result[1] = n; break; } } if (result[0] != -1) break; } } return result; }
Example 16
Source File: IB1.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Calculates the distance between two instances * * @param first the first instance * @param second the second instance * @return the distance between the two given instances */ private double distance(Instance first, Instance second) { double diff, distance = 0; for(int i = 0; i < m_Train.numAttributes(); i++) { if (i == m_Train.classIndex()) { continue; } if (m_Train.attribute(i).isNominal()) { // If attribute is nominal if (first.isMissing(i) || second.isMissing(i) || ((int)first.value(i) != (int)second.value(i))) { distance += 1; } } else { // If attribute is numeric if (first.isMissing(i) || second.isMissing(i)){ if (first.isMissing(i) && second.isMissing(i)) { diff = 1; } else { if (second.isMissing(i)) { diff = norm(first.value(i), i); } else { diff = norm(second.value(i), i); } if (diff < 0.5) { diff = 1.0 - diff; } } } else { diff = norm(first.value(i), i) - norm(second.value(i), i); } distance += diff * diff; } } return distance; }
Example 17
Source File: XMLInstances.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * adds the instance to the XML structure * * @param parent the parent node to add the instance node as child * @param inst the instance to add */ protected void addInstance(Element parent, Instance inst) { Element node; Element value; Element child; boolean sparse; int i; int n; int index; node = m_Document.createElement(TAG_INSTANCE); parent.appendChild(node); // sparse? sparse = (inst instanceof SparseInstance); if (sparse) node.setAttribute(ATT_TYPE, VAL_SPARSE); // weight if (inst.weight() != 1.0) node.setAttribute(ATT_WEIGHT, Utils.doubleToString(inst.weight(), m_Precision)); // values for (i = 0; i < inst.numValues(); i++) { index = inst.index(i); value = m_Document.createElement(TAG_VALUE); node.appendChild(value); if (inst.isMissing(index)) { value.setAttribute(ATT_MISSING, VAL_YES); } else { if (inst.attribute(index).isRelationValued()) { child = m_Document.createElement(TAG_INSTANCES); value.appendChild(child); for (n = 0; n < inst.relationalValue(i).numInstances(); n++) addInstance(child, inst.relationalValue(i).instance(n)); } else { if (inst.attribute(index).type() == Attribute.NUMERIC) value.appendChild(m_Document.createTextNode(Utils.doubleToString(inst.value(index), m_Precision))); else value.appendChild(m_Document.createTextNode(validContent(inst.stringValue(index)))); } } if (sparse) value.setAttribute(ATT_INDEX, "" + (index+1)); } }
Example 18
Source File: LPS.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Computes class distribution of an instance using the decision tree. * * @param instance the instance to compute the distribution for * @return the computed class distribution * @throws Exception if computation fails */ public double[] distributionForInstance(Instance instance) throws Exception { double[] returnedDist = null; if(m_Attribute > -1) { // Node is not a leaf if (instance.isMissing(m_Attribute)) { // Value is missing returnedDist = new double[m_Info.numClasses()]; // Split instance up for (int i = 0; i < m_Successors.length; i++) { double[] help = m_Successors[i].distributionForInstance(instance); if (help != null) { for (int j = 0; j < help.length; j++) { returnedDist[j] += m_Prop[i] * help[j]; } } } } else if (m_Info.attribute(m_Attribute).isNominal()) { // For nominal attributes returnedDist = m_Successors[(int) instance.value(m_Attribute)] .distributionForInstance(instance); } else { // For numeric attributes if (instance.value(m_Attribute) < m_SplitPoint) { returnedDist = m_Successors[0].distributionForInstance(instance); } else { returnedDist = m_Successors[1].distributionForInstance(instance); } } } // Node is a leaf or successor is empty? if ((m_Attribute == -1) || (returnedDist == null)) { lastNode=leafNodeID; // System.out.println("Setting last node ="+leafNodeID); // Is node empty? if (m_ClassDistribution == null) { if (getAllowUnclassifiedInstances()) { double[] result = new double[m_Info.numClasses()]; if (m_Info.classAttribute().isNumeric()) { result[0] = Utils.missingValue(); } return result; } else { return null; } } // Else return normalized distribution double[] normalizedDistribution = m_ClassDistribution.clone(); if (m_Info.classAttribute().isNominal()) { Utils.normalize(normalizedDistribution); } return normalizedDistribution; } else { return returnedDist; } }
Example 19
Source File: REPTree.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Computes class distribution of an instance using the tree. * * @param instance the instance to compute the distribution for * @return the distribution * @throws Exception if computation fails */ protected double[] distributionForInstance(Instance instance) throws Exception { double[] returnedDist = null; if (m_Attribute > -1) { // Node is not a leaf if (instance.isMissing(m_Attribute)) { // Value is missing returnedDist = new double[m_Info.numClasses()]; // Split instance up for (int i = 0; i < m_Successors.length; i++) { double[] help = m_Successors[i].distributionForInstance(instance); if (help != null) { for (int j = 0; j < help.length; j++) { returnedDist[j] += m_Prop[i] * help[j]; } } } } else if (m_Info.attribute(m_Attribute).isNominal()) { // For nominal attributes returnedDist = m_Successors[(int)instance.value(m_Attribute)]. distributionForInstance(instance); } else { // For numeric attributes if (instance.value(m_Attribute) < m_SplitPoint) { returnedDist = m_Successors[0].distributionForInstance(instance); } else { returnedDist = m_Successors[1].distributionForInstance(instance); } } } if ((m_Attribute == -1) || (returnedDist == null)) { // Node is a leaf or successor is empty if (m_ClassProbs == null) { return m_ClassProbs; } return (double[])m_ClassProbs.clone(); } else { return returnedDist; } }
Example 20
Source File: LPS.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Computes array that indicates node membership. Array locations are * allocated based on breadth-first exploration of the tree. */ @Override public double[] getMembershipValues(Instance instance) throws Exception { if (m_zeroR != null) { double[] m = new double[1]; m[0] = instance.weight(); return m; } else { // Set up array for membership values double[] a = new double[numElements()]; // Initialize queues Queue<Double> queueOfWeights = new LinkedList<Double>(); Queue<Tree> queueOfNodes = new LinkedList<Tree>(); queueOfWeights.add(instance.weight()); queueOfNodes.add(m_Tree); int index = 0; // While the queue is not empty while (!queueOfNodes.isEmpty()) { a[index++] = queueOfWeights.poll(); Tree node = queueOfNodes.poll(); // Is node a leaf? if (node.m_Attribute <= -1) { continue; } // Compute weight distribution double[] weights = new double[node.m_Successors.length]; if (instance.isMissing(node.m_Attribute)) { System.arraycopy(node.m_Prop, 0, weights, 0, node.m_Prop.length); } else if (m_Info.attribute(node.m_Attribute).isNominal()) { weights[(int) instance.value(node.m_Attribute)] = 1.0; } else { if (instance.value(node.m_Attribute) < node.m_SplitPoint) { weights[0] = 1.0; } else { weights[1] = 1.0; } } for (int i = 0; i < node.m_Successors.length; i++) { queueOfNodes.add(node.m_Successors[i]); queueOfWeights.add(a[index - 1] * weights[i]); } } return a; } }