下面列出了怎么用weka.core.DistanceFunction的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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));
}
/**
* @param nearestNeighbour The nearest neighbour search algorithm to use.
* @author Michael
*
*/
public KNNAugSpaceSampler(final Instances preciseInsts, final Random rng, final int k, final NearestNeighbourSearch nearestNeighbour) {
super(preciseInsts, rng);
this.k = k;
DistanceFunction dist = new EuclideanDistance(preciseInsts);
String distOptionColumns = String.format("-R first-%d", preciseInsts.numAttributes() - 1);
String[] distOptions = {distOptionColumns};
try {
dist.setOptions(distOptions);
nearestNeighbour.setDistanceFunction(dist);
nearestNeighbour.setInstances(preciseInsts);
} catch (Exception e) {
logger.error("Could not configure distance function or setup nearest neighbour: {}", e);
}
nearestNeighbour.setMeasurePerformance(false);
this.nearestNeighbour = nearestNeighbour;
}
/**
* 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();
}
/**
* 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;
}
public static Pair<Instance, Double> findMinDistance(Instances data, Instance inst, DistanceFunction dist){
double min = dist.distance(data.get(0), inst);
Instance minI = data.get(0);
for (int i = 1; i < data.numInstances(); i++) {
double temp = dist.distance(data.get(i), inst);
if(temp < min){
min = temp;
minI = data.get(i);
}
}
return new Pair(minI, min);
}
public static double[][] createDistanceMatrix(Instances data, DistanceFunction distFunc){
double[][] distMatrix = new double[data.numInstances()][];
distFunc.setInstances(data);
for (int i = 1; i < data.numInstances(); i++){
distMatrix[i] = new double[i];
Instance first = data.get(i);
for (int n = 0; n < i; n++){
distMatrix[i][n] = distFunc.distance(first, data.get(n));
}
}
return distMatrix;
}
public TransformedDistanceMeasure(String name, Filter transformer,
DistanceFunction distanceFunction) {
setName(name);
setDistanceFunction(distanceFunction);
setTransformer(transformer);
}
public DistanceFunction getDistanceFunction() {
return distanceFunction;
}
protected void setDistanceFunction(DistanceFunction distanceFunction) {
if(distanceFunction == null) throw new NullPointerException();
this.distanceFunction = distanceFunction;
}
public TransformDistanceMeasure(String name,
Filter transformer, DistanceFunction distanceFunction) {
super(name, transformer, distanceFunction);
}
@Override
public void setDistanceFunction(DistanceFunction distanceFunction) {
super.setDistanceFunction(distanceFunction);
}
@Override public void setParams(final ParamSet param) {
super.setParams(param);
ParamHandler.setParam(param, getTransformerFlag(), this::setTransformer, Filter.class);
ParamHandler.setParam(param, DistanceMeasureable.getDistanceFunctionFlag(), this::setDistanceFunction,
DistanceFunction.class);
}
public KNNLOOCV(DistanceFunction df) {
super(df);
setAbleToEstimateOwnPerformance(true);
}
@Override public void setParams(final ParamSet params) {
ParamHandler.setParam(params, DistanceMeasureable.getDistanceFunctionFlag(), this::setDistanceFunction, DistanceFunction.class);
ParamHandler.setParam(params, getKFlag(), this::setK, Integer.class);
ParamHandler.setParam(params, getEarlyAbandonFlag(), this::setEarlyAbandon, Boolean.class);
ParamHandler.setParam(params, getRandomTieBreakFlag(), this::setRandomTieBreak, Boolean.class);
}
public DistanceFunction getDistanceFunction() {
return distanceFunction;
}
public void setDistanceFunction(final DistanceFunction distanceFunction) {
this.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
*/
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 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);
}
public void setDistanceFunction(DistanceFunction distFunc){
this.distFunc = distFunc;
}
/**
* Sets the options
*
* @param options
* a list of options as an array of strings
* @throws Exception
* if an option is not support
*/
public void setOptions(String[] options) throws Exception {
// Set the number of the cluster
String optionString = Utils.getOption('N', options);
if (optionString.length() != 0) {
setNumClusters(Integer.parseInt(optionString));
}
// Set the number of the maximum iterations
optionString = Utils.getOption("I", options);
if (optionString.length() != 0) {
setMaxIterations(Integer.parseInt(optionString));
}
// Set the repeat times
optionString = Utils.getOption("J", options);
if (optionString.length() != 0) {
setRepeatTimes(Integer.parseInt(optionString));
}
// Set the distance function
String distFunctionClass = Utils.getOption('A', options);
if (distFunctionClass.length() != 0) {
String distFunctionClassSpec[] = Utils.splitOptions(distFunctionClass);
if (distFunctionClassSpec.length == 0) {
throw new Exception("Invalid DistanceFunction specification string.");
}
String className = distFunctionClassSpec[0];
distFunctionClassSpec[0] = "";
setDistanceFunction(
(DistanceFunction) Utils.forName(DistanceFunction.class, className, distFunctionClassSpec));
} else {
setDistanceFunction(new EuclideanDistance());
}
// Set whether to output the cluster result
m_SaveClusterResult = Utils.getFlag("s", options);
// Other options
super.setOptions(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;
}
/**
* 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 df is not EuclideanDistance or ManhattanDistance
*/
public void setDistanceFunction(DistanceFunction df) throws Exception {
if ((df instanceof EuclideanDistance) || (df instanceof ManhattanDistance)) {
m_DistanceFunction = df;
} else {
throw new Exception("MyPAM only support the Euclidean or Manhattan distance.");
}
}
/**
* returns the distance function currently in use.
*
* @return the distance function
*/
public DistanceFunction getDistanceFunction() {
return m_DistanceFunction;
}
/**
* Parses a given list of options.
* <p/>
*
<!-- options-start -->
* Valid options are: <p/>
*
* <pre> -N <num>
* number of clusters.
* (default 2).</pre>
*
* <pre> -P
* Initialize using the k-means++ method.
* </pre>
*
* <pre> -V
* Display std. deviations for centroids.
* </pre>
*
* <pre> -M
* Replace missing values with mean/mode.
* </pre>
*
* <pre> -A <classname and options>
* Distance function to use.
* (default: weka.core.EuclideanDistance)</pre>
*
* <pre> -I <num>
* Maximum number of iterations.
* </pre>
*
* <pre> -O
* Preserve order of instances.
* </pre>
*
* <pre> -fast
* Enables faster distance calculations, using cut-off values.
* Disables the calculation/output of squared errors/distances.
* </pre>
*
* <pre> -num-slots <num>
* Number of execution slots.
* (default 1 - i.e. no parallelism)</pre>
*
* <pre> -S <num>
* Random number seed.
* (default 10)</pre>
*
<!-- 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_displayStdDevs = Utils.getFlag("V", options);
m_dontReplaceMissing = Utils.getFlag("M", options);
m_initializeWithKMeansPlusPlus = Utils.getFlag('P', options);
String optionString = Utils.getOption('N', options);
if (optionString.length() != 0) {
setNumClusters(Integer.parseInt(optionString));
}
optionString = Utils.getOption("I", options);
if (optionString.length() != 0) {
setMaxIterations(Integer.parseInt(optionString));
}
String distFunctionClass = Utils.getOption('A', options);
if (distFunctionClass.length() != 0) {
String distFunctionClassSpec[] = Utils.splitOptions(distFunctionClass);
if (distFunctionClassSpec.length == 0) {
throw new Exception("Invalid DistanceFunction specification string.");
}
String className = distFunctionClassSpec[0];
distFunctionClassSpec[0] = "";
setDistanceFunction((DistanceFunction) Utils.forName(
DistanceFunction.class, className, distFunctionClassSpec));
} else {
setDistanceFunction(new EuclideanDistance());
}
m_PreserveOrder = Utils.getFlag("O", options);
m_FastDistanceCalc = Utils.getFlag("fast", options);
String slotsS = Utils.getOption("num-slots", options);
if (slotsS.length() > 0) {
setNumExecutionSlots(Integer.parseInt(slotsS));
}
super.setOptions(options);
}
/**
* gets the "binary" distance value.
* @param distanceF the distance function with all options set
*/
public void setDistanceF(DistanceFunction distanceF) {
m_DistanceF = distanceF;
}