📄 barhillelmetric.java
字号:
} while (!s.startsWith("# columns")); // System.out.println("Line: " + s); while ((s = r.readLine()) != null) { StringTokenizer tokenizer = new StringTokenizer(s); while (tokenizer.hasMoreTokens()) { String value = tokenizer.nextToken(); try { vectors[i][j] = Double.parseDouble(value); } catch (Exception e) { System.err.println("Couldn't parse " + value + " as double"); } j++; if (j > m_numAttribs) { System.err.println("Too many columns (" + j + " instead of " + m_numAttribs + ") in line: " + s); } } if (j != m_numAttribs) { System.err.println("Too few columns (" + j + " instead of " + m_numAttribs + ") in line: " + s); } j = 0; i++; if (i > m_numAttribs) { System.err.println("Too many rows: " + i + ", expecting " + m_numAttribs + " attributes"); } } if (i != m_numAttribs) { System.err.println("Too few rows: " + i + " expecting " + m_numAttribs + " attributes"); } return vectors; } /** * Dump data matrix into a file */ private void dumpInstances(String tempFile) { try { PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream(tempFile))); for (int k = 0; k < m_numInstances; k++) { Instance instance = m_trainInstances.instance(k); for (int j = 0; j < m_numAttribs; j++) { writer.print(instance.value(j) + " "); } writer.println(); } writer.close(); } catch (Exception e) { System.err.println("Could not create a temporary file for dumping the data matrix: " + e); } } /** * Dump chunklet vector into a file */ private void dumpChunklets (String tempFile) { try { PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream(tempFile))); for (int k = 0; k < m_chunkletAssignments.length; k++) { writer.print(m_chunkletAssignments[k] + " "); } writer.println(); writer.close(); } catch (Exception e) { System.err.println("Could not create a temporary file for dumping the data matrix: " + e); } } /** Create octave m-file for ICA * @param filename file where octave script is created */ public void prepareOctave(String filename) { try{ PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream(filename))); writer.println("fprintf(stderr,\"Before loading data file\\n\");"); writer.println("fflush(stderr);"); writer.println("load " + m_dataFilename + ";"); writer.println("fprintf(stderr,\"Before loading chunklet file\\n\");"); writer.println("fflush(stderr);"); writer.println("load " + m_chunkletAssignmentFilename + ";"); writer.println("fprintf(stderr,\"Before running RCA\\n\");"); writer.println("fflush(stderr);"); writer.println("[A] = RCA(" + m_dataFilenameToken + "," + m_chunkletAssignmentFilenameToken + ");"); writer.println("fprintf(stderr,\"After running RCA\\n\");"); writer.println("fflush(stderr);"); writer.println("[AnumRows, AnumCols] = size(A);"); writer.println("save " + m_rcaAttributeMatrixFilename + " AnumRows AnumCols A"); writer.close(); } catch (Exception e) { System.err.println("Could not create octave file: " + e); } } /** Run octave in command line with a given argument * @param inFile file to be input to Octave * @param outFile file where results are stored */ public static void runOctave(String inFile, String outFile) { // call octave to do the dirty work try { int exitValue; String cmd = "octave " + inFile + " > " + outFile; //String cmd = "octave " + inFile; System.out.println("Starting to run octave: " + cmd); Process proc = Runtime.getRuntime().exec(cmd); System.out.println("Cmd set up:" + cmd); System.out.println("Now waiting for process ..."); // read the error if (proc != null){ // proc.getErrorStream().flush(); BufferedReader procError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); try { String line; while ((line = procError.readLine()) != null){ System.out.println("ERROR: " + line); } } catch (Exception e) { System.err.println("Problems trapping output in debug mode:"); e.printStackTrace(); System.out.println(e); } } // read the output if (proc != null){ BufferedReader procOutput = new BufferedReader(new InputStreamReader(proc.getInputStream())); try { String line; while ((line = procOutput.readLine()) != null){ System.out.println("OUTPUT: " + line); } } catch (Exception e) { System.err.println("Problems trapping output in debug mode:"); e.printStackTrace(); System.out.println(e); } } exitValue = proc.waitFor(); System.out.println("Done waiting for process!"); System.out.println("End of running octave, exitValue = " + exitValue); } catch (Exception e) { System.err.println("Problems running octave: " + e); } } /** Get a timestamp string as a weak uniqueid * @returns a timestamp string in the form "mmddhhmmssS" */ public static String getLogTimestamp() { Calendar cal = Calendar.getInstance(TimeZone.getDefault()); String DATE_FORMAT = "MMddHHmmssS"; java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT); sdf.setTimeZone(TimeZone.getDefault()); return (sdf.format(cal.getTime())); } /** * Given a cluster of instances, return the centroid of that cluster * @param instances objects belonging to a cluster * @param fastMode whether fast mode should be used for SparseInstances * @param normalized normalize centroids for SPKMeans * @return a centroid instance for the given cluster */ public Instance getCentroidInstance(Instances instances, boolean fastMode, boolean normalized) { System.out.println("\n\nWARNING!! Not implemented!!\n\n"); return null; } /** Get the values of the partial derivates for the metric components * for a particular instance pair @param instance1 the first instance @param instance2 the first instance */ public double[] getGradients(Instance instance1, Instance instance2) throws Exception { System.out.println("\n\nWARNING!! Not implemented!!\n\n"); return null; } /** * Create an instance with features corresponding to components of the two given instances * @param instance1 first instance * @param instance2 second instance */ public Instance createDiffInstance (Instance instance1, Instance instance2) { System.out.println("\n\nWARNING!! Not implemented!!\n\n"); return null; } /** * Train the distance metric. A specific metric will take care of * its own training either via a metric learner or by itself. */ public void learnMetric(Instances data) throws Exception { System.out.println("\n\nWARNING!! Not implemented!!\n\n"); } /** * Returns a distance value between two instances. * @param instance1 First instance. * @param instance2 Second instance. * @exception Exception if distance could not be estimated. */ public double distanceNonWeighted(Instance instance1, Instance instance2) throws Exception { System.out.println("\n\nWARNING!! Not implemented!!\n\n"); return -1; } /** * Returns a similarity estimate between two instances without using the weights. * @param instance1 First instance. * @param instance2 Second instance. * @exception Exception if similarity could not be estimated. */ public double similarityNonWeighted(Instance instance1, Instance instance2) throws Exception { switch (m_conversionType) { case CONVERSION_LAPLACIAN: return 1 / (1 + distanceNonWeighted(instance1, instance2)); case CONVERSION_UNIT: return 2 * (1 - distanceNonWeighted(instance1, instance2)); case CONVERSION_EXPONENTIAL: return Math.exp(-distanceNonWeighted(instance1, instance2)); default: throw new Exception ("Unknown distance to similarity conversion method"); } } /** * Gets the current settings of WeightedEuclideanP. * * @return an array of strings suitable for passing to setOptions() */ public String [] getOptions() { String [] options = new String [45]; int current = 0; if (m_conversionType == CONVERSION_EXPONENTIAL) { options[current++] = "-E"; } else if (m_conversionType == CONVERSION_UNIT) { options[current++] = "-U"; } while (current < options.length) { options[current++] = ""; } return options; } /** * Parses a given list of options. Valid options are:<p> * * -N <br> * Normalize the euclidean distance by vectors lengths * * -E <br> * Use exponential conversion from distance to similarity * (default laplacian conversion) <p> * * -U <br> * Use unit conversion from similarity to distance (dist=1-sim) * (default laplacian conversion) <p> * * -R <br> * The metric is trainable and will be trained using the current MetricLearner * (default non-trainable) * * @param options the list of options as an array of strings * @exception Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { if (Utils.getFlag('E', options)) { setConversionType(new SelectedTag(CONVERSION_EXPONENTIAL, TAGS_CONVERSION)); } else if (Utils.getFlag('U', options)) { setConversionType(new SelectedTag(CONVERSION_UNIT, TAGS_CONVERSION)); } else { setConversionType(new SelectedTag(CONVERSION_LAPLACIAN, TAGS_CONVERSION)); } if (Utils.getFlag('R', options)) { setTrainable(Utils.getFlag('R', options)); setExternal(Utils.getFlag('X', options)); } Utils.checkForRemainingOptions(options); } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector newVector = new Vector(4); newVector.addElement(new Option("\tNormalize the euclidean distance by vectors lengths\n", "N", 0, "-N")); newVector.addElement(new Option("\tUse exponential conversion from similarity to distance\n", "E", 0, "-E")); newVector.addElement(new Option("\tUse unit conversion from similarity to distance\n", "U", 0, "-U")); newVector.addElement(new Option("\tTrain the metric\n", "R", 0, "-R")); newVector.addElement(new Option("\tUse the metric learner for similarity calculations(\"external\")", "X", 0, "-X")); newVector.addElement(new Option( "\tFull class name of metric learner to use, followed\n" + "\tby scheme options. (required)\n" + "\teg: \"weka.core.metrics.ClassifierMetricLearner -B weka.classifiers.function.SMO\"", "L", 1, "-L <classifier specification>")); return newVector.elements(); } /** * Main method for testing this class * @param argv should contain the command line arguments to the * evaluator/transformer (see AttributeSelection) */ public static void main(String [] argv) { } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -