📄 euclideandistance.java
字号:
bestPoint = i; } } return pointList[bestPoint]; } /** * Returns true if the value of the given dimension is smaller or equal the * value to be compared with. * @param instance the instance where the value should be taken of * @param dim the dimension of the value * @param the value to compare with * @return true is value of instance is smaller or equal value */ public boolean valueIsSmallerEqual(Instance instance, int dim, double value) { //This stays return instance.value(dim) <= value; //Utils.smOrEq(instance.value(dim), value); } /** * Documents the content of an EuclideanDistance object in a string. * @return the converted string */ public String toString() { StringBuffer text = new StringBuffer(); //todo text.append("\n"); return text.toString(); } /** * Used for debug println's. * @param output string that is printed */ private void OOPS(String output) { System.out.println(output); } /** * Computes and sets the number of attributes used. */ private void setNumAttributesUsed() { m_NumAttributesUsed = 0.0; for (int i = 0; i < m_Data.numAttributes(); i++) { if ((i != m_Data.classIndex()) && (m_Data.attribute(i).isNominal() || m_Data.attribute(i).isNumeric())) { m_NumAttributesUsed += 1.0; } } } /*============================Ranges related functions=====================*/ /** The range of the attributes */ //being used in KDTree and EuclideanDistance protected double [][] m_Ranges; /** * Index in ranges for MIN and MAX and WIDTH */ public static final int R_MIN = 0; public static final int R_MAX = 1; public static final int R_WIDTH = 2; /** * Initializes the ranges using all instances of the dataset. * Sets m_Ranges. * @return the ranges */ //Being used in other classes (KDTree). public double [][] initializeRanges() throws Exception { if(m_Data == null) { throw new Exception("No instances supplied."); } int numAtt = m_Data.numAttributes(); double [][] ranges = new double [numAtt][3]; if (m_Data.numInstances() <= 0) { initializeRangesEmpty(numAtt, ranges); m_Ranges = ranges; return ranges; } else // initialize ranges using the first instance updateRangesFirst(m_Data.instance(0), numAtt, ranges); // update ranges, starting from the second for (int i = 1; i < m_Data.numInstances(); i++) { updateRanges(m_Data.instance(i), numAtt, ranges); } m_Ranges = ranges;// System.out.println("Initialized ranges");// printRanges(m_Ranges); return ranges; } /** * Initializes the ranges of a subset of the instances of this dataset. * Therefore m_Ranges is not set. * @param instList list of indexes of the subset * @return the ranges */ //being used in other classes (KDTree and XMeans) public double [][] initializeRanges(int[] instList) throws Exception { if(m_Data == null) { throw new Exception("No instances supplied."); } int numAtt = m_Data.numAttributes(); double [][] ranges = new double [numAtt][3]; if (m_Data.numInstances() <= 0) { initializeRangesEmpty(numAtt, ranges); return ranges; } else { // initialize ranges using the first instance updateRangesFirst(m_Data.instance(instList[0]), numAtt, ranges); // update ranges, starting from the second for (int i = 1; i < instList.length; i++) { updateRanges(m_Data.instance(instList[i]), numAtt, ranges); } } return ranges; } /** * Initializes the ranges of a subset of the instances of this dataset. * Therefore m_Ranges is not set. * The caller of this method should ensure that the supplied start and end * indices are valid (start <= end, end<instList.length etc) and * correct. * * @param instList list of indexes of the instances * @param startIdx start index of the subset of instances in the indices array * @param endIdx end index of the subset of instances in the indices array * @return the ranges */ //being used in other classes (KDTree and XMeans) public double [][] initializeRanges(int[] instList, int startIdx, int endIdx) throws Exception { if(m_Data == null) { throw new Exception("No instances supplied."); } int numAtt = m_Data.numAttributes(); double [][] ranges = new double [numAtt][3]; if (m_Data.numInstances() <= 0) { initializeRangesEmpty(numAtt, ranges); return ranges; } else { // initialize ranges using the first instance updateRangesFirst(m_Data.instance(instList[startIdx]), numAtt, ranges); // update ranges, starting from the second for (int i = startIdx+1; i <= endIdx; i++) { updateRanges(m_Data.instance(instList[i]), numAtt, ranges); } } return ranges; } /** * Used to initialize the ranges. * @param numAtt number of attributes in the model * @param ranges low, high and width values for all attributes */ //being used in the functions above public void initializeRangesEmpty(int numAtt, double[][] ranges) { for (int j = 0; j < numAtt; j++) { ranges[j][R_MIN] = Double.MAX_VALUE; ranges[j][R_MAX] = -Double.MAX_VALUE; ranges[j][R_WIDTH] = Double.MAX_VALUE; } } /** * Used to initialize the ranges. For this the values of the first * instance is used to save time. * Sets low and high to the values of the first instance and * width to zero. * @param instance the new instance * @param numAtt number of attributes in the model * @param ranges low, high and width values for all attributes */ //being used in the functions above public void updateRangesFirst(Instance instance, int numAtt, double[][] ranges) { //try { // throw new Exception(); //} catch(Exception ex) { ex.printStackTrace(); } //System.out.println("In ranges the first supplied instance is:\n"+ // instance.toString()); for (int j = 0; j < numAtt; j++) { if (!instance.isMissing(j)) { ranges[j][R_MIN] = instance.value(j); ranges[j][R_MAX] = instance.value(j); ranges[j][R_WIDTH] = 0.0; } else { // if value was missing ranges[j][R_MIN] = Double.MAX_VALUE; ranges[j][R_MAX] = -Double.MAX_VALUE; ranges[j][R_WIDTH] = Double.MAX_VALUE; } } } /** * Updates the minimum and maximum and width values for all the attributes * based on a new instance. * @param instance the new instance * @param numAtt number of attributes in the model * @param ranges low, high and width values for all attributes */ //Being used in the functions above private void updateRanges(Instance instance, int numAtt, double [][] ranges) { // updateRangesFirst must have been called on ranges for (int j = 0; j < numAtt; j++) { double value = instance.value(j); if (!instance.isMissing(j)) { if (value < ranges[j][R_MIN]) { ranges[j][R_MIN] = value; ranges[j][R_WIDTH] = ranges[j][R_MAX] - ranges[j][R_MIN]; if(value > ranges[j][R_MAX]) { //if this is the first value that is ranges[j][R_MAX] = value; //not missing. The,0 ranges[j][R_WIDTH] = ranges[j][R_MAX] - ranges[j][R_MIN]; } } else { if (value > ranges[j][R_MAX]) { ranges[j][R_MAX] = value; ranges[j][R_WIDTH] = ranges[j][R_MAX] - ranges[j][R_MIN]; } } } } } /** * Updates the ranges given a new instance. * @param instance the new instance * @param ranges low, high and width values for all attributes */ //being used in other classes (KDTree) public double [][] updateRanges(Instance instance, double [][] ranges) { // updateRangesFirst must have been called on ranges for (int j = 0; j < ranges.length; j++) { double value = instance.value(j); if (!instance.isMissing(j)) { if (value < ranges[j][R_MIN]) { ranges[j][R_MIN] = value; ranges[j][R_WIDTH] = ranges[j][R_MAX] - ranges[j][R_MIN]; } else { if (instance.value(j) > ranges[j][R_MAX]) { ranges[j][R_MAX] = value; ranges[j][R_WIDTH] = ranges[j][R_MAX] - ranges[j][R_MIN]; } } } } return ranges; } /** * Update the ranges if a new instance comes. * @param instance the new instance */ //Being used in KDTree public void updateRanges(Instance instance) { m_Ranges = updateRanges(instance, m_Ranges); // System.out.println("Updated ranges");// printRanges(m_Ranges); } /** * prints the ranges. * @param instance the new instance * @param numAtt number of attributes in the model * @param ranges low, high and width values for all attributes */ //Not being used in any other class. Not even being used in this class. public void printRanges(double [][] ranges) { OOPS("printRanges"); // updateRangesFirst must have been called on ranges for (int j = 0; j < ranges.length; j++) { OOPS(" "+j+"-MIN "+ranges[j][R_MIN]); OOPS(" "+j+"-MAX "+ranges[j][R_MAX]); OOPS(" "+j+"-WIDTH "+ranges[j][R_WIDTH]); } } /** * Test if an instance is within the given ranges. * @param instance the instance * @param ranges the ranges the instance is tested to be in * @return true if instance is within the ranges */ //being used in IBk but better to remove from there. public boolean inRanges(Instance instance, double [][] ranges) { boolean isIn = true; // updateRangesFirst must have been called on ranges for (int j = 0; isIn && (j < ranges.length); j++) { if (!instance.isMissing(j)) { double value = instance.value(j); isIn = value <= ranges[j][R_MAX]; if (isIn) isIn = value >= ranges[j][R_MIN]; } } return isIn; } /** * Prints a range to standard output * @param ranges the ranges to print */ //Not being used in any other class. Not even being used in this class. public void printRanges(Instances model, double[][] ranges) { System.out.println("printRanges"); for (int j = 0; j < model.numAttributes(); j++) { System.out.print("Attribute "+ j +" MIN: " + ranges[j][R_MIN]); System.out.print(" MAX: " + ranges[j][R_MAX]); System.out.print(" WIDTH: " + ranges[j][R_WIDTH]); System.out.println(" "); } } /** * Check if ranges are set. * @return true if ranges are set */ //Not being used in any other class public boolean rangesSet() { return (m_Ranges != null); } /** * Method to get the ranges. * @return the ranges */ //Not being used in any other class public double[][] getRanges() throws Exception { if (m_Ranges == null) throw new Exception("Ranges not yet set."); return m_Ranges; } /** * Main method for testing this class. */ public static void main(String[] args) { try { Reader r = null; if (args.length > 1) { throw (new Exception("Usage: EuclideanDistance <filename>")); } else if (args.length == 0) { r = new BufferedReader(new InputStreamReader(System.in)); } else { r = new BufferedReader(new FileReader(args[0])); } Instances i = new Instances(r); EuclideanDistance test = new EuclideanDistance(i); System.out.println("test:\n " + test); } catch (Exception e) { e.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -