Class ColumnDataClassifier


  • public class ColumnDataClassifier
    extends Object
    ColumnDataClassifier provides a command-line interface for doing context-free (independent) classification of a series of data items, where each data item is represented by a line of a file, as a list of String variables, in tab-separated columns. Some features will interpret these variables as numbers, but the code is mainly oriented towards generating features for string classification. To designate a real-valued feature, use the realValued option described below. The classifier by default is a maxent classifier (also known as a softmax classifier or a discriminative loglinear classifier; equivalent to multiclass logistic regression apart from a slightly different symmetric parameterization. It also implements a Bernoulli Naive Bayes model and can implement an SVM by an external call to SVMlight. You can also use ColumnDataClassifier programmatically, where its main usefulness beyond simply building your own LinearClassifier is that it provides easy conversion of data items into features, using the same properties as the command-line version. You can see example of usage in the class ClassifierDemo. Input files are expected to be one data item per line with two or more columns indicating the class of the item and one or more predictive features. Columns are separated by tab characters. Tab and newline characters cannot occur inside field values (there is no escaping mechanism); any other characters are legal in field values. Typical usage: java edu.stanford.nlp.classify.ColumnDataClassifier -prop propFile or java -mx300m edu.stanford.nlp.classify.ColumnDataClassifier -trainFile trainFile -testFile testFile -useNGrams|... > output (Note that for large data sets, you may wish to specify the amount of memory available to Java, such as in the second example above.) In the simplest case, there are just two tab-separated columns in the training input: the first for the class, and the second for the String datum which has that class. In more complex uses, each datum can be multidimensional, and there are many columns of data attributes. To illustrate simple uses, and the behavior of Naive Bayes and Maximum entropy classifiers, example files corresponding to the examples from the Manning and Klein maxent classifier tutorial, slides 46-49, available at https://nlp.stanford.edu/software/classifier.html are included in the classify package source directory (files starting with "easy"). Other examples appear in the examples directory of the distributed classifier. In many instances, parameters can either be given on the command line or provided using a Properties file (specified on the command-line with -prop propFile). Option names are the same as property names with a preceding dash. Boolean properties can simply be turned on via the command line. Parameters of types int, String, and double take a following argument after the option. Command-line parameters can only define features for the first column describing the datum. If you have multidimensional data, you need to use a properties file. Property names, as below, are either global (things like the testFile name) or are seen as properties that define features for the first data column (NOTE: we count columns from 0 - unlike the Unix cut command!). To specify features for a particular data column, precede a feature by a column number and then a period (for example, 3.wordShape=chris4). If no number is specified, then the default interpretation is column 0. Note that in properties files you must give a value to boolean properties (e.g., 2.useString=true); just giving the property name (as 2.useString) isn't sufficient. The following properties are recognized:
    Properties for ColumnDataClassifier
    Property NameTypeDefault ValueDescriptionFeatName
    loadClassifier Stringn/aPath of serialized classifier file to load
    serializeToStringn/aPath to serialize classifier to
    printToStringn/aPath to print a text representation of the linear classifier to
    trainFileStringn/aPath of file to use as training data
    testFileStringn/aPath of file to use as test data
    encodingStringutf-8Character encoding of training and test file, e.g., utf-8, GB18030, or iso-8859-1
    displayedColumnint1Column number that will be printed out to stdout in the output next to the gold class and the chosen class. This is just an aide memoire. If the value is negative, nothing is printed.
    displayAllAnswersbooleanfalseIf true, print all classes and their probability, sorted by probability, rather than just the highest scoring and correct classes.
    goldAnswerColumnint0Column number that contains the correct class for each data item (again, columns are numbered from 0 up).
    groupingColumnint-1Column for grouping multiple data items for the purpose of computing ranking accuracy. This is appropriate when only one datum in a group can be correct, and the intention is to choose the highest probability one, rather than accepting all above a threshold. Multiple items in the same group must be contiguous in the test file (otherwise it would be necessary to cache probabilities and groups for the entire test file to check matches). If it is negative, no grouping column is used, and no ranking accuracy is reported.
    rankingScoreColumnint-1If this parameter is non-negative and a groupingColumn is defined, then an average ranking score will be calculated by scoring the chosen candidate from a group according to its value in this column (for instance, the values of this column can be set to a mean reciprocal rank of 1.0 for the best answer, 0.5 for the second best and so on, or the value of this column can be a similarity score reflecting the similarity of the answer to the true answer.
    rankingAccuracyClassStringnullIf this and groupingColumn are defined (positive), then the system will compute a ranking accuracy under the assumption that there is (at most) one assignment of this class for each group, and ranking accuracy counts the classifier as right if that datum is the one with highest probability according to the model.
    useStringbooleanfalseGives you a feature for whole string sS-str
    useClassFeaturebooleanfalseInclude a feature for the class (as a class marginal). This is the same thing as having a bias vector or having an always-on feature in a model.CLASS
    binnedLengthsStringnullIf non-null, treat as a sequence of comma separated integer bounds, where items above the previous bound (if any) up to the next bound (inclusive) are binned (e.g., "1,5,15,30,60"). The feature represents the length of the String in this column.Len-range
    binnedLengthsStatisticsbooleanfalseIf true, print to stderr contingency table of statistics for binnedLengths.
    binnedValuesStringnullIf non-null, treat as a sequence of comma separated double bounds, where data items above the previous bound up to the next bound (inclusive) are binned. If a value in this column isn't a legal double, then the value is treated as binnedValuesNaN.Val-range
    binnedValuesNaNdouble-1.0If the value of a numeric binnedValues field is not a number, it will be given this value.
    binnedValuesStatisticsbooleanfalseIf true, print to stderr a contingency table of statistics for binnedValues.
    countCharsStringnullIf non-null, count the number of occurrences of each character in the String, and make a feature for each character, binned according to countCharsBinsChar-ch-range
    countCharsBinsString"0,1"Treat as a sequence of comma separated integer bounds, where character counts above the previous bound up to and including the next bound are binned. For instance, a value of "0,2" will give 3 bins, dividing a character count into bins of 0, 1-or-2, and 3-or-more occurrences.
    splitWordsRegexpStringnullIf defined, use this as a regular expression on which to split the whole string (as in the String.split() function, which will return the things between delimiters, and discard the delimiters). The resulting split-up "words" will be used in classifier features iff one of the other "useSplit" options is turned on.
    splitWordsTokenizerRegexpStringnullIf defined, use this as a regular expression to cut initial pieces off a String. Either this regular expression or splitWordsIgnoreRegexp should always match the start of the String, and the size of the token is the number of characters matched. So, for example, one can group letter and number characters but do nothing else with a regular expression like ([A-Za-z]+|[0-9]+|.), where the last disjunct will match any other single character. (If neither regular expression matches, the first character of the string is treated as a one character word, and then matching is tried again, but in this case a warning message is printed.) Note that, for Java regular expressions with disjunctions like this, the match is the first matching disjunction, not the longest matching disjunction, so patterns with common prefixes need to be ordered from most specific (longest) to least specific (shortest).) The resulting split up "words" will be used in classifier features iff one of the other "useSplit" options is turned on. Note that as usual for Java String processing, backslashes must be doubled in the regular expressions that you write.
    splitWordsIgnoreRegexpString\\s+If non-empty, this regexp is used to determine character sequences which should not be returned as tokens when using splitWordsTokenizerRegexp or splitWordsRegexp. With the former, first the program attempts to match this regular expression at the start of the string (with lookingAt()) and if it matches, those characters are discarded, but if it doesn't match then splitWordsTokenizerRegexp is tried. With splitWordsRegexp, this is used to filter tokens (with matches() resulting from the splitting. By default this regular expression is set to be all whitespace tokens (i.e., \\s+). Set it to an empty string to get all tokens returned.
    splitWordsWithPTBTokenizerbooleanfalseIf true, and splitWordsRegexp and splitWordsTokenizerRegexp are false, then will tokenize using the PTBTokenizer
    useSplitWordsbooleanfalseMake features from the "words" that are returned by dividing the string on splitWordsRegexp or splitWordsTokenizerRegexp. Requires splitWordsRegexp or splitWordsTokenizerRegexp.SW-str
    useLowercaseSplitWordsbooleanfalseMake features from the "words" that are returned by dividing the string on splitWordsRegexp or splitWordsTokenizerRegexp and then lowercasing the result. Requires splitWordsRegexp or splitWordsTokenizerRegexp. Note that this can be specified independently of useSplitWords. You can put either or both original cased and lowercased words in as features.LSW-str
    useSplitWordPairsbooleanfalseMake features from the pairs of adjacent "words" that are returned by dividing the string into splitWords. Requires splitWordsRegexp or splitWordsTokenizerRegexp. This doesn't add features for the first and last words being next to a boundary; if you want those, also set useSplitFirstLastWords.SWP-str1-str2
    useLowercaseSplitWordPairsbooleanfalseMake features from the lowercased form of the pairs of adjacent "words" that are returned by dividing the string into splitWords. Requires splitWordsRegexp or splitWordsTokenizerRegexp. This doesn't add features for the first and last words being next to a boundary; if you want those, also set useLowercaseSplitFirstLastWords.LSWP-str1-str2
    useAllSplitWordPairsbooleanfalseMake features from all pairs of "words" that are returned by dividing the string into splitWords. Requires splitWordsRegexp or splitWordsTokenizerRegexp.ASWP-str1-str2
    useAllSplitWordTriplesbooleanfalseMake features from all triples of "words" that are returned by dividing the string into splitWords. Requires splitWordsRegexp or splitWordsTokenizerRegexp.ASWT-str1-str2-str3
    useSplitWordNGramsbooleanfalseMake features of adjacent word n-grams of lengths between minWordNGramLeng and maxWordNGramLeng inclusive. Note that these are word sequences, not character n-grams.SW#-str1-str2-strN
    splitWordCountbooleanfalseIf true, the value of this real-valued feature is the number of split word tokens in the column.SWNUM
    logSplitWordCountbooleanfalseIf true, the value of this real-valued feature is the log of the number of split word tokens in the column.LSWNUM
    binnedSplitWordCountsStringnullIf non-null, treat as a sequence of comma-separated integer bounds, where items above the previous bound (if any) up to the next bound (inclusive) are binned (e.g., "1,5,15,30,60"). The feature represents the number of split words in this column.SWNUMBIN-range
    maxWordNGramLengint-1If this number is positive, word n-grams above this size will not be used in the model
    minWordNGramLengint1Must be positive. word n-grams below this size will not be used in the model
    wordNGramBoundaryRegexpStringnullIf this is defined and the regexp matches, then the ngram stops
    useSplitFirstLastWordsbooleanfalseMake a feature from each of the first and last "words" that are returned as splitWords. This is equivalent to having word bigrams with boundary tokens at each end of the sequence (they get a special feature). Requires splitWordsRegexp or splitWordsTokenizerRegexp.SFW-str, SLW-str
    useLowercaseSplitFirstLastWordsbooleanfalseMake a feature from each of the first and last "words" that are returned as splitWords. This is equivalent to having word bigrams with boundary tokens at each end of the sequence (they get a special feature). Requires splitWordsRegexp or splitWordsTokenizerRegexp.LSFW-str, LSLW-str
    useSplitNGramsbooleanfalseMake features from letter n-grams - internal as well as edge all treated the same - after the data string has been split into tokens. Requires splitWordsRegexp or splitWordsTokenizerRegexp.S#-str
    useSplitPrefixSuffixNGramsbooleanfalseMake features from prefixes and suffixes of each token, after splitting string with splitWordsRegexp. Requires splitWordsRegexp or splitWordsTokenizerRegexp.S#B-str, S#E-str
    useSplitWordVectorsStringnull(If non-null) load word vectors from this file and add their average over all split words as real-valued features. Requires splitWordsRegexp or splitWordsTokenizerRegexp. Note that for best results you need a close match between your tokenization and that used by the word vectors.SWV-num
    useNGramsbooleanfalseMake features from letter n-grams - internal as well as edge all treated the same.#-str
    usePrefixSuffixNGramsbooleanfalseMake features from prefix and suffix substrings of the string.#B-str, #E-str
    lowercasebooleanfalseMake the input string lowercase so all features work uncased
    lowercaseNGramsbooleanfalseMake features from letter n-grams all lowercase (for all of useNGrams, usePrefixSuffixNGrams, useSplitNGrams, and useSplitPrefixSuffixNGrams)
    maxNGramLengint-1If this number is positive, n-grams above this size will not be used in the model
    minNGramLengint2Must be positive. n-grams below this size will not be used in the model
    partialNGramRegexpStringnullIf this is defined and the regexp matches, then n-grams are made only from the matching text (if no capturing groups are defined) or from the first capturing group of the regexp, if there is one. This substring is used for both useNGrams and usePrefixSuffixNGrams.
    realValuedbooleanfalseTreat this column as real-valued and do not perform any transforms on the feature value.Value
    logTransformbooleanfalseTreat this column as real-valued and use the log of the value as the feature value.Log
    logitTransformbooleanfalseTreat this column as real-valued and use the logit of the value as the feature value.Logit
    sqrtTransformbooleanfalseTreat this column as real-valued and use the square root of the value as the feature value.Sqrt
    filenamebooleanfalseTreat this column as a filename (path) and then use the contents of that file (assumed to be plain text) in the calculation of features according to other flag specifications.
    wordShapeStringnoneEither "none" for no wordShape use, or the name of a word shape function recognized by WordShapeClassifier.lookupShaper(String), such as "dan1" or "chris4". WordShape functions equivalence-class strings based on the pattern of letter, digit, and symbol characters that they contain. The details depend on the particular function chosen.SHAPE-str
    splitWordShapeStringnoneEither "none" for no wordShape or the name of a word shape function recognized by WordShapeClassifier.lookupShaper(String). This is applied to each "word" found by splitWordsRegexp or splitWordsTokenizerRegexp.SSHAPE-str
    featureMinimumSupportint0A feature, that is, an (observed,class) pair, will only be included in the model providing it is seen a minimum of this number of times in the training data.
    biasedHyperplaneStringnullIf non-null, a sequence of comma-separated pairs of className prob. An item will only be classified to a certain class className if its probability of class membership exceeds the given conditional probability prob; otherwise it will be assigned to a different class. If this list of classes is exhaustive, and no condition is satisfied, then the most probable class is chosen.
    printFeaturesStringnullPrint out the features and their values for each instance to a file based on this name.
    printClassifierStringnullStyle in which to print the classifier. One of: HighWeight, HighMagnitude, AllWeights, WeightHistogram, WeightDistribution. See LinearClassifier class for details.
    printClassifierParamint100A parameter to the printing style, which may give, for example the number of parameters to print (for HighWeight or HighMagnitude).
    justifybooleanfalseFor each test data item, print justification (weights) for active features used in classification.
    exitAfterTrainingFeaturizationbooleanfalseIf true, the program exits after reading the training data (trainFile) and before building a classifier. This is useful in conjunction with printFeatures, if one only wants to convert data to features for use with another classifier.
    verboseOptimizationbooleanfalseIf true, print much more detail about classifier optimization.
    internbooleanfalseIf true, (String) intern all of the (final) feature names. Recommended (this saves memory, but slows down feature generation in training).
    cacheNGramsbooleanfalseIf true, record the NGram features that correspond to a String (under the current option settings and reuse rather than recalculating if the String is seen again. Disrecommended (speeds training but can require enormous amounts of memory).
    useNBbooleanfalseUse a Naive Bayes generative classifier (over all features) rather than a discriminative logistic regression classifier. (Set useClass to true to get a prior term.)
    useBinarybooleanfalseUse the binary classifier (i.e. use LogisticClassifierFactory, rather than LinearClassifierFactory) to get classifier
    l1regdouble0.0If set to be larger than 0, uses L1 regularization
    useAdaptL1booleanfalseIf true, uses adaptive L1 regularization to find value of l1reg that gives the desired number of features set by limitFeatures
    l1regmindouble0.0Minimum L1 in search
    l1regmaxdouble500.0Maximum L1 in search
    featureWeightThresholddouble0.0Threshold of model weight at which feature is kept. "Unimportant" low weight features are discarded. (Currently only implemented for adaptL1.)
    limitFeaturesLabelsStringnullIf set, only include features for these labels in the desired number of features
    limitFeaturesint0If set to be larger than 0, uses adaptive L1 regularization to find value of l1reg that gives the desired number of features
    priorString/intquadraticType of prior (regularization penalty on weights). Possible values are null, "no", "quadratic", "huber", "quartic", "cosh", or "adapt". See LogPrior for more information.
    useSumbooleanfalseDo optimization via summed conditional likelihood, rather than the product. (This is expensive, non-standard, and somewhat unstable, but can be quite effective: see Klein and Manning 2002 EMNLP paper.)
    tolerancedouble1e-4Convergence tolerance in parameter optimization
    sigmadouble1.0A parameter to several of the smoothing (i.e., regularization) methods, usually giving a degree of smoothing as a standard deviation (with small positive values being stronger smoothing, and bigger values weaker smoothing). However, for Naive Bayes models it is the amount of add-sigma smoothing, so a bigger number is more smoothing.
    epsilondouble0.01Used only as a parameter in the Huber loss: this is the distance from 0 at which the loss changes from quadratic to linear
    useQNbooleantrueUse Quasi-Newton optimization if true, otherwise use Conjugate Gradient optimization. Recommended.
    QNsizeint15Number of previous iterations of Quasi-Newton to store (this increases memory use, but speeds convergence by letting the Quasi-Newton optimization more effectively approximate the second derivative).
    featureFormatbooleanfalseAssumes the input file isn't text strings but already featurized. One column is treated as the class column (as defined by goldAnswerColumn, and all other columns are treated as features of the instance. (If answers are not present, set goldAnswerColumn to a negative number.)
    trainFromSVMLightbooleanfalseAssumes the trainFile is in SVMLight format (see SVMLight web page for more information)
    testFromSVMLightbooleanfalseAssumes the testFile is in SVMLight format
    printSVMLightFormatToStringnullIf non-null, print the featurized training data to an SVMLight format file (usually used with exitAfterTrainingFeaturization). This is just an option to write out data in a particular format. After that, you're on your own using some other piece of software that reads SVMlight format files.
    crossValidationFoldsint-1If positive, the training data is divided in to this many folds and cross-validation is done on the training data (prior to testing on test data, if it is also specified)
    printCrossValidationDecisionsbooleanfalseWhether to print the individual classification decisions in cross-validation training, if crossValidationFolds is positive.
    shuffleTrainingDatabooleanfalseIf true, the training data is shuffled prior to training and cross-validation. This is vital in cross-validation if the training data is otherwise sorted by class.
    shuffleSeedlong0If non-zero, and the training data is being shuffled, this is used as the seed for the Random. Otherwise, System.nanoTime() is used.
    csvInputbooleanfalseIf true, reads train and test file in csv format, with support for quoted fields.
    inputFormatStringnullIf "header" then reads file with first line treated as header; if "comments" treats lines starting with # as comments; else treated as "plain" tsv/csv file
    csvOutputStringnullIf non-null, used to format the output of the classifier. This is a printf-style format specification where %0 through %9 can print columns of the input, %c prints the assigned class and %n a newline character. This option can produce Kaggle-format output files!
    Author:
    Christopher Manning, Anna Rafferty, Angel Chang (added options for using l1reg)
    • Constructor Detail

      • ColumnDataClassifier

        public ColumnDataClassifier​(String filename)
        Construct a ColumnDataClassifier.
        Parameters:
        filename - The file with properties which specifies all aspects of behavior. See the class documentation for details of the properties.
      • ColumnDataClassifier

        public ColumnDataClassifier​(Properties props)
        Construct a ColumnDataClassifier.
        Parameters:
        props - The properties object specifies all aspects of its behavior. See the class documentation for details of the properties.
      • ColumnDataClassifier

        public ColumnDataClassifier​(Pair<edu.stanford.nlp.classify.ColumnDataClassifier.Flags[],​Classifier<String,​String>> flagsClassifierPair)
        Construct a ColumnDataClassifier.
        Parameters:
        flagsClassifierPair - A Pair of a Flags object array specifies all aspects of featurization and other behavior and a Classifier that will be used.
    • Method Detail

      • makeDatumFromLine

        public Datum<String,​String> makeDatumFromLine​(String line)
        Entry point for taking a String (formatted as a line of a TSV file) and translating it into a Datum of features. If real-valued features are used, this method returns an RVFDatum; otherwise, categorical features are used.
        Parameters:
        line - Line of file
        Returns:
        A Datum (may be an RVFDatum; never null)
      • makeDatumFromStrings

        public Datum<String,​String> makeDatumFromStrings​(String[] strings)
        Takes a String[] of elements and translates them into a Datum of features. If real-valued features are used, this method accesses makeRVFDatumFromLine and returns an RVFDatum; otherwise, categorical features are used.
        Parameters:
        strings - The elements that features are made from (the columns of a TSV/CSV file)
        Returns:
        A Datum (may be an RVFDatum; never null)
      • readTrainingExamples

        public GeneralDataset<String,​String> readTrainingExamples​(String fileName)
        Read a set of training examples from a file, and return the data in a featurized form. If feature selection is asked for, the returned featurized form is after feature selection has been applied.
        Parameters:
        fileName - File with supervised training examples.
        Returns:
        A GeneralDataset, where the labels and features are Strings.
      • readAndReturnTrainingExamples

        public Pair<GeneralDataset<String,​String>,​List<String[]>> readAndReturnTrainingExamples​(String fileName)
        Read a set of training examples from a file, and return the data in a featurized form and in String form. If feature selection is asked for, the returned featurized form is after feature selection has been applied. (Note that at present we sometimes need the String form, e.g., for cross-validation, and so we always pass to readDataset that we are inTestPhase (even though we are training), so this second element is filled in.)
        Parameters:
        fileName - File with supervised training examples.
        Returns:
        A Pair of a GeneralDataset, where the labels and features are Strings and a List of the input examples
      • readTestExamples

        public Pair<GeneralDataset<String,​String>,​List<String[]>> readTestExamples​(String filename)
        Read a data set from a file at test time, and return it.
        Parameters:
        filename - The file to read the examples from.
        Returns:
        A Pair. The first item of the pair is the featurized data set, ready for passing to the classifier. The second item of the pair is simply each line of the file split into tab-separated columns. This is at present necessary for the built-in evaluation, which uses the gold class from here, and may also be helpful when wanting to print extra output about the classification process.
      • getClassifier

        public static ColumnDataClassifier getClassifier​(String path)
        Return a new ColumnDataClassifier object based on a serialized object. The serialized object stores both a Flags[] that specifies feature extraction and other properties of the classifier and a Classifier object.
        Parameters:
        path - A classpath resource, URL, or file system path
        Returns:
        The ColumnDataClassifier
      • main

        public static void main​(String[] args)
                         throws IOException
        Runs the ColumnDataClassifier from the command-line. Usage: java edu.stanford.nlp.classify.ColumnDataClassifier -trainFile trainFile -testFile testFile [-useNGrams|-useString|-sigma sigma|...] or java ColumnDataClassifier -prop propFile
        Parameters:
        args - Command line arguments, as described in the class documentation
        Throws:
        IOException - If IO problems
      • serializeClassifier

        public void serializeClassifier​(String serializeTo)
                                 throws IOException
        Serialize a classifier to a file. This writes to the file both a LinearClassifier and the Flags[] object from a ColumnDataClassifier. The latter captures all the information about how the ColumnDataClassifier is extracting features from data items for the classifier.
        Parameters:
        serializeTo - Filename to serialize the classifier to
        Throws:
        IOException - If any IO error
      • serializeClassifier

        public void serializeClassifier​(ObjectOutputStream oos)
                                 throws IOException
        Serialize a classifier to an ObjectOutputStream. This writes to the file both a LinearClassifier and the Flags[] object from a ColumnDataClassifier. The latter captures all the information about how the ColumnDataClassifier is extracting features from data items for the classifier.
        Parameters:
        oos - ObjectOutputStream to serialize the classifier to
        Throws:
        IOException - If any IO error
      • testClassifier

        public Pair<Double,​Double> testClassifier​(String testFile)
        Test and evaluate classifier on examples available in a file (or URL, classpath resource, etc.)
        Parameters:
        testFile - The path, classpath resource or URL to load TSV data from
        Returns:
        A Pair consisting of the accuracy (micro-averaged F1) and macro-averaged F1 for the dataset
      • crossValidate

        public Pair<Double,​Double> crossValidate​(GeneralDataset<String,​String> dataset,
                                                       List<String[]> lineInfos)
        Run cross-validation on a dataset, and return accuracy and macro-F1 scores. The number of folds is given by the crossValidationFolds property.
        Parameters:
        dataset - The dataset of examples to cross-validate on.
        lineInfos - The String form of the items in the dataset. (Must be present.)
        Returns:
        Accuracy and macro F1