📄 normalizabledistance.java
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * NormalizableDistance.java * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand * */package weka.core;import weka.core.neighboursearch.PerformanceStats;import java.io.Serializable;import java.util.Enumeration;import java.util.Vector;/** * Represents the abstract ancestor for normalizable distance functions, like * Euclidean or Manhattan distance. * * @author Fracpete (fracpete at waikato dot ac dot nz) * @author Gabi Schmidberger (gabi@cs.waikato.ac.nz) -- original code from weka.core.EuclideanDistance * @author Ashraf M. Kibriya (amk14@cs.waikato.ac.nz) -- original code from weka.core.EuclideanDistance * @version $Revision: 1.1 $ */public abstract class NormalizableDistance implements DistanceFunction, OptionHandler, Serializable { /** Index in ranges for MIN. */ public static final int R_MIN = 0; /** Index in ranges for MAX. */ public static final int R_MAX = 1; /** Index in ranges for WIDTH. */ public static final int R_WIDTH = 2; /** the instances used internally. */ protected Instances m_Data = null; /** True if normalization is turned off (default false).*/ protected boolean m_DontNormalize = false; /** The range of the attributes. */ protected double[][] m_Ranges; /** The range of attributes to use for calculating the distance. */ protected Range m_AttributeIndices = new Range("first-last"); /** The boolean flags, whether an attribute will be used or not. */ protected boolean[] m_ActiveIndices; /** Whether all the necessary preparations have been done. */ protected boolean m_Validated; /** * Invalidates the distance function, Instances must be still set. */ public NormalizableDistance() { invalidate(); } /** * Initializes the distance function and automatically initializes the * ranges. * * @param data the instances the distance function should work on */ public NormalizableDistance(Instances data) { setInstances(data); } /** * Returns a string describing this object. * * @return a description of the evaluator suitable for * displaying in the explorer/experimenter gui */ public abstract String globalInfo(); /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector result = new Vector(); result.add(new Option( "\tTurns off the normalization of attribute \n" + "\tvalues in distance calculation.", "D", 0, "-D")); result.addElement(new Option( "\tSpecifies list of columns to used in the calculation of the \n" + "\tdistance. 'first' and 'last' are valid indices.\n" + "\t(default: first-last)", "R", 1, "-R <col1,col2-col4,...>")); result.addElement(new Option( "\tInvert matching sense of column indices.", "V", 0, "-V")); return result.elements(); } /** * Gets the current settings. Returns empty array. * * @return an array of strings suitable for passing to setOptions() */ public String[] getOptions() { Vector<String> result; result = new Vector<String>(); if (getDontNormalize()) result.add("-D"); result.add("-R"); result.add(getAttributeIndices()); if (getInvertSelection()) result.add("-V"); return result.toArray(new String[result.size()]); } /** * Parses a given list of options. * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { String tmpStr; setDontNormalize(Utils.getFlag('D', options)); tmpStr = Utils.getOption('R', options); if (tmpStr.length() != 0) setAttributeIndices(tmpStr); else setAttributeIndices("first-last"); setInvertSelection(Utils.getFlag('V', options)); } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String dontNormalizeTipText() { return "Whether if the normalization of attributes should be turned off " + "for distance calculation (Default: false i.e. attribute values " + "are normalized). "; } /** * Sets whether if the attribute values are to be normalized in distance * calculation. * * @param dontNormalize if true the values are not normalized */ public void setDontNormalize(boolean dontNormalize) { m_DontNormalize = dontNormalize; invalidate(); } /** * Gets whether if the attribute values are to be normazlied in distance * calculation. (default false i.e. attribute values are normalized.) * * @return false if values get normalized */ public boolean getDontNormalize() { return m_DontNormalize; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String attributeIndicesTipText() { return "Specify range of attributes to act on. " + "This is a comma separated list of attribute indices, with " + "\"first\" and \"last\" valid values. Specify an inclusive " + "range with \"-\". E.g: \"first-3,5,6-10,last\"."; } /** * Sets the range of attributes to use in the calculation of the distance. * The indices start from 1, 'first' and 'last' are valid as well. * E.g.: first-3,5,6-last * * @param value the new attribute index range */ public void setAttributeIndices(String value) { m_AttributeIndices.setRanges(value); invalidate(); } /** * Gets the range of attributes used in the calculation of the distance. * * @return the attribute index range */ public String getAttributeIndices() { return m_AttributeIndices.getRanges(); } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String invertSelectionTipText() { return "Set attribute selection mode. If false, only selected " + "attributes in the range will be used in the distance calculation; if " + "true, only non-selected attributes will be used for the calculation."; } /** * Sets whether the matching sense of attribute indices is inverted or not. * * @param value if true the matching sense is inverted */ public void setInvertSelection(boolean value) { m_AttributeIndices.setInvert(value); invalidate(); } /** * Gets whether the matching sense of attribute indices is inverted or not. * * @return true if the matching sense is inverted */ public boolean getInvertSelection() { return m_AttributeIndices.getInvert(); } /** * invalidates all initializations. */ protected void invalidate() { m_Validated = false; } /** * performs the initializations if necessary. */ protected void validate() { if (!m_Validated) { initialize(); m_Validated = true; } } /** * initializes the ranges and the attributes being used. */ protected void initialize() { initializeAttributeIndices(); initializeRanges(); } /** * initializes the attribute indices. */ protected void initializeAttributeIndices() { m_AttributeIndices.setUpper(m_Data.numAttributes() - 1); m_ActiveIndices = new boolean[m_Data.numAttributes()]; for (int i = 0; i < m_ActiveIndices.length; i++) m_ActiveIndices[i] = m_AttributeIndices.isInRange(i); } /** * Sets the instances. * * @param insts the instances to use */ public void setInstances(Instances insts) { m_Data = insts; invalidate(); } /** * returns the instances currently set. * * @return the current instances */ public Instances getInstances() { return m_Data; } /** * Does nothing, derived classes may override it though. * * @param distances the distances to post-process */ public void postProcessDistances(double[] distances) { } /** * Update the distance function (if necessary) for the newly added instance. * * @param ins the instance to add */ public void update(Instance ins) { validate(); m_Ranges = updateRanges(ins, m_Ranges); } /** * Calculates the distance between two instances. * * @param first the first instance * @param second the second instance * @return the distance between the two given instances */ public double distance(Instance first, Instance second) { return distance(first, second, null); } /** * Calculates the distance between two instances. * * @param first the first instance * @param second the second instance * @param stats the performance stats object * @return the distance between the two given instances */ public double distance(Instance first, Instance second, PerformanceStats stats) { return distance(first, second, Double.POSITIVE_INFINITY, stats); } /** * Calculates the distance between two instances. Offers speed up (if the * distance function class in use supports it) in nearest neighbour search by * taking into account the cutOff or maximum distance. Depending on the * distance function class, post processing of the distances by * postProcessDistances(double []) may be required if this function is used. * * @param first the first instance * @param second the second instance * @param cutOffValue If the distance being calculated becomes larger than * cutOffValue then the rest of the calculation is * discarded. * @return the distance between the two given instances or * Double.POSITIVE_INFINITY if the distance being * calculated becomes larger than cutOffValue. */ public double distance(Instance first, Instance second, double cutOffValue) { return distance(first, second, cutOffValue, null); } /** * Calculates the distance between two instances. Offers speed up (if the * distance function class in use supports it) in nearest neighbour search by * taking into account the cutOff or maximum distance. Depending on the * distance function class, post processing of the distances by * postProcessDistances(double []) may be required if this function is used. * * @param first the first instance * @param second the second instance * @param cutOffValue If the distance being calculated becomes larger than * cutOffValue then the rest of the calculation is * discarded. * @param stats the performance stats object * @return the distance between the two given instances or * Double.POSITIVE_INFINITY if the distance being * calculated becomes larger than cutOffValue. */ public double distance(Instance first, Instance second, double cutOffValue, PerformanceStats stats) { double distance = 0; int firstI, secondI; int firstNumValues = first.numValues(); int secondNumValues = second.numValues(); int numAttributes = m_Data.numAttributes(); int classIndex = m_Data.classIndex(); validate(); for (int p1 = 0, p2 = 0; p1 < firstNumValues || p2 < secondNumValues; ) { if (p1 >= firstNumValues) firstI = numAttributes;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -