/** * Parses a given list of options. Valid options are: * * <!-- options-start --> <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ @Override public void setOptions(String[] options) throws Exception { String nnSearchClass = Utils.getOption('A', options); if (nnSearchClass.length() != 0) { String nnSearchClassSpec[] = Utils.splitOptions(nnSearchClass); if (nnSearchClassSpec.length == 0) { throw new Exception("Invalid DistanceFunction specification string."); } String className = nnSearchClassSpec[0]; nnSearchClassSpec[0] = ""; setDistanceFunction((DistanceFunction) Utils.forName( DistanceFunction.class, className, nnSearchClassSpec)); } else { setDistanceFunction(new EuclideanDistance()); } setMeasurePerformance(Utils.getFlag('P', options)); }
/** * Parses a given list of options. Valid options are: * <!-- options-start --> <!-- options-end --> * * @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 nnSearchClass = Utils.getOption('A', options); if(nnSearchClass.length() != 0) { String nnSearchClassSpec[] = Utils.splitOptions(nnSearchClass); if(nnSearchClassSpec.length == 0) { throw new Exception("Invalid DistanceFunction specification string."); } String className = nnSearchClassSpec[0]; nnSearchClassSpec[0] = ""; setDistanceFunction( (DistanceFunction) Utils.forName( DistanceFunction.class, className, nnSearchClassSpec) ); } else { setDistanceFunction(new EuclideanDistance()); } setMeasurePerformance(Utils.getFlag('P',options)); }
private IBk useCosine() { IBk ibk = new IBk(); Instances data = ClassificationModel.getInstance().getInstances(); Normalize normalizer = new Normalize(); try { normalizer.setInputFormat(data); // Euclidean Distance working over normalized instances = Cosine Similarity according to Foundations of Statistical Natural Processing Language p.301 // As long as attribute normalization is disabled. Instances normalizedInstances; normalizedInstances = Filter.useFilter(data, normalizer); ClassificationModel.getInstance().setInstances(normalizedInstances); DistanceFunction df = new EuclideanDistance(); ((EuclideanDistance) df).setDontNormalize(true); ibk.getNearestNeighbourSearchAlgorithm().setDistanceFunction(df); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return ibk; }
/** * sets the distance function to use for instance comparison. * * @param df the new distance function to use * @throws Exception if instances cannot be processed */ public void setDistanceFunction(DistanceFunction df) throws Exception { if (!(df instanceof EuclideanDistance) && !(df instanceof ManhattanDistance)) { throw new Exception( "SimpleKMeans currently only supports the Euclidean and Manhattan distances."); } m_DistanceFunction = df; }
/** * sets the distance function to use for instance comparison. * * @param df the new distance function to use * @throws Exception if instances cannot be processed */ public void setDistanceFunction(DistanceFunction df) throws Exception { if (!(df instanceof EuclideanDistance) && !(df instanceof ManhattanDistance)) { throw new Exception( "KMeansPlusPlus only supports the Euclidean and Manhattan distances."); } m_DistanceFunction = df; }
/** * Gets the distance specification string, which contains the class name of * the distance and any options to the distance * * @return the distance string. */ protected String getDistanceSpec() { DistanceFunction c = getDistance(); if (c instanceof OptionHandler) { return c.getClass().getName() + " " + Utils.joinOptions(((OptionHandler)c).getOptions()); } return c.getClass().getName(); }
/** * Sets the distance function to use for nearest neighbour search. Currently * only EuclideanDistance is supported. * * @param df the distance function to use * @throws Exception if not EuclideanDistance */ @Override public void setDistanceFunction(DistanceFunction df) throws Exception { if (!(df instanceof EuclideanDistance)) { throw new Exception("CoverTree currently only works with " + "EuclideanDistanceFunction."); } m_DistanceFunction = m_EuclideanDistance = (EuclideanDistance) df; }
/** * sets the distance function to use for nearest neighbour search. * * @param df the distance function to use * @throws Exception if not EuclideanDistance */ public void setDistanceFunction(DistanceFunction df) throws Exception { if (!(df instanceof EuclideanDistance)) throw new Exception("KDTree currently only works with " + "EuclideanDistanceFunction."); m_DistanceFunction = m_EuclideanDistance = (EuclideanDistance) df; }
/** * sets the distance function to use for instance comparison. * * @param df the new distance function to use * @throws Exception if instances cannot be processed */ public void setDistanceFunction(DistanceFunction df) throws Exception { if (!(df instanceof EuclideanDistance) && !(df instanceof ManhattanDistance)) { throw new Exception("SimpleKMeans currently only supports the Euclidean and Manhattan distances."); } m_DistanceFunction = df; }
/** * Gets the distance function specification string, which contains the * class name of the distance function class and any options to it. * * @return the distance function specification string */ protected String getDistanceFSpec() { DistanceFunction d = getDistanceF(); if (d instanceof OptionHandler) { return d.getClass().getName() + " " + Utils.joinOptions(((OptionHandler) d).getOptions()); } return d.getClass().getName(); }
public DistanceFunction getDistanceFunction() { return m_DistanceFunction; }
public void setDistanceFunction(DistanceFunction distanceFunction) { m_DistanceFunction = distanceFunction; }
/** * Parses a given list of options. * <p/> * * <!-- options-start --> Valid options are: * <p/> * * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ @Override public void setOptions(String[] options) throws Exception { m_bPrintNewick = Utils.getFlag('P', options); String optionString = Utils.getOption('N', options); if (optionString.length() != 0) { Integer temp = new Integer(optionString); setNumClusters(temp); } else { setNumClusters(2); } setDistanceIsBranchLength(Utils.getFlag('B', options)); String sLinkType = Utils.getOption('L', options); if (sLinkType.compareTo("SINGLE") == 0) { setLinkType(new SelectedTag(SINGLE, TAGS_LINK_TYPE)); } if (sLinkType.compareTo("COMPLETE") == 0) { setLinkType(new SelectedTag(COMPLETE, TAGS_LINK_TYPE)); } if (sLinkType.compareTo("AVERAGE") == 0) { setLinkType(new SelectedTag(AVERAGE, TAGS_LINK_TYPE)); } if (sLinkType.compareTo("MEAN") == 0) { setLinkType(new SelectedTag(MEAN, TAGS_LINK_TYPE)); } if (sLinkType.compareTo("CENTROID") == 0) { setLinkType(new SelectedTag(CENTROID, TAGS_LINK_TYPE)); } if (sLinkType.compareTo("WARD") == 0) { setLinkType(new SelectedTag(WARD, TAGS_LINK_TYPE)); } if (sLinkType.compareTo("ADJCOMPLETE") == 0) { setLinkType(new SelectedTag(ADJCOMPLETE, TAGS_LINK_TYPE)); } if (sLinkType.compareTo("NEIGHBOR_JOINING") == 0) { setLinkType(new SelectedTag(NEIGHBOR_JOINING, TAGS_LINK_TYPE)); } String nnSearchClass = Utils.getOption('A', options); if (nnSearchClass.length() != 0) { String nnSearchClassSpec[] = Utils.splitOptions(nnSearchClass); if (nnSearchClassSpec.length == 0) { throw new Exception("Invalid DistanceFunction specification string."); } String className = nnSearchClassSpec[0]; nnSearchClassSpec[0] = ""; setDistanceFunction((DistanceFunction) Utils.forName( DistanceFunction.class, className, nnSearchClassSpec)); } else { setDistanceFunction(new EuclideanDistance()); } super.setOptions(options); Utils.checkForRemainingOptions(options); }
/** * Calculates the radius of a node. * * @param start The start index of the portion in indices array * that belongs to the node. * @param end The end index of the portion in indices array * that belongs to the node. * @param instList The indices array holding indices of * instances. * @param insts The actual instances. instList points to * instances in this object. * @param pivot The centre/pivot of the node. * @param distanceFunction The distance function to use to * calculate the radius. * @return The radius of the node. * @throws Exception If there is some problem calculating the * radius. */ public static double calcRadius(int start, int end, int[] instList, Instances insts, Instance pivot, DistanceFunction distanceFunction) throws Exception { double radius = Double.NEGATIVE_INFINITY; for(int i=start; i<=end; i++) { double dist = distanceFunction.distance(pivot, insts.instance(instList[i]), Double.POSITIVE_INFINITY); if(dist>radius) radius = dist; } return Math.sqrt(radius); }
/** * Parses a given list of options. <p/> * <!-- options-start --> * Valid options are: <p/> * <!-- options-end --> * * @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 { m_bPrintNewick = Utils.getFlag('P', options); String optionString = Utils.getOption('N', options); if (optionString.length() != 0) { Integer temp = new Integer(optionString); setNumClusters(temp); } else { setNumClusters(2); } setDebug(Utils.getFlag('D', options)); setDistanceIsBranchLength(Utils.getFlag('B', options)); String sLinkType = Utils.getOption('L', options); if (sLinkType.compareTo("SINGLE") == 0) {setLinkType(new SelectedTag(SINGLE, TAGS_LINK_TYPE));} if (sLinkType.compareTo("COMPLETE") == 0) {setLinkType(new SelectedTag(COMPLETE, TAGS_LINK_TYPE));} if (sLinkType.compareTo("AVERAGE") == 0) {setLinkType(new SelectedTag(AVERAGE, TAGS_LINK_TYPE));} if (sLinkType.compareTo("MEAN") == 0) {setLinkType(new SelectedTag(MEAN, TAGS_LINK_TYPE));} if (sLinkType.compareTo("CENTROID") == 0) {setLinkType(new SelectedTag(CENTROID, TAGS_LINK_TYPE));} if (sLinkType.compareTo("WARD") == 0) {setLinkType(new SelectedTag(WARD, TAGS_LINK_TYPE));} if (sLinkType.compareTo("ADJCOMLPETE") == 0) {setLinkType(new SelectedTag(ADJCOMLPETE, TAGS_LINK_TYPE));} if (sLinkType.compareTo("NEIGHBOR_JOINING") == 0) {setLinkType(new SelectedTag(NEIGHBOR_JOINING, TAGS_LINK_TYPE));} String nnSearchClass = Utils.getOption('A', options); if(nnSearchClass.length() != 0) { String nnSearchClassSpec[] = Utils.splitOptions(nnSearchClass); if(nnSearchClassSpec.length == 0) { throw new Exception("Invalid DistanceFunction specification string."); } String className = nnSearchClassSpec[0]; nnSearchClassSpec[0] = ""; setDistanceFunction( (DistanceFunction) Utils.forName( DistanceFunction.class, className, nnSearchClassSpec) ); } else { setDistanceFunction(new EuclideanDistance()); } Utils.checkForRemainingOptions(options); }
/** * Calculates the radius of node. * * @param instList The indices array containing the indices of the * instances inside the node. * @param insts The actual instances object. instList points to * instances in this object. * @param pivot The centre/pivot of the node. * @param distanceFunction The distance fuction to use to calculate * the radius. * @return The radius of the node. * @throws Exception If there is some problem in calculating the * radius. */ public static double calcRadius(int[] instList, Instances insts,Instance pivot, DistanceFunction distanceFunction) throws Exception { return calcRadius(0, instList.length-1, instList, insts, pivot, distanceFunction); }
/** * Calculates the radius of a node based on its two * child nodes (if merging two nodes). * @param child1 The first child of the node. * @param child2 The second child of the node. * @param pivot The centre/pivot of the node. * @param distanceFunction The distance function to * use to calculate the radius * @return The radius of the node. * @throws Exception If there is some problem * in calculating the radius. */ public static double calcRadius(BallNode child1, BallNode child2, Instance pivot, DistanceFunction distanceFunction) throws Exception { Instance p1 = child1.getPivot(), p2 = child2.getPivot(); double radius = child1.getRadius() + distanceFunction.distance(p1, p2) + child2.getRadius(); return radius/2; }
/** * Sets the distance function to use for nearest neighbour search. * Currently only EuclideanDistance is supported. * * @param df the distance function to use * @throws Exception if not EuclideanDistance */ public void setDistanceFunction(DistanceFunction df) throws Exception { if (!(df instanceof EuclideanDistance)) throw new Exception("CoverTree currently only works with " + "EuclideanDistanceFunction."); m_DistanceFunction = m_EuclideanDistance = (EuclideanDistance) df; }