📄 classifierinstancemetric.java
字号:
Instance diffInstance = new Instance(1.0, distances); diffInstance.setDataset(m_diffInstances); return m_classifier.distributionForInstance(diffInstance)[1]; } /** * Returns similarity between two records * @param instance1 First instance. * @param instance2 Second instance. * @exception Exception if similarity could not be calculated. */ public double similarity(Instance instance1, Instance instance2) throws Exception { double d = distance(instance1, instance2); return Math.exp(-d); } /** The computation can be either based on distance, or on similarity * @returns true if the underlying metric computes distance, false if similarity */ public boolean isDistanceBased() { return true; }; /** * Set the classifier * * @param classifier the classifier */ public void setClassifier (DistributionClassifier classifier) { m_classifier = classifier; } /** * Get the classifier * * @returns the classifierthat this metric employs */ public DistributionClassifier getClassifier () { return m_classifier; } /** * Set the baseline metric * * @param metrics string metrics that will used on each string attribute */ public void setStringMetrics (StringMetric[] metrics) { m_stringMetrics = metrics; } /** * Get the baseline string metrics * * @return the string metrics that are used for each field */ public StringMetric[] getStringMetrics () { return m_stringMetrics; } /** Set the pairwise selector for this metric * @param selector a new pairwise selector */ public void setSelector(PairwiseSelector selector) { m_selector = selector; } /** Get the pairwise selector for this metric * @return the pairwise selector */ public PairwiseSelector getSelector() { return m_selector; } /** Set the number of same-class training pairs that is desired * @param numPosPairs the number of same-class training pairs to be * created for training the classifier */ public void setNumPosPairs(int numPosPairs) { m_numPosPairs = numPosPairs; } /** Get the number of same-class training pairs * @return the number of same-class training pairs to create for * training the classifier */ public int getNumPosPairs() { return m_numPosPairs; } /** Set the number of different-class training pairs * @param numNegPairs the number of different-class training pairs * to create for training the classifier */ public void setNumNegPairs(int numNegPairs) { m_numNegPairs = numNegPairs; } /** Get the number of different-class training pairs * @return the number of different-class training pairs to create * for training the classifier */ public int getNumNegPairs() { return m_numNegPairs; } /** * Gets a string containing current date and time. * * @return a string containing the date and time. */ protected static String getTimestamp() { return (new SimpleDateFormat("HH:mm:ss:")).format(new Date()); } /** A little helper to create a single String from an array of Strings * @param strings an array of strings * @returns a single concatenated string */ public static String concatStringArray(String[] strings) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < strings.length; i++) { buffer.append(strings[i]); buffer.append(" "); } return buffer.toString(); } /** * Returns an enumeration describing the available options * * @return an enumeration of all the available options **/ public Enumeration listOptions() { Vector newVector = new Vector(2); newVector.addElement(new Option("\tMetric.\n" +"\t(default=AffineMetric)", "M", 1,"-M metric_name metric_options")); newVector.addElement(new Option("\tClassifier.\n" +"\t(default=weka.classifiers.functions.SMO)", "C", 1,"-C clasifierName classifierOptions")); return newVector.elements(); } /** * Parses a given list of options. * * Valid options are:<p> * * -M metric options <p> * StringMetric used <p> * * -C classifier options <p> * Classifier used <p> * * @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 { String optionString; // TODO: implement command-line options// String metricString = Utils.getOption('M', options);// if (metricString.length() != 0) {// String[] metricSpec = Utils.splitOptions(metricString);// String metricName = metricSpec[0]; // metricSpec[0] = "";// System.out.println("Metric name: " + metricName + "\nMetric parameters: " + concatStringArray(metricSpec));// setMetric(StringMetric.forName(metricName, metricSpec));// } String classifierString = Utils.getOption('C', options); if (classifierString.length() == 0) { throw new Exception("A classifier must be specified" + " with the -C option."); } String [] classifierSpec = Utils.splitOptions(classifierString); if (classifierSpec.length == 0) { throw new Exception("Invalid classifier specification string"); } String classifierName = classifierSpec[0]; classifierSpec[0] = ""; System.out.println("Classifier name: " + classifierName + "\nClassifier parameters: " + concatStringArray(classifierSpec)); setClassifier((DistributionClassifier) DistributionClassifier.forName(classifierName, classifierSpec)); } /** * Gets the current settings of Greedy Agglomerative Clustering * * @return an array of strings suitable for passing to setOptions() */ public String [] getOptions() { String [] options = new String [200]; int current = 0; if (m_selector instanceof OptionHandler) { String[] selectorOptions = ((OptionHandler)m_selector).getOptions(); for (int i = 0; i < selectorOptions.length; i++) { options[current++] = selectorOptions[i]; } } options[current++] = "-p"; options[current++] = "" + m_numPosPairs; options[current++] = "-n"; options[current++] = "" + m_numNegPairs; options[current++] = "-M" + m_stringMetrics.length; for (int i = 0; i < m_stringMetrics.length; i++) { options[current++] = Utils.removeSubstring(m_stringMetrics[i].getClass().getName(), "weka.deduping.metrics."); if (m_stringMetrics[i] instanceof OptionHandler) { String[] metricOptions = ((OptionHandler)m_stringMetrics[i]).getOptions(); for (int j = 0; j < metricOptions.length; j++) { options[current++] = metricOptions[j]; } } } options[current++] = "-C"; options[current++] = Utils.removeSubstring(m_classifier.getClass().getName(), "weka.classifiers."); if (m_classifier instanceof OptionHandler) { String[] classifierOptions = ((OptionHandler)m_classifier).getOptions(); for (int i = 0; i < classifierOptions.length; i++) { options[current++] = classifierOptions[i]; } } while (current < options.length) { options[current++] = ""; } return options; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -