List of usage examples for weka.core Utils doubleToString
public staticString doubleToString(double value, int afterDecimalPoint)
From source file:Bilbo.java
License:Open Source License
/** * Returns description of the bagged classifier. * * @return description of the bagged classifier as a string *///from w ww . j a v a 2 s . c o m @Override public String toString() { if (m_Classifiers == null) { return "Bagging: No model built yet."; } StringBuffer text = new StringBuffer(); text.append("All the base classifiers: \n\n"); for (int i = 0; i < m_Classifiers.length; i++) text.append(m_Classifiers[i].toString() + "\n\n"); if (m_CalcOutOfBag) { text.append("Out of bag error: " + Utils.doubleToString(m_OutOfBagError, 4) + "\n\n"); } return text.toString(); }
From source file:BaggingImprove.java
/** * Returns description of the bagged classifier. * * @return description of the bagged classifier as a string *//*w w w .j a v a 2 s .c o m*/ @Override public String toString() { if (m_Classifiers == null) { return "Bagging: No model built yet."; } StringBuffer text = new StringBuffer(); text.append("All the base classifiers: \n\n"); for (int i = 0; i < m_Classifiers.length; i++) { text.append(m_Classifiers[i].toString() + "\n\n"); } if (m_CalcOutOfBag) { text.append("Out of bag error: " + Utils.doubleToString(m_OutOfBagError, 4) + "\n\n"); } return text.toString(); }
From source file:adams.gui.menu.ROC.java
License:Open Source License
/** * Launches the functionality of the menu item. *//*from w ww . j a v a 2s . c o m*/ @Override public void launch() { File file; if (m_Parameters.length == 0) { // choose file int retVal = m_FileChooser.showOpenDialog(null); if (retVal != JFileChooser.APPROVE_OPTION) return; file = m_FileChooser.getSelectedFile(); } else { file = new PlaceholderFile(m_Parameters[0]).getAbsoluteFile(); m_FileChooser.setSelectedFile(file); } // create plot Instances result; try { result = m_FileChooser.getLoader().getDataSet(); } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Error loading file '" + file + "':\n" + adams.core.Utils.throwableToString(e)); return; } result.setClassIndex(result.numAttributes() - 1); ThresholdVisualizePanel vmc = new ThresholdVisualizePanel(); vmc.setROCString("(Area under ROC = " + Utils.doubleToString(ThresholdCurve.getROCArea(result), 4) + ")"); vmc.setName(result.relationName()); PlotData2D tempd = new PlotData2D(result); tempd.setPlotName(result.relationName()); tempd.addInstanceNumberAttribute(); // specify which points are connected boolean[] cp = new boolean[result.numInstances()]; for (int n = 1; n < cp.length; n++) cp[n] = true; try { tempd.setConnectPoints(cp); vmc.addPlot(tempd); } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Error adding plot:\n" + adams.core.Utils.throwableToString(e)); return; } ChildFrame frame = createChildFrame(vmc, GUIHelper.getDefaultDialogDimension()); frame.setTitle(frame.getTitle() + " - " + file); }
From source file:ai.BalancedRandomForest.java
License:GNU General Public License
/** * Outputs a description of this classifier. * * @return a string containing a description of the classifier *//*from ww w .j ava 2s . c om*/ public String toString() { if (tree == null) return "Balanced random forest not built yet"; else return "Balanced random forest of " + this.numTrees + " trees, each constructed while considering " + this.numFeatures + " random feature" + (this.numFeatures == 1 ? "" : "s") + "\nOut of bag error: " + Utils.doubleToString(measureOutOfBagError(), 4) + ".\n"; }
From source file:br.ufrn.ia.core.clustering.EMIaProject.java
License:Open Source License
protected String toStringOriginal() { if (m_priors == null) { return "No clusterer built yet!"; }//from w w w . j ava2s . c o m StringBuffer temp = new StringBuffer(); temp.append("\nEM\n==\n"); if (m_initialNumClusters == -1) { temp.append("\nNumber of clusters selected by cross validation: " + m_num_clusters + "\n"); } else { temp.append("\nNumber of clusters: " + m_num_clusters + "\n"); } for (int j = 0; j < m_num_clusters; j++) { temp.append("\nCluster: " + j + " Prior probability: " + Utils.doubleToString(m_priors[j], 4) + "\n\n"); for (int i = 0; i < m_num_attribs; i++) { temp.append("Attribute: " + m_theInstances.attribute(i).name() + "\n"); if (m_theInstances.attribute(i).isNominal()) { if (m_model[j][i] != null) { temp.append(m_model[j][i].toString()); } } else { temp.append("Normal Distribution. Mean = " + Utils.doubleToString(m_modelNormal[j][i][0], 4) + " StdDev = " + Utils.doubleToString(m_modelNormal[j][i][1], 4) + "\n"); } } } return temp.toString(); }
From source file:cba.Apriori.java
License:Open Source License
/** * Outputs the size of all the generated sets of itemsets and the rules. * //from w w w . ja v a 2 s . com * @return a string representation of the model */ public String toString() { StringBuffer text = new StringBuffer(); if (m_Ls.size() <= 1) return "\nNo large itemsets and rules found!\n"; text.append("\nApriori\n=======\n\n"); text.append("Minimum support: " + Utils.doubleToString(m_minSupport, 2) + " (" + ((int) (m_minSupport * (double) m_instances.numInstances() + 0.5)) + " instances)" + '\n'); text.append("Minimum metric <"); switch (m_metricType) { case CONFIDENCE: text.append("confidence>: "); break; case LIFT: text.append("lift>: "); break; case LEVERAGE: text.append("leverage>: "); break; case CONVICTION: text.append("conviction>: "); break; } text.append(Utils.doubleToString(m_minMetric, 2) + '\n'); if (m_significanceLevel != -1) text.append("Significance level: " + Utils.doubleToString(m_significanceLevel, 2) + '\n'); text.append("Number of cycles performed: " + m_cycles + '\n'); text.append("\nGenerated sets of large itemsets:\n"); if (!m_car) { for (int i = 0; i < m_Ls.size(); i++) { text.append("\nSize of set of large itemsets L(" + (i + 1) + "): " + ((FastVector) m_Ls.elementAt(i)).size() + '\n'); if (m_outputItemSets) { text.append("\nLarge Itemsets L(" + (i + 1) + "):\n"); for (int j = 0; j < ((FastVector) m_Ls.elementAt(i)).size(); j++) text.append(((AprioriItemSet) ((FastVector) m_Ls.elementAt(i)).elementAt(j)) .toString(m_instances) + "\n"); } } text.append("\nBest rules found:\n\n"); for (int i = 0; i < m_allTheRules[0].size(); i++) { text.append(Utils.doubleToString((double) i + 1, (int) (Math.log(m_numRules) / Math.log(10) + 1), 0) + ". " + ((AprioriItemSet) m_allTheRules[0].elementAt(i)).toString(m_instances) + " ==> " + ((AprioriItemSet) m_allTheRules[1].elementAt(i)).toString(m_instances) + " conf:(" + Utils.doubleToString(((Double) m_allTheRules[2].elementAt(i)).doubleValue(), 2) + ")"); if (m_metricType != CONFIDENCE || m_significanceLevel != -1) { text.append((m_metricType == LIFT ? " <" : "") + " lift:(" + Utils.doubleToString(((Double) m_allTheRules[3].elementAt(i)).doubleValue(), 2) + ")" + (m_metricType == LIFT ? ">" : "")); text.append((m_metricType == LEVERAGE ? " <" : "") + " lev:(" + Utils.doubleToString(((Double) m_allTheRules[4].elementAt(i)).doubleValue(), 2) + ")"); text.append(" [" + (int) (((Double) m_allTheRules[4].elementAt(i)).doubleValue() * (double) m_instances.numInstances()) + "]" + (m_metricType == LEVERAGE ? ">" : "")); text.append((m_metricType == CONVICTION ? " <" : "") + " conv:(" + Utils.doubleToString(((Double) m_allTheRules[5].elementAt(i)).doubleValue(), 2) + ")" + (m_metricType == CONVICTION ? ">" : "")); } text.append('\n'); } } else { for (int i = 0; i < m_Ls.size(); i++) { text.append("\nSize of set of large itemsets L(" + (i + 1) + "): " + ((FastVector) m_Ls.elementAt(i)).size() + '\n'); if (m_outputItemSets) { text.append("\nLarge Itemsets L(" + (i + 1) + "):\n"); for (int j = 0; j < ((FastVector) m_Ls.elementAt(i)).size(); j++) { text.append(((ItemSet) ((FastVector) m_Ls.elementAt(i)).elementAt(j)).toString(m_instances) + "\n"); text.append(((LabeledItemSet) ((FastVector) m_Ls.elementAt(i)).elementAt(j)).m_classLabel + " "); text.append( ((LabeledItemSet) ((FastVector) m_Ls.elementAt(i)).elementAt(j)).support() + "\n"); } } } text.append("\nBest rules found:\n\n"); for (int i = 0; i < m_allTheRules[0].size(); i++) { text.append(Utils.doubleToString((double) i + 1, (int) (Math.log(m_numRules) / Math.log(10) + 1), 0) + ". " + ((ItemSet) m_allTheRules[0].elementAt(i)).toString(m_instances) + " ==> " + ((ItemSet) m_allTheRules[1].elementAt(i)).toString(m_onlyClass) + " conf:(" + Utils.doubleToString(((Double) m_allTheRules[2].elementAt(i)).doubleValue(), 2) + ")"); text.append('\n'); } } return text.toString(); }
From source file:com.github.polarisation.kea.main.KEAKeyphraseExtractor.java
License:Open Source License
/** * Builds the model from the files/*from w ww . j a va 2s.c o m*/ */ public void extractKeyphrases(Hashtable stems) throws Exception { Vector stats = new Vector(); // Check whether there is actually any data // = if there any files in the directory if (stems.size() == 0) { throw new Exception("Couldn't find any data!"); } m_KEAFilter.setNumPhrases(m_numPhrases); m_KEAFilter.setVocabulary(m_vocabulary); m_KEAFilter.setVocabularyFormat(m_vocabularyFormat); m_KEAFilter.setDocumentLanguage(getDocumentLanguage()); m_KEAFilter.setStemmer(m_Stemmer); m_KEAFilter.setStopwords(m_Stopwords); if (getVocabulary().equals("none")) { m_KEAFilter.m_NODEfeature = false; } else { m_KEAFilter.loadThesaurus(m_Stemmer, m_Stopwords); } FastVector atts = new FastVector(3); atts.addElement(new Attribute("doc", (FastVector) null)); atts.addElement(new Attribute("keyphrases", (FastVector) null)); atts.addElement(new Attribute("filename", (String) null)); Instances data = new Instances("keyphrase_training_data", atts, 0); if (m_KEAFilter.m_Dictionary == null) { buildGlobalDictionaries(stems); } System.err.println("-- Extracting Keyphrases... "); // Extract keyphrases Enumeration elem = stems.keys(); // Enumeration over all files in the directory (now in the hash): while (elem.hasMoreElements()) { String str = (String) elem.nextElement(); double[] newInst = new double[2]; try { File txt = new File(m_dirName + "/" + str + ".txt"); InputStreamReader is; if (!m_encoding.equals("default")) { is = new InputStreamReader(new FileInputStream(txt), m_encoding); } else { is = new InputStreamReader(new FileInputStream(txt)); } StringBuffer txtStr = new StringBuffer(); int c; while ((c = is.read()) != -1) { txtStr.append((char) c); } newInst[0] = (double) data.attribute(0).addStringValue(txtStr.toString()); } catch (Exception e) { if (m_debug) { System.err.println("Can't read document " + str + ".txt"); } newInst[0] = Instance.missingValue(); } try { File key = new File(m_dirName + "/" + str + ".key"); InputStreamReader is; if (!m_encoding.equals("default")) { is = new InputStreamReader(new FileInputStream(key), m_encoding); } else { is = new InputStreamReader(new FileInputStream(key)); } StringBuffer keyStr = new StringBuffer(); int c; // keyStr = keyphrases in the str.key file // Kea assumes, that these keyphrases were assigned by the author // and evaluates extracted keyphrases againse these while ((c = is.read()) != -1) { keyStr.append((char) c); } newInst[1] = (double) data.attribute(1).addStringValue(keyStr.toString()); } catch (Exception e) { if (m_debug) { System.err.println("No existing keyphrases for stem " + str + "."); } newInst[1] = Instance.missingValue(); } data.add(new Instance(1.0, newInst)); m_KEAFilter.input(data.instance(0)); data = data.stringFreeStructure(); if (m_debug) { System.err.println("-- Document: " + str); } Instance[] topRankedInstances = new Instance[m_numPhrases]; Instance inst; // Iterating over all extracted keyphrases (inst) while ((inst = m_KEAFilter.output()) != null) { int index = (int) inst.value(m_KEAFilter.getRankIndex()) - 1; if (index < m_numPhrases) { topRankedInstances[index] = inst; } } if (m_debug) { System.err.println("-- Keyphrases and feature values:"); } FileOutputStream out = null; PrintWriter printer = null; File key = new File(m_dirName + "/" + str + ".key"); if (!key.exists()) { out = new FileOutputStream(m_dirName + "/" + str + ".key"); if (!m_encoding.equals("default")) { printer = new PrintWriter(new OutputStreamWriter(out, m_encoding)); } else { printer = new PrintWriter(out); } } double numExtracted = 0, numCorrect = 0; for (int i = 0; i < m_numPhrases; i++) { if (topRankedInstances[i] != null) { if (!topRankedInstances[i].isMissing(topRankedInstances[i].numAttributes() - 1)) { numExtracted += 1.0; } if ((int) topRankedInstances[i].value(topRankedInstances[i].numAttributes() - 1) == 1) { numCorrect += 1.0; } if (printer != null) { printer.print(topRankedInstances[i].stringValue(m_KEAFilter.getUnstemmedPhraseIndex())); if (m_AdditionalInfo) { printer.print("\t"); printer.print(topRankedInstances[i].stringValue(m_KEAFilter.getStemmedPhraseIndex())); printer.print("\t"); printer.print(Utils.doubleToString( topRankedInstances[i].value(m_KEAFilter.getProbabilityIndex()), 4)); } printer.println(); } if (m_debug) { System.err.println(topRankedInstances[i]); } } } if (numExtracted > 0) { if (m_debug) { System.err.println("-- " + numCorrect + " correct"); } stats.addElement(new Double(numCorrect)); } if (printer != null) { printer.flush(); printer.close(); out.close(); } } double[] st = new double[stats.size()]; for (int i = 0; i < stats.size(); i++) { st[i] = ((Double) stats.elementAt(i)).doubleValue(); } double avg = Utils.mean(st); double stdDev = Math.sqrt(Utils.variance(st)); System.err.println("Avg. number of matching keyphrases compared to existing ones : " + Utils.doubleToString(avg, 2) + " +/- " + Utils.doubleToString(stdDev, 2)); System.err.println("Based on " + stats.size() + " documents"); // m_KEAFilter.batchFinished(); }
From source file:com.jgaap.util.Instance.java
License:Open Source License
/** * Returns the description of one instance. If the instance * doesn't have access to a dataset, it returns the internal * floating-point values. Quotes string//from w w w .ja va 2 s . c om * values that contain whitespace characters. * * @return the instance's description as a string */ public String toString() { StringBuffer text = new StringBuffer(); for (int i = 0; i < m_AttValues.length; i++) { if (i > 0) text.append(","); text.append(toString(i)); } if (m_Weight != 1.0) { text.append(",{" + Utils.doubleToString(m_Weight, 6) + "}"); } return text.toString(); }
From source file:com.openkm.kea.modelcreator.KEAKeyphraseExtractor.java
License:Open Source License
/** * Builds the model from the files/* w w w.j a va2 s .c o m*/ */ public void extractKeyphrases(Hashtable<String, Double> stems) throws Exception { Vector<Double> stats = new Vector<Double>(); // Check whether there is actually any data // = if there any files in the directory if (stems.size() == 0) { throw new Exception("Couldn't find any data!"); } m_KEAFilter.setNumPhrases(m_numPhrases); m_KEAFilter.setVocabulary(m_vocabulary); m_KEAFilter.setVocabularyFormat(m_vocabularyFormat); m_KEAFilter.setDocumentLanguage(getDocumentLanguage()); m_KEAFilter.setStemmer(m_Stemmer); m_KEAFilter.setStopwords(m_Stopwords); if (getVocabulary().equals("none")) { m_KEAFilter.m_NODEfeature = false; } else { m_KEAFilter.loadThesaurus(m_Stemmer, m_Stopwords); } FastVector atts = new FastVector(3); atts.addElement(new Attribute("doc", (FastVector) null)); atts.addElement(new Attribute("keyphrases", (FastVector) null)); atts.addElement(new Attribute("filename", (String) null)); Instances data = new Instances("keyphrase_training_data", atts, 0); if (m_KEAFilter.m_Dictionary == null) { buildGlobalDictionaries(stems); } log.info("-- Extracting Keyphrases... "); // Extract keyphrases Enumeration<String> elem = stems.keys(); // Enumeration over all files in the directory (now in the hash): while (elem.hasMoreElements()) { String str = elem.nextElement(); double[] newInst = new double[2]; try { File txt = new File(m_dirName + "/" + str + ".txt"); InputStreamReader is; if (!m_encoding.equals("default")) { is = new InputStreamReader(new FileInputStream(txt), m_encoding); } else { is = new InputStreamReader(new FileInputStream(txt)); } StringBuffer txtStr = new StringBuffer(); int c; while ((c = is.read()) != -1) { txtStr.append((char) c); } newInst[0] = (double) data.attribute(0).addStringValue(txtStr.toString()); } catch (Exception e) { if (m_debug) { log.debug("Can't read document " + str + ".txt"); } newInst[0] = Instance.missingValue(); } try { File key = new File(m_dirName + "/" + str + ".key"); InputStreamReader is; if (!m_encoding.equals("default")) { is = new InputStreamReader(new FileInputStream(key), m_encoding); } else { is = new InputStreamReader(new FileInputStream(key)); } StringBuffer keyStr = new StringBuffer(); int c; // keyStr = keyphrases in the str.key file // Kea assumes, that these keyphrases were assigned by the // author // and evaluates extracted keyphrases againse these while ((c = is.read()) != -1) { keyStr.append((char) c); } newInst[1] = (double) data.attribute(1).addStringValue(keyStr.toString()); } catch (Exception e) { if (m_debug) { log.debug("No existing keyphrases for stem " + str + "."); } newInst[1] = Instance.missingValue(); } data.add(new Instance(1.0, newInst)); m_KEAFilter.input(data.instance(0)); data = data.stringFreeStructure(); if (m_debug) { log.debug("-- Document: " + str); } Instance[] topRankedInstances = new Instance[m_numPhrases]; Instance inst; // Iterating over all extracted keyphrases (inst) while ((inst = m_KEAFilter.output()) != null) { int index = (int) inst.value(m_KEAFilter.getRankIndex()) - 1; if (index < m_numPhrases) { topRankedInstances[index] = inst; } } if (m_debug) { log.debug("-- Keyphrases and feature values:"); } FileOutputStream out = null; PrintWriter printer = null; File key = new File(m_dirName + "/" + str + ".key"); if (!key.exists()) { out = new FileOutputStream(m_dirName + "/" + str + ".key"); if (!m_encoding.equals("default")) { printer = new PrintWriter(new OutputStreamWriter(out, m_encoding)); } else { printer = new PrintWriter(out); } } double numExtracted = 0, numCorrect = 0; for (int i = 0; i < m_numPhrases; i++) { if (topRankedInstances[i] != null) { // My addition: to exclude low ranking phrases double rank = topRankedInstances[i].value(m_KEAFilter.getProbabilityIndex()); if (rank >= 0.00) { if (!topRankedInstances[i].isMissing(topRankedInstances[i].numAttributes() - 1)) { numExtracted += 1.0; } if ((int) topRankedInstances[i].value(topRankedInstances[i].numAttributes() - 1) == 1) { numCorrect += 1.0; } if (printer != null) { printer.print(topRankedInstances[i].stringValue(m_KEAFilter.getUnstemmedPhraseIndex())); if (m_AdditionalInfo) { printer.print("\t"); printer.print( topRankedInstances[i].stringValue(m_KEAFilter.getStemmedPhraseIndex())); printer.print("\t"); printer.print(Utils.doubleToString( topRankedInstances[i].value(m_KEAFilter.getProbabilityIndex()), 4)); } printer.println(); } if (m_debug) { log.debug("" + topRankedInstances[i]); } } } } if (numExtracted > 0) { if (m_debug) { log.debug("-- " + numCorrect + " correct"); } stats.addElement(new Double(numCorrect)); } if (printer != null) { printer.flush(); printer.close(); out.close(); } } double[] st = new double[stats.size()]; for (int i = 0; i < stats.size(); i++) { st[i] = ((Double) stats.elementAt(i)).doubleValue(); } double avg = Utils.mean(st); double stdDev = Math.sqrt(Utils.variance(st)); log.info("Avg. number of matching keyphrases compared to existing ones : " + Utils.doubleToString(avg, 2) + " +/- " + Utils.doubleToString(stdDev, 2)); log.info("Based on " + stats.size() + " documents"); // m_KEAFilter.batchFinished(); }
From source file:com.sliit.views.DataVisualizerPanel.java
void getRocCurve() { try {/*www .java2 s . c o m*/ Instances data; data = new Instances(new BufferedReader(new FileReader(datasetPathText.getText()))); data.setClassIndex(data.numAttributes() - 1); // train classifier Classifier cl = new NaiveBayes(); Evaluation eval = new Evaluation(data); eval.crossValidateModel(cl, data, 10, new Random(1)); // generate curve ThresholdCurve tc = new ThresholdCurve(); int classIndex = 0; Instances result = tc.getCurve(eval.predictions(), classIndex); // plot curve ThresholdVisualizePanel vmc = new ThresholdVisualizePanel(); vmc.setROCString("(Area under ROC = " + Utils.doubleToString(tc.getROCArea(result), 4) + ")"); vmc.setName(result.relationName()); PlotData2D tempd = new PlotData2D(result); tempd.setPlotName(result.relationName()); tempd.addInstanceNumberAttribute(); // specify which points are connected boolean[] cp = new boolean[result.numInstances()]; for (int n = 1; n < cp.length; n++) { cp[n] = true; } tempd.setConnectPoints(cp); // add plot vmc.addPlot(tempd); // display curve String plotName = vmc.getName(); final javax.swing.JFrame jf = new javax.swing.JFrame("Weka Classifier Visualize: " + plotName); jf.setSize(500, 400); jf.getContentPane().setLayout(new BorderLayout()); jf.getContentPane().add(vmc, BorderLayout.CENTER); jf.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { jf.dispose(); } }); jf.setVisible(true); } catch (IOException ex) { Logger.getLogger(DataVisualizerPanel.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(DataVisualizerPanel.class.getName()).log(Level.SEVERE, null, ex); } }