for(Instance i: testInstances){ double [] distributions = classifier.distributionForInstance(i); for(int index = 0; index < distributions.length; index++) System.out.println(i.classAttribute().value(index) + ": " + distributions[index]); }
/** * Classify the test instance with the rule learner and provide the class * distributions * * @param datum the instance to be classified * @return the distribution */ @Override public double[] distributionForInstance(Instance datum) { try { for (int i = 0; i < m_Ruleset.size(); i++) { Rule rule = m_Ruleset.get(i); if (rule.covers(datum)) { return m_Distributions.get(i); } } } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); } System.err.println("Should never happen!"); return new double[datum.classAttribute().numValues()]; }
/** * Classify the test instance with the rule learner and provide the class * distributions * * @param datum the instance to be classified * @return the distribution */ @Override public double[] distributionForInstance(Instance datum) { try { for (int i = 0; i < m_Ruleset.size(); i++) { Rule rule = m_Ruleset.get(i); if (rule.covers(datum)) { return m_Distributions.get(i); } } } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); } System.err.println("Should never happen!"); return new double[datum.classAttribute().numValues()]; }
/** * 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(); }
public static String applyClassifier(PMMLModel model, Instances test) throws Exception { StringBuffer buff = new StringBuffer(); if (!(model instanceof PMMLClassifier)) { throw new Exception("PMML model is not a classifier!"); } double[] preds = null; PMMLClassifier classifier = (PMMLClassifier) model; for (int i = 0; i < test.numInstances(); i++) { buff.append("Actual: "); Instance temp = test.instance(i); if (temp.classAttribute().isNumeric()) { buff.append(temp.value(temp.classIndex()) + " "); } else { buff.append(temp.classAttribute().value( (int) temp.value(temp.classIndex())) + " "); } preds = classifier.distributionForInstance(temp); buff.append(" Predicted: "); for (double pred : preds) { buff.append("" + pred + " "); } buff.append("\n"); } return buff.toString(); }
/** * 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(); }
public static String applyClassifier(PMMLModel model, Instances test) throws Exception { StringBuffer buff = new StringBuffer(); if (!(model instanceof PMMLClassifier)) { throw new Exception("PMML model is not a classifier!"); } double[] preds = null; PMMLClassifier classifier = (PMMLClassifier) model; for (int i = 0; i < test.numInstances(); i++) { buff.append("Actual: "); Instance temp = test.instance(i); if (temp.classAttribute().isNumeric()) { buff.append(temp.value(temp.classIndex()) + " "); } else { buff.append(temp.classAttribute().value( (int) temp.value(temp.classIndex())) + " "); } preds = classifier.distributionForInstance(temp); buff.append(" Predicted: "); for (double pred : preds) { buff.append("" + pred + " "); } buff.append("\n"); } return buff.toString(); }
@Override public void updateNode(Instance inst) throws Exception { String trueClass = inst.classAttribute().value((int) inst.classValue()); int trueClassIndex = (int) inst.classValue(); if (majorityClass().equals(trueClass)) { m_majClassCorrectWeight += inst.weight(); } if (m_bayes.classifyInstance(inst) == trueClassIndex) { m_nbCorrectWeight += inst.weight(); } super.updateNode(inst); }
@Override public void updateNode(Instance inst) throws Exception { String trueClass = inst.classAttribute().value((int) inst.classValue()); int trueClassIndex = (int) inst.classValue(); if (majorityClass().equals(trueClass)) { m_majClassCorrectWeight += inst.weight(); } if (m_bayes.classifyInstance(inst) == trueClassIndex) { m_nbCorrectWeight += inst.weight(); } super.updateNode(inst); }
if (instance.classAttribute().isNumeric()) { sums[0] += m_Classifiers[i].classifyInstance(instance); } else { if (instance.classAttribute().isNumeric()) { sums[0] /= (double) m_NumIterations; return sums;
/** * 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 */ @Override protected void doPrintClassification(double[] dist, Instance inst, int index) throws Exception { PredictionContainer cont; cont = new PredictionContainer(); cont.instance = inst; if (inst.classAttribute().isNominal()) cont.prediction = new NominalPrediction(inst.classValue(), dist, inst.weight()); else cont.prediction = new NumericPrediction(inst.classValue(), dist[0], inst.weight()); cont.attributeValues.putAll(attributeValuesToMap(inst)); m_Predictions.add(cont); }
/** * 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 */ @Override protected void doPrintClassification(double[] dist, Instance inst, int index) throws Exception { PredictionContainer cont; cont = new PredictionContainer(); cont.instance = inst; if (inst.classAttribute().isNominal()) cont.prediction = new NominalPrediction(inst.classValue(), dist, inst.weight()); else cont.prediction = new NumericPrediction(inst.classValue(), dist[0], inst.weight()); cont.attributeValues.putAll(attributeValuesToMap(inst)); m_Predictions.add(cont); }
if (instance.classAttribute().isNumeric()) { unclassified = new double[1]; unclassified[0] = Utils.missingValue(); } else { unclassified = new double[instance.classAttribute().numValues()];
/** * Returns class probabilities for an instance. * * @param instance the instance to compute the distribution for * @return the class probabilities * @throws Exception if distribution can't be computed successfully */ @Override public double[] distributionForInstance(Instance inst) throws Exception { Attribute classAtt = inst.classAttribute(); double[] pred = new double[classAtt.numValues()]; if (m_root != null) { LeafNode l = m_root.leafForInstance(inst, null, null); HNode actualNode = l.m_theNode; if (actualNode == null) { actualNode = l.m_parentNode; } pred = actualNode.getDistribution(inst, classAtt); } else { // all class values equally likely for (int i = 0; i < classAtt.numValues(); i++) { pred[i] = 1; } Utils.normalize(pred); } // Utils.normalize(pred); return pred; }
/** * Returns class probabilities for an instance. * * @param instance the instance to compute the distribution for * @return the class probabilities * @throws Exception if distribution can't be computed successfully */ @Override public double[] distributionForInstance(Instance inst) throws Exception { Attribute classAtt = inst.classAttribute(); double[] pred = new double[classAtt.numValues()]; if (m_root != null) { LeafNode l = m_root.leafForInstance(inst, null, null); HNode actualNode = l.m_theNode; if (actualNode == null) { actualNode = l.m_parentNode; } pred = actualNode.getDistribution(inst, classAtt); } else { // all class values equally likely for (int i = 0; i < classAtt.numValues(); i++) { pred[i] = 1; } Utils.normalize(pred); } // Utils.normalize(pred); return pred; }
switch (instance.classAttribute().type()) { case Attribute.NOMINAL: double classification = classifyInstance(instance);
/** * Generate a single prediction for a test instance given the pre-trained * classifier. * * @param classifier the pre-trained Classifier to evaluate * @param test the test instance * @exception Exception if an error occurs */ public Prediction getPrediction(Classifier classifier, Instance test) throws Exception { double actual = test.classValue(); double[] dist = classifier.distributionForInstance(test); if (test.classAttribute().isNominal()) { return new NominalPrediction(actual, dist, test.weight()); } else { return new NumericPrediction(actual, dist[0], test.weight()); } }
/** * Generate a single prediction for a test instance given the pre-trained * classifier. * * @param classifier the pre-trained Classifier to evaluate * @param test the test instance * @exception Exception if an error occurs */ public Prediction getPrediction(Classifier classifier, Instance test) throws Exception { double actual = test.classValue(); double[] dist = classifier.distributionForInstance(test); if (test.classAttribute().isNominal()) { return new NominalPrediction(actual, dist, test.weight()); } else { return new NumericPrediction(actual, dist[0], test.weight()); } }
@Override public void updateNode(Instance inst) throws Exception { super.updateDistribution(inst); for (int i = 0; i < inst.numAttributes(); i++) { Attribute a = inst.attribute(i); if (i != inst.classIndex()) { ConditionalSufficientStats stats = m_nodeStats.get(a.name()); if (stats == null) { if (a.isNumeric()) { stats = new GaussianConditionalSufficientStats(); } else { stats = new NominalConditionalSufficientStats(); } m_nodeStats.put(a.name(), stats); } stats .update(inst.value(a), inst.classAttribute().value((int) inst.classValue()), inst.weight()); } } }
@Override public void updateNode(Instance inst) throws Exception { super.updateDistribution(inst); for (int i = 0; i < inst.numAttributes(); i++) { Attribute a = inst.attribute(i); if (i != inst.classIndex()) { ConditionalSufficientStats stats = m_nodeStats.get(a.name()); if (stats == null) { if (a.isNumeric()) { stats = new GaussianConditionalSufficientStats(); } else { stats = new NominalConditionalSufficientStats(); } m_nodeStats.put(a.name(), stats); } stats .update(inst.value(a), inst.classAttribute().value((int) inst.classValue()), inst.weight()); } } }