All Packages  Class Hierarchy  This Package  Previous  Next  Index  WEKA's home

Class weka.core.Utils

java.lang.Object
   |
   +----weka.core.Utils

public final class Utils
extends Object
Class implementing some simple utility methods.

Version:
$Revision: 1.14 $
Author:
Eibe Frank (eibe@cs.waikato.ac.nz), Yong Wang (yongwang@cs.waikato.ac.nz), Len Trigg (trigg@cs.waikato.ac.nz)

Variable Index

 o log2
The natural logarithm of 2.
 o SMALL
The small deviation allowed in double comparisons

Constructor Index

 o Utils()

Method Index

 o backQuoteChars(String)
Converts carriage returns and new lines in a string into \r and \n.
 o checkForRemainingOptions(String[])
Checks if the given array contains any non-empty options.
 o correlation(double[], double[], int)
Returns the correlation coefficient of two double vectors.
 o doubleToString(double, int)
Rounds a double and converts it into String.
 o doubleToString(double, int, int)
Rounds a double and converts it into a formatted decimal-justified String.
 o eq(double, double)
Tests if a is equal to b.
 o forName(Class, String, String[])
Creates a new instance of an object given it's class name and (optional) arguments to pass to it's setOptions method.
 o getFlag(char, String[])
Checks if the given array contains the flag "-Char".
 o getOption(char, String[])
Gets an option indicated by a flag "-Char" from the given array of strings.
 o gr(double, double)
Tests if a is smaller than b.
 o grOrEq(double, double)
Tests if a is greater or equal to b.
 o info(int[])
Computes entropy for an array of integers.
 o joinOptions(String[])
Joins all the options in an option array into a single string, as might be used on the command line.
 o log2(double)
Returns the logarithm of a for base 2.
 o main(String[])
Main method for testing this class.
 o maxIndex(double[])
Returns index of maximum element in a given array of doubles.
 o maxIndex(int[])
Returns index of maximum element in a given array of integers.
 o mean(double[])
Computes the mean for an array of doubles.
 o minIndex(double[])
Returns index of minimum element in a given array of doubles.
 o minIndex(int[])
Returns index of minimum element in a given array of integers.
 o normalize(double[])
Normalizes the doubles in the array by their sum.
 o normalize(double[], double)
Normalizes the doubles in the array using the given value.
 o padLeft(String, int)
Pads a string to a specified length, inserting spaces on the left as required.
 o padRight(String, int)
Pads a string to a specified length, inserting spaces on the right as required.
 o partitionOptions(String[])
Returns the secondary set of options (if any) contained in the supplied options array.
 o quote(String)
Quotes a string if it contains special characters.
 o round(double)
Rounds a double to the next nearest integer value.
 o roundDouble(double, int)
Rounds a double to the given number of decimal places.
 o sm(double, double)
Tests if a is smaller than b.
 o smOrEq(double, double)
Tests if a is smaller or equal to b.
 o sort(double[])
Sorts a given array of doubles in ascending order and returns an array of integers with the positions of the elements of the original array in the sorted array.
 o sort(int[])
Sorts a given array of integers in ascending order and returns an array of integers with the positions of the elements of the original array in the sorted array.
 o splitOptions(String)
Split up a string containing options into an array of strings, one for each option.
 o sum(double[])
Computes the sum of the elements of an array of doubles.
 o sum(int[])
Computes the sum of the elements of an array of integers.
 o variance(double[])
Computes the variance for an array of doubles.
 o xlogx(int)
Returns c*log2(c) for a given integer value c.

Variables

 o log2
 public static double log2
The natural logarithm of 2.

 o SMALL
 public static double SMALL
The small deviation allowed in double comparisons

Constructors

 o Utils
 public Utils()

Methods

 o correlation
 public static final double correlation(double y1[],
                                        double y2[],
                                        int n)
Returns the correlation coefficient of two double vectors.

Parameters:
y1 - double vector 1
y2 - double vector 2
n - the length of two double vectors
Returns:
the correlation coefficient
 o padLeft
 public static String padLeft(String inString,
                              int length)
Pads a string to a specified length, inserting spaces on the left as required. If the string is too long, characters are removed (from the right).

