Class Linear


  • public class Linear
    extends java.lang.Object
    Copyright (c) 2007-2014 The LIBLINEAR Project. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither name of copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    Java port of liblinear 1.51

    The usage should be pretty similar to the C version of liblinear.

    Please consider reading the README file of liblinear.

    The port was done by Benedikt Waldvogel (mail at bwaldvogel.de)

    Version:
    1.51
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  Linear.GroupClassesReturn
      used as complex return type
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.io.PrintStream DEBUG_OUTPUT  
      (package private) static java.util.Locale DEFAULT_LOCALE  
      private static long DEFAULT_RANDOM_SEED  
      static java.nio.charset.Charset FILE_CHARSET  
      (package private) static java.lang.String NL
      platform-independent new-line string
      private static java.lang.Object OUTPUT_MUTEX  
      (package private) static java.util.Random random  
    • Constructor Summary

      Constructors 
      Constructor Description
      Linear()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double atof​(java.lang.String s)  
      static int atoi​(java.lang.String s)  
      (package private) static void closeQuietly​(java.io.Closeable c)  
      static double[] copyOf​(double[] original, int newLength)
      Java5 'backport' of Arrays.copyOf
      static int[] copyOf​(int[] original, int newLength)
      Java5 'backport' of Arrays.copyOf
      static void crossValidation​(Problem prob, Parameter param, int nr_fold, int[] target)  
      static void disableDebugOutput()  
      static void enableDebugOutput()  
      private static int GETI​(byte[] y, int i)  
      private static Linear.GroupClassesReturn groupClasses​(Problem prob, int[] perm)  
      (package private) static void info​(java.lang.String message)  
      (package private) static void info​(java.lang.String format, java.lang.Object... args)  
      static Model loadModel​(java.io.File modelFile)
      Loads the model from the file with ISO-8859-1 charset.
      static Model loadModel​(java.io.Reader inputReader)
      Loads the model from inputReader.
      static int predict​(Model model, FeatureNode[] x)  
      static int predictProbability​(Model model, FeatureNode[] x, double[] prob_estimates)  
      static int predictValues​(Model model, FeatureNode[] x, double[] dec_values)  
      (package private) static void printf​(java.util.Formatter formatter, java.lang.String format, java.lang.Object... args)  
      static void resetRandom()
      resets the PRNG this is i.a. needed for regression testing (eg. the Weka wrapper)
      static void saveModel​(java.io.File modelFile, Model model)
      Writes the model to the file with ISO-8859-1 charset.
      static void saveModel​(java.io.Writer modelOutput, Model model)
      Writes the model to the modelOutput.
      static void setDebugOutput​(java.io.PrintStream debugOutput)  
      private static void solve_l1r_l2_svc​(Problem prob_col, double[] w, double eps, double Cp, double Cn)
      A coordinate descent algorithm for L1-regularized L2-loss support vector classification
      private static void solve_l1r_lr​(Problem prob_col, double[] w, double eps, double Cp, double Cn)
      A coordinate descent algorithm for L1-regularized logistic regression problems
      private static void solve_l2r_l1l2_svc​(Problem prob, double[] w, double eps, double Cp, double Cn, SolverType solver_type)
      A coordinate descent algorithm for L1-loss and L2-loss SVM dual problems
      (package private) static void swap​(double[] array, int idxA, int idxB)  
      (package private) static void swap​(int[] array, int idxA, int idxB)  
      (package private) static void swap​(IntArrayPointer array, int idxA, int idxB)  
      static Model train​(Problem prob, Parameter param)  
      private static void train_one​(Problem prob, Parameter param, double[] w, double Cp, double Cn)  
      (package private) static Problem transpose​(Problem prob)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • FILE_CHARSET

        public static final java.nio.charset.Charset FILE_CHARSET
      • DEFAULT_LOCALE

        static final java.util.Locale DEFAULT_LOCALE
      • OUTPUT_MUTEX

        private static java.lang.Object OUTPUT_MUTEX
      • DEBUG_OUTPUT

        private static java.io.PrintStream DEBUG_OUTPUT
      • NL

        static final java.lang.String NL
        platform-independent new-line string
      • random

        static java.util.Random random
    • Constructor Detail

      • Linear

        public Linear()
    • Method Detail

      • crossValidation

        public static void crossValidation​(Problem prob,
                                           Parameter param,
                                           int nr_fold,
                                           int[] target)
        Parameters:
        target - predicted classes
      • info

        static void info​(java.lang.String message)
      • info

        static void info​(java.lang.String format,
                         java.lang.Object... args)
      • atof

        public static double atof​(java.lang.String s)
        Parameters:
        s - the string to parse for the double value
        Throws:
        java.lang.IllegalArgumentException - if s is empty or represents NaN or Infinity
        java.lang.NumberFormatException - see Double.parseDouble(String)
      • atoi

        public static int atoi​(java.lang.String s)
                        throws java.lang.NumberFormatException
        Parameters:
        s - the string to parse for the integer value
        Throws:
        java.lang.IllegalArgumentException - if s is empty
        java.lang.NumberFormatException - see Integer.parseInt(String)
      • copyOf

        public static double[] copyOf​(double[] original,
                                      int newLength)
        Java5 'backport' of Arrays.copyOf
      • copyOf

        public static int[] copyOf​(int[] original,
                                   int newLength)
        Java5 'backport' of Arrays.copyOf
      • loadModel

        public static Model loadModel​(java.io.Reader inputReader)
                               throws java.io.IOException
        Loads the model from inputReader. It uses Locale.ENGLISH for number formatting.

        Note: The inputReader is closed after reading or in case of an exception.

        Throws:
        java.io.IOException
      • loadModel

        public static Model loadModel​(java.io.File modelFile)
                               throws java.io.IOException
        Loads the model from the file with ISO-8859-1 charset. It uses Locale.ENGLISH for number formatting.
        Throws:
        java.io.IOException
      • closeQuietly

        static void closeQuietly​(java.io.Closeable c)
      • predictProbability

        public static int predictProbability​(Model model,
                                             FeatureNode[] x,
                                             double[] prob_estimates)
      • predictValues

        public static int predictValues​(Model model,
                                        FeatureNode[] x,
                                        double[] dec_values)
      • printf

        static void printf​(java.util.Formatter formatter,
                           java.lang.String format,
                           java.lang.Object... args)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • saveModel

        public static void saveModel​(java.io.Writer modelOutput,
                                     Model model)
                              throws java.io.IOException
        Writes the model to the modelOutput. It uses Locale.ENGLISH for number formatting.

        Note: The modelOutput is closed after reading or in case of an exception.

        Throws:
        java.io.IOException
      • saveModel

        public static void saveModel​(java.io.File modelFile,
                                     Model model)
                              throws java.io.IOException
        Writes the model to the file with ISO-8859-1 charset. It uses Locale.ENGLISH for number formatting.
        Throws:
        java.io.IOException
      • GETI

        private static int GETI​(byte[] y,
                                int i)
      • solve_l2r_l1l2_svc

        private static void solve_l2r_l1l2_svc​(Problem prob,
                                               double[] w,
                                               double eps,
                                               double Cp,
                                               double Cn,
                                               SolverType solver_type)
        A coordinate descent algorithm for L1-loss and L2-loss SVM dual problems
          min_\alpha  0.5(\alpha^T (Q + D)\alpha) - e^T \alpha,
            s.t.      0 
      • solve_l1r_l2_svc

        private static void solve_l1r_l2_svc​(Problem prob_col,
                                             double[] w,
                                             double eps,
                                             double Cp,
                                             double Cn)
        A coordinate descent algorithm for L1-regularized L2-loss support vector classification
          min_w \sum |wj| + C \sum max(0, 1-yi w^T xi)^2,
        
         Given:
         x, y, Cp, Cn
         eps is the stopping tolerance
        
         solution will be put in w
        
      • solve_l1r_lr

        private static void solve_l1r_lr​(Problem prob_col,
                                         double[] w,
                                         double eps,
                                         double Cp,
                                         double Cn)
        A coordinate descent algorithm for L1-regularized logistic regression problems
          min_w \sum |wj| + C \sum log(1+exp(-yi w^T xi)),
        
         Given:
         x, y, Cp, Cn
         eps is the stopping tolerance
        
         solution will be put in w
        
      • swap

        static void swap​(double[] array,
                         int idxA,
                         int idxB)
      • swap

        static void swap​(int[] array,
                         int idxA,
                         int idxB)
      • train

        public static Model train​(Problem prob,
                                  Parameter param)
        Throws:
        java.lang.IllegalArgumentException - if the feature nodes of prob are not sorted in ascending order
      • train_one

        private static void train_one​(Problem prob,
                                      Parameter param,
                                      double[] w,
                                      double Cp,
                                      double Cn)
      • disableDebugOutput

        public static void disableDebugOutput()
      • enableDebugOutput

        public static void enableDebugOutput()
      • setDebugOutput

        public static void setDebugOutput​(java.io.PrintStream debugOutput)
      • resetRandom

        public static void resetRandom()
        resets the PRNG this is i.a. needed for regression testing (eg. the Weka wrapper)