List of usage examples for weka.core Instance classValue
public double classValue();
From source file:ID3Chi.java
License:Open Source License
private double[] GetClassCounts(Instances data) { double[] classCounts = new double[data.numClasses()]; Enumeration instEnum = data.enumerateInstances(); while (instEnum.hasMoreElements()) { Instance inst = (Instance) instEnum.nextElement(); classCounts[(int) inst.classValue()]++; }/*from w w w . ja v a2 s. c o m*/ return classCounts; }
From source file:MPCKMeans.java
License:Open Source License
/** Outputs the current clustering * * @exception Exception if something goes wrong *//*from w w w .j a v a 2 s .c o m*/ public void printIndexClusters() throws Exception { if (m_IndexClusters == null) throw new Exception("Clusters were not created"); for (int i = 0; i < m_NumClusters; i++) { HashSet cluster = m_IndexClusters[i]; if (cluster == null) { System.out.println("Cluster " + i + " is null"); } else { System.out.println("Cluster " + i + " consists of " + cluster.size() + " elements"); Iterator iter = cluster.iterator(); while (iter.hasNext()) { int idx = ((Integer) iter.next()).intValue(); Instance inst = m_TotalTrainWithLabels.instance(idx); if (m_TotalTrainWithLabels.classIndex() >= 0) { System.out .println("\t\t" + idx + ":" + inst.classAttribute().value((int) inst.classValue())); } } } } }
From source file:asap.PostProcess.java
private void generateGoldStandardFile(Instances instances) { File tmp;/* w w w . j a v a 2 s. co m*/ try { tmp = File.createTempFile("input", ".tmp", new File(".")); tmp.deleteOnExit(); } catch (IOException ex) { Logger.getLogger(PostProcess.class.getName()).log(Level.SEVERE, null, ex); return; } goldStandardFile = tmp.getAbsolutePath(); FileOutputStream fos; try { fos = new FileOutputStream(tmp); } catch (FileNotFoundException ex) { Logger.getLogger(PostProcess.class.getName()).log(Level.SEVERE, null, ex); return; } for (weka.core.Instance instance : instances) { try { fos.write((instance.classValue() + "\n").getBytes()); } catch (IOException ex) { Logger.getLogger(PostProcess.class.getName()).log(Level.SEVERE, null, ex); return; } } try { fos.flush(); fos.close(); } catch (IOException ex) { Logger.getLogger(PostProcess.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * Evaluates the classifier on a single instance and records the prediction * (if the class is nominal)./*from w w w.j a v a 2 s. co m*/ * * @param classifier machine learning classifier * @param instance the test instance to be classified * @return the prediction made by the clasifier * @throws Exception if model could not be evaluated successfully or the data * contains string attributes */ public double evaluateModelOnceAndRecordPrediction(List<LibSVM> classifier, List<Double> classifierWeight, Instance instance) throws Exception { Instance classMissing = (Instance) instance.copy(); double pred = 0; classMissing.setDataset(instance.dataset()); classMissing.setClassMissing(); if (m_ClassIsNominal) { if (m_Predictions == null) { m_Predictions = new FastVector(); } List<double[]> prob = new ArrayList<double[]>();// double[] finalProb = new double[instance.numClasses()]; for (int i = 0; i < classifier.size(); i++) { double[] dist = classifier.get(i).distributionForInstance(classMissing);// prob.add(dist); } for (int i = 0; i < finalProb.length; i++) { for (int j = 0; j < classifier.size(); j++) { finalProb[i] += prob.get(j)[i] * classifierWeight.get(j); } } double sum = 0; for (int i = 0; i < finalProb.length; i++) { sum += finalProb[i]; } for (int i = 0; i < finalProb.length; i++) { finalProb[i] = finalProb[i] / sum; } pred = Utils.maxIndex(finalProb); if (finalProb[(int) pred] <= 0) { pred = Instance.missingValue(); } updateStatsForClassifier(finalProb, instance); m_Predictions.addElement(new NominalPrediction(instance.classValue(), finalProb, instance.weight())); } else { pred = classifier.get(0).classifyInstance(classMissing); updateStatsForPredictor(pred, instance); } return pred; }
From source file:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * Evaluates the supplied distribution on a single instance. * //from www .jav a 2 s . c o m * @param dist the supplied distribution * @param instance the test instance to be classified * @return the prediction * @throws Exception if model could not be evaluated successfully */ public double evaluateModelOnceAndRecordPrediction(double[] dist, Instance instance) throws Exception { double pred; if (m_ClassIsNominal) { if (m_Predictions == null) { m_Predictions = new FastVector(); } pred = Utils.maxIndex(dist); if (dist[(int) pred] <= 0) { pred = Instance.missingValue(); } updateStatsForClassifier(dist, instance); m_Predictions.addElement(new NominalPrediction(instance.classValue(), dist, instance.weight())); } else { pred = dist[0]; updateStatsForPredictor(pred, instance); } return pred; }
From source file:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * Sets the class prior probabilities/*from www . j a v a 2 s .co m*/ * * @param train the training instances used to determine the prior * probabilities * @throws Exception if the class attribute of the instances is not set */ public void setPriors(Instances train) throws Exception { m_NoPriors = false; if (!m_ClassIsNominal) { m_NumTrainClassVals = 0; m_TrainClassVals = null; m_TrainClassWeights = null; m_PriorErrorEstimator = null; m_ErrorEstimator = null; for (int i = 0; i < train.numInstances(); i++) { Instance currentInst = train.instance(i); if (!currentInst.classIsMissing()) { addNumericTrainClass(currentInst.classValue(), currentInst.weight()); } } } else { for (int i = 0; i < m_NumClasses; i++) { m_ClassPriors[i] = 1; } m_ClassPriorsSum = m_NumClasses; for (int i = 0; i < train.numInstances(); i++) { if (!train.instance(i).classIsMissing()) { m_ClassPriors[(int) train.instance(i).classValue()] += train.instance(i).weight(); m_ClassPriorsSum += train.instance(i).weight(); } } } }
From source file:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * Updates the class prior probabilities (when incrementally training) * //from w w w . j a va2s.com * @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) { if (!instance.classIsMissing()) { addNumericTrainClass(instance.classValue(), instance.weight()); } } else { m_ClassPriors[(int) instance.classValue()] += instance.weight(); m_ClassPriorsSum += instance.weight(); } } }
From source file:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * store the prediction made by the classifier as a string * //w w w.j av a2 s . c o m * @param classifier the classifier to use * @param inst the instance to generate text from * @param instNum the index in the dataset * @param attributesToOutput the indices of the attributes to output * @param printDistribution prints the complete distribution for nominal * classes, not just the predicted value * @return the prediction as a String * @throws Exception if something goes wrong * @see #printClassifications(Classifier, Instances, String, int, Range, * boolean) */ protected static String predictionText(Classifier classifier, Instance inst, int instNum, Range attributesToOutput, boolean printDistribution) throws Exception { StringBuffer result = new StringBuffer(); int width = 10; int prec = 3; Instance withMissing = (Instance) inst.copy(); withMissing.setDataset(inst.dataset()); withMissing.setMissing(withMissing.classIndex()); double predValue = classifier.classifyInstance(withMissing); // index result.append(Utils.padLeft("" + (instNum + 1), 6)); if (inst.dataset().classAttribute().isNumeric()) { // actual if (inst.classIsMissing()) { result.append(" " + Utils.padLeft("?", width)); } else { result.append(" " + Utils.doubleToString(inst.classValue(), width, prec)); } // predicted if (Instance.isMissingValue(predValue)) { result.append(" " + Utils.padLeft("?", width)); } else { result.append(" " + Utils.doubleToString(predValue, width, prec)); } // error if (Instance.isMissingValue(predValue) || inst.classIsMissing()) { result.append(" " + Utils.padLeft("?", width)); } else { result.append(" " + Utils.doubleToString(predValue - inst.classValue(), width, prec)); } } else { // actual result.append(" " + Utils.padLeft(((int) inst.classValue() + 1) + ":" + inst.toString(inst.classIndex()), width)); // predicted if (Instance.isMissingValue(predValue)) { result.append(" " + Utils.padLeft("?", width)); } else { result.append(" " + Utils.padLeft( ((int) predValue + 1) + ":" + inst.dataset().classAttribute().value((int) predValue), width)); } // error? if (!Instance.isMissingValue(predValue) && !inst.classIsMissing() && ((int) predValue + 1 != (int) inst.classValue() + 1)) { result.append(" " + " + "); } else { result.append(" " + " "); } // prediction/distribution if (printDistribution) { if (Instance.isMissingValue(predValue)) { result.append(" " + "?"); } else { result.append(" "); double[] dist = classifier.distributionForInstance(withMissing); for (int n = 0; n < dist.length; n++) { if (n > 0) { result.append(","); } if (n == (int) predValue) { result.append("*"); } result.append(Utils.doubleToString(dist[n], prec)); } } } else { if (Instance.isMissingValue(predValue)) { result.append(" " + "?"); } else { result.append(" " + Utils.doubleToString( classifier.distributionForInstance(withMissing)[(int) predValue], prec)); } } } // attributes result.append(" " + attributeValuesString(withMissing, attributesToOutput) + "\n"); return result.toString(); }
From source file:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * Updates all the statistics about a classifiers performance for the current * test instance.//from w w w.jav a 2 s .c o m * * @param predictedDistribution the probabilities assigned to each class * @param instance the instance to be classified * @throws Exception if the class of the instance is not set */ protected void updateStatsForClassifier(double[] predictedDistribution, Instance instance) throws Exception { int actualClass = (int) instance.classValue(); if (!instance.classIsMissing()) { updateMargins(predictedDistribution, actualClass, instance.weight()); // Determine the predicted class (doesn't detect multiple // classifications) int predictedClass = -1; double bestProb = 0.0; for (int i = 0; i < m_NumClasses; i++) { if (predictedDistribution[i] > bestProb) { predictedClass = i; bestProb = predictedDistribution[i]; } } m_WithClass += instance.weight(); // Determine misclassification cost if (m_CostMatrix != null) { if (predictedClass < 0) { // For missing predictions, we assume the worst possible cost. // This is pretty harsh. // Perhaps we could take the negative of the cost of a correct // prediction (-m_CostMatrix.getElement(actualClass,actualClass)), // although often this will be zero m_TotalCost += instance.weight() * m_CostMatrix.getMaxCost(actualClass, instance); } else { m_TotalCost += instance.weight() * m_CostMatrix.getElement(actualClass, predictedClass, instance); } } // Update counts when no class was predicted if (predictedClass < 0) { m_Unclassified += instance.weight(); return; } double predictedProb = Math.max(MIN_SF_PROB, predictedDistribution[actualClass]); double priorProb = Math.max(MIN_SF_PROB, m_ClassPriors[actualClass] / m_ClassPriorsSum); if (predictedProb >= priorProb) { m_SumKBInfo += (Utils.log2(predictedProb) - Utils.log2(priorProb)) * instance.weight(); } else { m_SumKBInfo -= (Utils.log2(1.0 - predictedProb) - Utils.log2(1.0 - priorProb)) * instance.weight(); } m_SumSchemeEntropy -= Utils.log2(predictedProb) * instance.weight(); m_SumPriorEntropy -= Utils.log2(priorProb) * instance.weight(); updateNumericScores(predictedDistribution, makeDistribution(instance.classValue()), instance.weight()); // Update other stats m_ConfusionMatrix[actualClass][predictedClass] += instance.weight(); if (predictedClass != actualClass) { m_Incorrect += instance.weight(); } else { m_Correct += instance.weight(); } } else { m_MissingClass += instance.weight(); } }
From source file:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * Updates all the statistics about a predictors performance for the current * test instance.//from ww w . j av a 2 s .c o m * * @param predictedValue the numeric value the classifier predicts * @param instance the instance to be classified * @throws Exception if the class of the instance is not set */ protected void updateStatsForPredictor(double predictedValue, Instance instance) throws Exception { if (!instance.classIsMissing()) { // Update stats m_WithClass += instance.weight(); if (Instance.isMissingValue(predictedValue)) { m_Unclassified += instance.weight(); return; } m_SumClass += instance.weight() * instance.classValue(); m_SumSqrClass += instance.weight() * instance.classValue() * instance.classValue(); m_SumClassPredicted += instance.weight() * instance.classValue() * predictedValue; m_SumPredicted += instance.weight() * predictedValue; m_SumSqrPredicted += instance.weight() * predictedValue * predictedValue; if (m_ErrorEstimator == null) { setNumericPriorsFromBuffer(); } double predictedProb = Math.max(m_ErrorEstimator.getProbability(predictedValue - instance.classValue()), MIN_SF_PROB); double priorProb = Math.max(m_PriorErrorEstimator.getProbability(instance.classValue()), MIN_SF_PROB); m_SumSchemeEntropy -= Utils.log2(predictedProb) * instance.weight(); m_SumPriorEntropy -= Utils.log2(priorProb) * instance.weight(); m_ErrorEstimator.addValue(predictedValue - instance.classValue(), instance.weight()); updateNumericScores(makeDistribution(predictedValue), makeDistribution(instance.classValue()), instance.weight()); } else { m_MissingClass += instance.weight(); } }