Package de.bwaldvogel.liblinear
Class Linear
- java.lang.Object
-
- de.bwaldvogel.liblinear.Linear
-
public class Linear extends java.lang.ObjectJava port of liblinear
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:
- 2.44
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classLinear.GroupClassesReturnused as complex return type
-
Field Summary
Fields Modifier and Type Field Description private static java.io.PrintStreamDEBUG_OUTPUTprivate static java.util.LocaleDEFAULT_LOCALE(package private) static java.nio.charset.CharsetFILE_CHARSETprivate static java.lang.ObjectOUTPUT_MUTEX(package private) static intVERSION
-
Constructor Summary
Constructors Constructor Description Linear()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description (package private) static doubleatof(java.lang.String s)(package private) static intatoi(java.lang.String s)private static doublecalc_max_p(Problem prob)private static doublecalc_start_C(Problem prob, Parameter param)private static voidcheckProblemSize(int n, int nr_class)verify the size and throw an exception early if the problem is too largestatic voidcrossValidation(Problem prob, Parameter param, int nr_fold, double[] target)static voiddisableDebugOutput()static voidenableDebugOutput()static ParameterCSearchResultfind_parameter_C(Problem prob, Parameter param_tmp, double start_C, double max_C, int[] fold_start, int[] perm, Problem[] subprob, int nr_fold)static ParameterSearchResultfindParameters(Problem prob, Parameter param, int nr_fold, double start_C, double start_p)private static intGETI(byte[] y, int i)private static intGETI_SVR(int i)static intgetVersion()private static Linear.GroupClassesReturngroupClasses(Problem prob, int[] perm)(package private) static voidinfo(java.lang.String message)(package private) static voidinfo(java.lang.String format, java.lang.Object... args)static ModelloadModel(java.io.File modelFile)Deprecated.useloadModel(Path)insteadstatic ModelloadModel(java.io.Reader inputReader)Loads the model from inputReader.static ModelloadModel(java.nio.file.Path modelPath)Loads the model from the file with ISO-8859-1 charset.static doublepredict(Model model, Feature[] x)static doublepredictProbability(Model model, Feature[] x, double[] prob_estimates)static doublepredictValues(Model model, Feature[] x, double[] dec_values)(package private) static voidprintf(java.util.Formatter formatter, java.lang.String format, java.lang.Object... args)static voidresetRandom()Deprecated.UseParameter.setRandom(Random)insteadstatic voidsaveModel(java.io.File modelFile, Model model)Deprecated.usesaveModel(Path, Model)insteadstatic voidsaveModel(java.io.Writer modelOutput, Model model)Writes the model to the modelOutput.static voidsaveModel(java.nio.file.Path modelPath, Model model)Writes the model to the file with ISO-8859-1 charset.static voidsetDebugOutput(java.io.PrintStream debugOutput)private static intsolve_l1r_l2_svc(Problem prob_col, Parameter param, double[] w, double Cp, double Cn, double eps, int max_iter)A coordinate descent algorithm for L1-regularized L2-loss support vector classificationprivate static intsolve_l1r_lr(Problem prob_col, Parameter param, double[] w, double Cp, double Cn, double eps, int max_iter)A coordinate descent algorithm for L1-regularized logistic regression problemsprivate static intsolve_l2r_l1l2_svc(Problem prob, Parameter param, double[] w, double Cp, double Cn, int max_iter)A coordinate descent algorithm for L1-loss and L2-loss SVM dual problemsprivate static intsolve_l2r_l1l2_svr(Problem prob, Parameter param, double[] w, int max_iter)A coordinate descent algorithm for L1-loss and L2-loss epsilon-SVR dual problem min_\beta 0.5\beta^T (Q + diag(lambda)) \beta - p \sum_{i=1}^l|\beta_i| + \sum_{i=1}^l yi\beta_i, s.t.private static intsolve_l2r_lr_dual(Problem prob, Parameter param, double[] w, double Cp, double Cn, int max_iter)A coordinate descent algorithm for the dual of L2-regularized logistic regression problems(package private) static intsolve_oneclass_svm(Problem prob, Parameter param, double[] w, MutableDouble rho, int max_iter)(package private) static voidswap(double[] array, int idxA, int idxB)(package private) static voidswap(int[] array, int idxA, int idxB)(package private) static voidswap(Feature[] array, int idxA, int idxB)(package private) static voidswap(IntArrayPointer array, int idxA, int idxB)static Modeltrain(Problem prob, Parameter param)private static voidtrain_one(Problem prob, Parameter param, double[] w, double Cp, double Cn)(package private) static Problemtranspose(Problem prob)
-
-
-
Field Detail
-
VERSION
static final int VERSION
- See Also:
- Constant Field Values
-
FILE_CHARSET
static final java.nio.charset.Charset FILE_CHARSET
-
DEFAULT_LOCALE
private static final java.util.Locale DEFAULT_LOCALE
-
OUTPUT_MUTEX
private static final java.lang.Object OUTPUT_MUTEX
-
DEBUG_OUTPUT
private static java.io.PrintStream DEBUG_OUTPUT
-
-
Method Detail
-
crossValidation
public static void crossValidation(Problem prob, Parameter param, int nr_fold, double[] target)
- Parameters:
target- predicted classes
-
findParameters
public static ParameterSearchResult findParameters(Problem prob, Parameter param, int nr_fold, double start_C, double start_p)
-
groupClasses
private static Linear.GroupClassesReturn groupClasses(Problem prob, int[] perm)
-
info
static void info(java.lang.String message)
-
info
static void info(java.lang.String format, java.lang.Object... args)
-
atof
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 Infinityjava.lang.NumberFormatException- seeDouble.parseDouble(String)
-
atoi
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 emptyjava.lang.NumberFormatException- seeInteger.parseInt(String)
-
loadModel
public static Model loadModel(java.io.Reader inputReader) throws java.io.IOException
Loads the model from inputReader. It usesLocale.ENGLISHfor number formatting.Note: The inputReader is NOT 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
Deprecated.useloadModel(Path)insteadLoads the model from the file with ISO-8859-1 charset. It usesLocale.ENGLISHfor number formatting.- Throws:
java.io.IOException
-
loadModel
public static Model loadModel(java.nio.file.Path modelPath) throws java.io.IOException
Loads the model from the file with ISO-8859-1 charset. It usesLocale.ENGLISHfor number formatting.- Throws:
java.io.IOException
-
predictProbability
public static double predictProbability(Model model, Feature[] x, double[] prob_estimates) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException- if model is not probabilistic (seeModel.isProbabilityModel())
-
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.IOExceptionWrites the model to the modelOutput. It usesLocale.ENGLISHfor 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.IOExceptionDeprecated.usesaveModel(Path, Model)insteadWrites the model to the file with ISO-8859-1 charset. It usesLocale.ENGLISHfor number formatting.- Throws:
java.io.IOException
-
saveModel
public static void saveModel(java.nio.file.Path modelPath, Model model) throws java.io.IOExceptionWrites the model to the file with ISO-8859-1 charset. It usesLocale.ENGLISHfor number formatting.- Throws:
java.io.IOException
-
GETI
private static int GETI(byte[] y, int i)
-
solve_l2r_l1l2_svc
private static int solve_l2r_l1l2_svc(Problem prob, Parameter param, double[] w, double Cp, double Cn, int max_iter)
A coordinate descent algorithm for L1-loss and L2-loss SVM dual problemsmin_\alpha 0.5(\alpha^T (Q + D)\alpha) - e^T \alpha, s.t. 0 <= \alpha_i <= upper_bound_i, where Qij = yi yj xi^T xj and D is a diagonal matrix In L1-SVM case: upper_bound_i = Cp if y_i = 1 upper_bound_i = Cn if y_i = -1 D_ii = 0 In L2-SVM case: upper_bound_i = INF D_ii = 1/(2*Cp) if y_i = 1 D_ii = 1/(2*Cn) if y_i = -1 Given: x, y, Cp, Cn eps is the stopping tolerance solution will be put in w this function returns the number of iterations See Algorithm 3 of Hsieh et al., ICML 2008
-
GETI_SVR
private static int GETI_SVR(int i)
-
solve_l2r_l1l2_svr
private static int solve_l2r_l1l2_svr(Problem prob, Parameter param, double[] w, int max_iter)
A coordinate descent algorithm for L1-loss and L2-loss epsilon-SVR dual problem min_\beta 0.5\beta^T (Q + diag(lambda)) \beta - p \sum_{i=1}^l|\beta_i| + \sum_{i=1}^l yi\beta_i, s.t. -upper_bound_i <= \beta_i <= upper_bound_i, where Qij = xi^T xj and D is a diagonal matrix In L1-SVM case: upper_bound_i = C lambda_i = 0 In L2-SVM case: upper_bound_i = INF lambda_i = 1/(2*C) Given: x, y, p, C eps is the stopping tolerance solution will be put in w this function returns the number of iterations See Algorithm 4 of Ho and Lin, 2012
-
solve_l2r_lr_dual
private static int solve_l2r_lr_dual(Problem prob, Parameter param, double[] w, double Cp, double Cn, int max_iter)
A coordinate descent algorithm for the dual of L2-regularized logistic regression problemsmin_\alpha 0.5(\alpha^T Q \alpha) + \sum \alpha_i log (\alpha_i) + (upper_bound_i - \alpha_i) log (upper_bound_i - \alpha_i) , s.t. 0 <= \alpha_i <= upper_bound_i, where Qij = yi yj xi^T xj and upper_bound_i = Cp if y_i = 1 upper_bound_i = Cn if y_i = -1 Given: x, y, Cp, Cn eps is the stopping tolerance solution will be put in w this function returns the number of iterations See Algorithm 5 of Yu et al., MLJ 2010- Since:
- 1.7
-
solve_l1r_l2_svc
private static int solve_l1r_l2_svc(Problem prob_col, Parameter param, double[] w, double Cp, double Cn, double eps, int max_iter)
A coordinate descent algorithm for L1-regularized L2-loss support vector classificationmin_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 this function returns the number of iterations See Yuan et al. (2010) and appendix of LIBLINEAR paper, Fan et al. (2008) To not regularize the bias (i.e., regularize_bias = 0), a constant feature = 1 must have been added to the original data. (see -B and -R option)
- Since:
- 1.5
-
solve_l1r_lr
private static int solve_l1r_lr(Problem prob_col, Parameter param, double[] w, double Cp, double Cn, double eps, int max_iter)
A coordinate descent algorithm for L1-regularized logistic regression problemsmin_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 this function returns the number of iterations See Yuan et al. (2011) and appendix of LIBLINEAR paper, Fan et al. (2008) To not regularize the bias (i.e., regularize_bias = 0), a constant feature = 1 must have been added to the original data. (see -B and -R option)
- Since:
- 1.5
-
solve_oneclass_svm
static int solve_oneclass_svm(Problem prob, Parameter param, double[] w, MutableDouble rho, int max_iter)
-
swap
static void swap(double[] array, int idxA, int idxB)
-
swap
static void swap(int[] array, int idxA, int idxB)
-
swap
static void swap(IntArrayPointer array, int idxA, int idxB)
-
swap
static void swap(Feature[] 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
-
checkProblemSize
private static void checkProblemSize(int n, int nr_class)verify the size and throw an exception early if the problem is too large
-
train_one
private static void train_one(Problem prob, Parameter param, double[] w, double Cp, double Cn)
-
calc_max_p
private static double calc_max_p(Problem prob)
-
find_parameter_C
public static ParameterCSearchResult find_parameter_C(Problem prob, Parameter param_tmp, double start_C, double max_C, int[] fold_start, int[] perm, Problem[] subprob, int nr_fold)
-
disableDebugOutput
public static void disableDebugOutput()
-
enableDebugOutput
public static void enableDebugOutput()
-
setDebugOutput
public static void setDebugOutput(java.io.PrintStream debugOutput)
-
getVersion
public static int getVersion()
-
resetRandom
public static void resetRandom()
Deprecated.UseParameter.setRandom(Random)insteadresets the PRNG
-
-