/** * 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); }
protected void updateClassifier(Instance instance, boolean updateDictionary) throws Exception { if (!instance.classIsMissing()) { int classIndex = (int) instance.classValue(); m_probOfClass[classIndex] += instance.weight(); tokenizeInstance(instance, updateDictionary); m_t++; } }
protected void updateClassifier(Instance instance, boolean updateDictionary) throws Exception { if (!instance.classIsMissing()) { int classIndex = (int) instance.classValue(); m_probOfClass[classIndex] += instance.weight(); tokenizeInstance(instance, updateDictionary); m_t++; } }
/** * 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(); }
/** * Generate a bunch of predictions ready for processing, by performing a * evaluation on a test set assuming the classifier is already trained. * * @param classifier the pre-trained Classifier to evaluate * @param test the test dataset * @exception Exception if an error occurs */ public ArrayList<Prediction> getTestPredictions(Classifier classifier, Instances test) throws Exception { ArrayList<Prediction> predictions = new ArrayList<Prediction>(); for (int i = 0; i < test.numInstances(); i++) { if (!test.instance(i).classIsMissing()) { predictions.add(getPrediction(classifier, test.instance(i))); } } return predictions; }
/** * 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(); }
/** * Generate a bunch of predictions ready for processing, by performing a * evaluation on a test set assuming the classifier is already trained. * * @param classifier the pre-trained Classifier to evaluate * @param test the test dataset * @exception Exception if an error occurs */ public ArrayList<Prediction> getTestPredictions(Classifier classifier, Instances test) throws Exception { ArrayList<Prediction> predictions = new ArrayList<Prediction>(); for (int i = 0; i < test.numInstances(); i++) { if (!test.instance(i).classIsMissing()) { predictions.add(getPrediction(classifier, test.instance(i))); } } return predictions; }
/** * Check if the class is missing for all instances in the supplied set * * @param toCheck the instances to check * @return true if all class values are missing */ protected static boolean allMissingClass(Instances toCheck) { if (toCheck.classIndex() < 0) { return false; } for (int i = 0; i < toCheck.numInstances(); i++) { if (!toCheck.instance(i).classIsMissing()) { return false; } } return true; }
/** * Check if the class is missing for all instances in the supplied set * * @param toCheck the instances to check * @return true if all class values are missing */ protected static boolean allMissingClass(Instances toCheck) { if (toCheck.classIndex() < 0) { return false; } for (int i = 0; i < toCheck.numInstances(); i++) { if (!toCheck.instance(i).classIsMissing()) { return false; } } return true; }
/** * 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; }
/** * 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); } }
/** * 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); } }
/** * 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.equalHeaders(instance.dataset()) == false) throw new Exception("Incompatible instance types\n" + m_Train.equalHeadersMsg(instance.dataset())); if ( instance.classIsMissing() ) return; m_Train.add(instance); // update relevant attributes ... update_m_Attributes(); }
/** * 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.equalHeaders(instance.dataset()) == false) throw new Exception("Incompatible instance types\n" + m_Train.equalHeadersMsg(instance.dataset())); if ( instance.classIsMissing() ) return; m_Train.add(instance); // update relevant attributes ... update_m_Attributes(); }
/** * 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\n" + m_Train.equalHeadersMsg(instance.dataset())); } if (instance.classIsMissing()) { return; } m_Train.add(instance); updateMinMax(instance); }
/** * 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<Attribute> enumAtts = m_Instances.enumerateAttributes(); int attIndex = 0; while (enumAtts.hasMoreElements()) { 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()); } }
/** * 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<Attribute> enumAtts = m_Instances.enumerateAttributes(); int attIndex = 0; while (enumAtts.hasMoreElements()) { 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()); } }
/** * Computes the mean vector for the given dataset. */ protected Vector computeMean(Instances data, double[] totalWeight, int aI) { Vector meanVector = new DenseVector(data.numAttributes() - 1); totalWeight[aI] = 0; for (Instance inst : data) { if (!inst.classIsMissing()) { meanVector.add(inst.weight(), instanceToVector(inst)); totalWeight[aI] += inst.weight(); } } meanVector.scale(1.0 / totalWeight[aI]); return meanVector; }
/** * 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(); } } }
/** * 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(); } } }