List of usage examples for weka.core Utils padLeft
public static String padLeft(String inString, int length)
From source file:bme.mace.logicdomain.Evaluation.java
License:Open Source License
/** * store the prediction made by the classifier as a string * /* w ww. j ava2 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:cotraining.copy.Evaluation_D.java
License:Open Source License
/** * store the prediction made by the classifier as a string * //from ww w . ja va2s . 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:LogReg.Logistic.java
License:Open Source License
/** * Gets a string describing the classifier. * * @return a string describing the classifer built. *//*from w w w . j av a 2s .c om*/ public String toString() { StringBuffer temp = new StringBuffer(); String result = ""; temp.append("Logistic Regression with ridge parameter of " + m_Ridge); if (m_Par == null) { return result + ": No model built yet."; } // find longest attribute name int attLength = 0; for (int i = 0; i < m_structure.numAttributes(); i++) { if (i != m_structure.classIndex() && m_structure.attribute(i).name().length() > attLength) { attLength = m_structure.attribute(i).name().length(); } } if ("Intercept".length() > attLength) { attLength = "Intercept".length(); } if ("Variable".length() > attLength) { attLength = "Variable".length(); } attLength += 2; int colWidth = 0; // check length of class names for (int i = 0; i < m_structure.classAttribute().numValues() - 1; i++) { if (m_structure.classAttribute().value(i).length() > colWidth) { colWidth = m_structure.classAttribute().value(i).length(); } } // check against coefficients and odds ratios for (int j = 1; j <= m_NumPredictors; j++) { for (int k = 0; k < m_NumClasses - 1; k++) { if (Utils.doubleToString(m_Par[j][k], 12, 4).trim().length() > colWidth) { colWidth = Utils.doubleToString(m_Par[j][k], 12, 4).trim().length(); } double ORc = Math.exp(m_Par[j][k]); String t = " " + ((ORc > 1e10) ? "" + ORc : Utils.doubleToString(ORc, 12, 4)); if (t.trim().length() > colWidth) { colWidth = t.trim().length(); } } } if ("Class".length() > colWidth) { colWidth = "Class".length(); } colWidth += 2; temp.append("\nCoefficients...\n"); temp.append(Utils.padLeft(" ", attLength) + Utils.padLeft("Class", colWidth) + "\n"); temp.append(Utils.padRight("Variable", attLength)); for (int i = 0; i < m_NumClasses - 1; i++) { String className = m_structure.classAttribute().value(i); temp.append(Utils.padLeft(className, colWidth)); } temp.append("\n"); int separatorL = attLength + ((m_NumClasses - 1) * colWidth); for (int i = 0; i < separatorL; i++) { temp.append("="); } temp.append("\n"); int j = 1; for (int i = 0; i < m_structure.numAttributes(); i++) { if (i != m_structure.classIndex()) { temp.append(Utils.padRight(m_structure.attribute(i).name(), attLength)); for (int k = 0; k < m_NumClasses - 1; k++) { temp.append(Utils.padLeft(Utils.doubleToString(m_Par[j][k], 12, 4).trim(), colWidth)); } temp.append("\n"); j++; } } temp.append(Utils.padRight("Intercept", attLength)); for (int k = 0; k < m_NumClasses - 1; k++) { temp.append(Utils.padLeft(Utils.doubleToString(m_Par[0][k], 10, 4).trim(), colWidth)); } temp.append("\n"); temp.append("\n\nOdds Ratios...\n"); temp.append(Utils.padLeft(" ", attLength) + Utils.padLeft("Class", colWidth) + "\n"); temp.append(Utils.padRight("Variable", attLength)); for (int i = 0; i < m_NumClasses - 1; i++) { String className = m_structure.classAttribute().value(i); temp.append(Utils.padLeft(className, colWidth)); } temp.append("\n"); for (int i = 0; i < separatorL; i++) { temp.append("="); } temp.append("\n"); j = 1; for (int i = 0; i < m_structure.numAttributes(); i++) { if (i != m_structure.classIndex()) { temp.append(Utils.padRight(m_structure.attribute(i).name(), attLength)); for (int k = 0; k < m_NumClasses - 1; k++) { double ORc = Math.exp(m_Par[j][k]); String ORs = " " + ((ORc > 1e10) ? "" + ORc : Utils.doubleToString(ORc, 12, 4)); temp.append(Utils.padLeft(ORs.trim(), colWidth)); } temp.append("\n"); j++; } } return temp.toString(); }
From source file:meka.classifiers.multilabel.incremental.IncrementalEvaluation.java
License:Open Source License
/** * EvaluateModelBatchWindow - Evaluate a multi-label data-stream model over windows. * @param h Multilabel Classifier/* ww w. ja va 2 s.c o m*/ * @param D stream * @param numWindows number of windows * @param rLabeled labelled-ness (1.0 by default) * @param Top threshold option * @param Vop verbosity option * @return The Result on the final window (but it contains samples of all the other evaluated windows). * The window is sampled every N/numWindows instances, for a total of numWindows windows. */ public static Result evaluateModelBatchWindow(MultiLabelClassifier h, Instances D, int numWindows, double rLabeled, String Top, String Vop) throws Exception { if (h.getDebug()) System.out .println(":- Classifier -: " + h.getClass().getName() + ": " + Arrays.toString(h.getOptions())); int N = D.numInstances(); int L = D.classIndex(); // the Result to use Result result = null; // the samples of all windows ArrayList<HashMap<String, Object>> samples = new ArrayList<HashMap<String, Object>>(); long train_time = 0; long test_time = 0; int windowSize = (int) Math.floor(D.numInstances() / (double) numWindows); if (rLabeled * windowSize < 1.) throw new Exception("[Error] The ratio of labelled instances (" + rLabeled + ") is too small given the window size!"); double nth = 1. / rLabeled; // label every nth example Instances D_init = new Instances(D, 0, windowSize); // initial window if (h.getDebug()) { System.out.println("Training classifier on initial window ..."); } train_time = System.currentTimeMillis(); h.buildClassifier(D_init); // initial classifier train_time = System.currentTimeMillis() - train_time; if (h.getDebug()) { System.out.println("Done (in " + (train_time / 1000.0) + " s)"); } D = new Instances(D, windowSize, D.numInstances() - windowSize); // the rest (after the initial window) double t[] = new double[L]; Arrays.fill(t, 0.5); int V = MLUtils.getIntegerOption(Vop, 3); if (h.getDebug()) { System.out.println("--------------------------------------------------------------------------------"); System.out.print("#" + Utils.padLeft("w", 6) + " " + Utils.padLeft("n", 6)); for (String m : measures) { System.out.print(" "); System.out.print(Utils.padLeft(m, 12)); } System.out.println(""); System.out.println("--------------------------------------------------------------------------------"); } int i = 0; for (int w = 0; w < numWindows - 1; w++) { // For each evaluation window ... result = new Result(L); result.setInfo("Supervision", String.valueOf(rLabeled)); result.setInfo("Type", "MLi"); int n = 0; test_time = 0; train_time = 0; for (int c = 0; i < (w * windowSize) + windowSize; i++) { // For each instance in the evaluation window ... Instance x = D.instance(i); AbstractInstance x_ = (AbstractInstance) ((AbstractInstance) x).copy(); // copy // (we can't clear the class values because certain classifiers need to know how well they're doing -- just trust that there's no cheating!) //for(int j = 0; j < L; j++) // x_.setValue(j,0.0); if (rLabeled < 0.5 && (i % (int) (1 / rLabeled) == 0) || (rLabeled >= 0.5 && (i % (int) (1. / (1. - rLabeled)) != 0))) { // LABELLED - Test & record prediction long before_test = System.currentTimeMillis(); double y[] = h.distributionForInstance(x_); long after_test = System.currentTimeMillis(); test_time += (after_test - before_test); // was += result.addResult(y, x); n++; } else { // UNLABELLED x = MLUtils.setLabelsMissing(x, L); } // Update the classifier. (The classifier will have to decide if it wants to deal with unlabelled instances.) long before = System.currentTimeMillis(); ((UpdateableClassifier) h).updateClassifier(x); long after = System.currentTimeMillis(); train_time += (after - before); // was += } // calculate results result.setInfo("Threshold", Arrays.toString(t)); result.output = Result.getStats(result, Vop); result.setMeasurement("Test time", (test_time) / 1000.0); result.setMeasurement("Build time", (train_time) / 1000.0); result.setMeasurement("Total time", (test_time + train_time) / 1000.0); result.setMeasurement("Threshold", (double) t[0]); result.setMeasurement("Instances", (double) i); result.setMeasurement("Samples", (double) (samples.size() + 1)); samples.add(result.output); // Display results (to CLI) if (h.getDebug()) { System.out.print("#" + Utils.doubleToString((double) w + 1, 6, 0) + " " + Utils.doubleToString((double) n, 6, 0)); n = 0; for (String m : measures) { System.out.print(" "); System.out.print(Utils.doubleToString((Double) result.getMeasurement(m), 12, 4)); } System.out.println(""); } // Calibrate threshold for next window if (Top.equals("PCutL")) { t = ThresholdUtils.calibrateThresholds(result.predictions, MLUtils.labelCardinalities(result.actuals)); } else { Arrays.fill(t, ThresholdUtils.calibrateThreshold(result.predictions, MLUtils.labelCardinality(result.allTrueValues()))); } } if (h.getDebug()) { System.out.println("--------------------------------------------------------------------------------"); } // This is the last Result; prepare it for evaluation output. result.setInfo("Classifier", h.getClass().getName()); result.vals.put("Test time", (test_time) / 1000.0); result.vals.put("Build time", (train_time) / 1000.0); result.vals.put("Total time", (test_time + train_time) / 1000.0); result.vals.put("Total instances tested", (double) i); result.vals.put("Initial instances for training", (double) windowSize); result.setInfo("Options", Arrays.toString(h.getOptions())); result.setInfo("Additional Info", h.toString()); result.setInfo("Dataset", MLUtils.getDatasetName(D)); result.output = Result.getStats(result, Vop); result.setMeasurement("Results sampled over time", Result.getResultsAsInstances(samples)); return result; }