Parameters:
inString - the input string
length - the desired length of the output string
Returns:
the output string
 o padRight
 public static String padRight(String inString,
                               int length)
Pads a string to a specified length, inserting spaces on the right as required. If the string is too long, characters are removed (from the right).

Parameters:
inString - the input string
length - the desired length of the output string
Returns:
the output string
 o doubleToString
 public static String doubleToString(double value,
                                     int afterDecimalPoint)
Rounds a double and converts it into String.

Parameters:
value - the double value
afterDecimalPoint - the (maximum) number of digits permitted after the decimal point
Returns:
the double as a formatted string
 o doubleToString
 public static String doubleToString(double value,
                                     int width,
                                     int afterDecimalPoint)
Rounds a double and converts it into a formatted decimal-justified String. Trailing 0's are replaced with spaces.

Parameters:
value - the double value
width - the width of the string
afterDecimalPoint - the number of digits after the decimal point
Returns:
the double as a formatted string
 o eq
 public static boolean eq(double a,
                          double b)
Tests if a is equal to b.

Parameters:
a - a double
b - a double
 o checkForRemainingOptions
 public static void checkForRemainingOptions(String options[]) throws Exception
Checks if the given array contains any non-empty options.

Parameters:
strings - an array of strings
Throws: Exception
if there are any non-empty options
 o getFlag
 public static boolean getFlag(char flag,
                               String options[]) throws Exception
Checks if the given array contains the flag "-Char". Stops searching at the first marker "--". If the flag is found, it is replaced with the empty string.

Parameters:
flag - the character indicating the flag.
strings - the array of strings containing all the options.
Returns:
true if the flag was found
Throws: Exception
if an illegal option was found
 o getOption
 public static String getOption(char flag,
                                String options[]) throws Exception
Gets an option indicated by a flag "-Char" from the given array of strings. Stops searching at the first marker "--". Replaces flag and option with empty strings.

Parameters:
flag - the character indicating the option.
options - the array of strings containing all the options.
Returns:
the indicated option or an empty string
Throws: Exception
if the option indicated by the flag can't be found
 o quote
 public static String quote(String string)
Quotes a string if it contains special characters. The following rules are applied: A character is backquoted version of it is one of " ' % \ \n \r \t. A string is enclosed within single quotes if a character has been backquoted using the previous rule above or contains { } or is exactly equal to the strings , ? space or "" (empty string). A quoted question mark distinguishes it from the missing value which is represented as an unquoted question mark in arff files.

Parameters:
string - the string to be quoted
Returns:
the string (possibly quoted)
 o backQuoteChars
 public static String backQuoteChars(String string)
Converts carriage returns and new lines in a string into \r and \n. Backquotes the following characters: ` " \ \t and %

Parameters:
string - the string
Returns:
the converted string
 o partitionOptions
 public static String[] partitionOptions(String options[])
Returns the secondary set of options (if any) contained in the supplied options array. The secondary set is defined to be any options after the first "--". These options are removed from the original options array.

Parameters:
options - the input array of options
Returns:
the array of secondary options
 o splitOptions
 public static String[] splitOptions(String optionString)
Split up a string containing options into an array of strings, one for each option.

Parameters:
optionString - the string containing the options
Returns:
the array of options
 o joinOptions
 public static String joinOptions(String optionArray[])
Joins all the options in an option array into a single string, as might be used on the command line.

Parameters:
optionArray - the array of options
Returns:
the string containing all options.
 o forName
 public static Object forName(Class classType,
                              String className,
                              String options[]) throws Exception
Creates a new instance of an object given it's class name and (optional) arguments to pass to it's setOptions method. If the object implements OptionHandler and the options parameter is non-null, the object will have it's options set. Example use:

 String classifierName = Utils.getOption('W', options);
 Classifier c = (Classifier)Utils.forName(Classifier.class,
                                          classifierName,
                                          options);
 setClassifier(c);
 

Parameters:
classType - the class that the instantiated object should be assignable to -- an exception is thrown if this is not the case
className - the fully qualified class name of the object
options - an array of options suitable for passing to setOptions. May be null. Any options accepted by the object will be removed from the array.
Returns:
the newly created object, ready for use.
Throws: Exception
if the class name is invalid, or if the class is not assignable to the desired class type, or the options supplied are not acceptable to the object
 o info
 public static double info(int counts[])
Computes entropy for an array of integers.

Parameters:
counts - array of counts
Returns:
s - a log2 a - b log2 b - c log2 c + (a+b+c) log2 (a+b+c) when given array [a b c]
 o smOrEq
 public static boolean smOrEq(double a,
                              double b)
Tests if a is smaller or equal to b.

Parameters:
a - a double
b - a double
 o grOrEq
 public static boolean grOrEq(double a,
                              double b)
Tests if a is greater or equal to b.

Parameters:
a - a double
b - a double
 o sm
 public static boolean sm(double a,
                          double b)
Tests if a is smaller than b.

Parameters:
a - a double
b - a double
 o gr
 public static boolean gr(double a,
                          double b)
Tests if a is smaller than b.

Parameters:
a - a double
b - a double
 o log2
 public static double log2(double a)
Returns the logarithm of a for base 2.

Parameters:
a - a double
 o maxIndex
 public static int maxIndex(double doubles[])
Returns index of maximum element in a given array of doubles. First maximum is returned.

Parameters:
doubles - the array of doubles
Returns:
the index of the maximum element
 o maxIndex
 public static int maxIndex(int ints[])
Returns index of maximum element in a given array of integers. First maximum is returned.

Parameters:
ints - the array of integers
Returns:
the index of the maximum element
 o mean
 public static double mean(double vector[])
Computes the mean for an array of doubles.

Parameters:
vector - the array
Returns:
the mean
 o minIndex
 public static int minIndex(int ints[])
Returns index of minimum element in a given array of integers. First minimum is returned.

Parameters:
ints - the array of integers
Returns:
the index of the minimum element
 o minIndex
 public static int minIndex(double doubles[])
Returns index of minimum element in a given array of doubles. First minimum is returned.

Parameters:
doubles - the array of doubles
Returns:
the index of the minimum element
 o normalize
 public static void normalize(double doubles[]) throws Exception
Normalizes the doubles in the array by their sum.

Parameters:
doubles - the array of double
Throws: Exception
if sum is NaN
 o normalize
 public static void normalize(double doubles[],
                              double sum) throws Exception
Normalizes the doubles in the array using the given value.

Parameters:
doubles - the array of double
sum - the value by which the doubles are to be normalized
Throws: Exception
if sum is zero
 o round
 public static int round(double value)
Rounds a double to the next nearest integer value. The JDK version of it doesn't work properly.

Parameters:
value - the double value
Returns:
the resulting integer value
 o roundDouble
 public static double roundDouble(double value,
                                  int afterDecimalPoint)
Rounds a double to the given number of decimal places.

Parameters:
value - the double value
afterDecimalPoint - the number of digits after the decimal point
Returns:
the double rounded to the given precision
 o sort
 public static int[] sort(double array[])
Sorts a given array of doubles in ascending order and returns an array of integers with the positions of the elements of the original array in the sorted array. The sort is stable. (Equal elements remain in their original order.)

Parameters:
array - this array is not changed by the method!
Returns:
an array of integers with the positions in the sorted array.
 o sort
 public static int[] sort(int array[])
Sorts a given array of integers in ascending order and returns an array of integers with the positions of the elements of the original array in the sorted array. The sort is stable. (Equal elements remain in their original order.)

Parameters:
array - this array is not changed by the method!
Returns:
an array of integers with the positions in the sorted array.
 o variance
 public static double variance(double vector[])
Computes the variance for an array of doubles.

Parameters:
vector - the array
Returns:
the variance
 o sum
 public static double sum(double doubles[])
Computes the sum of the elements of an array of doubles.

Parameters:
doubles - the array of double
Returns:
s the sum of the elements
 o sum
 public static int sum(int ints[])
Computes the sum of the elements of an array of integers.

Parameters:
ints - the array of integers
Returns:
s the sum of the elements
 o xlogx
 public static double xlogx(int c)
Returns c*log2(c) for a given integer value c.

Parameters:
c - an integer value
Returns:
s c*log2(c) (but is careful to return 0 if c is 0)
 o main
 public static void main(String ops[])
Main method for testing this class.

Parameters:
ops - some dummy options

All Packages  Class Hierarchy  This Package  Previous  Next  Index  WEKA's home