### Complete Configuration Example for Liblinear-Java Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/configuration.md A comprehensive example demonstrating various configuration options for training a model, including data loading, parameter settings for unbalanced data, optimization, reproducibility, and bias regularization. ```java // Load training data Problem problem = Problem.readFromFile(Paths.get("data.txt"), 1.0); // Create parameter with base settings Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); // Configure for unbalanced data param.setWeights( new double[]{1.0, 2.5}, // Penalize minority class more new int[]{0, 1} ); // Configure optimization param.setMaxIters(2000); param.setEps(0.001); // Reproducible training param.setRandom(new Random(12345)); // Don't regularize bias param.setRegularizeBias(false); // Train model try { Model model = Linear.train(problem, param); System.out.println("Training completed successfully"); } catch (IllegalArgumentException e) { System.err.println("Configuration error: " + e.getMessage()); } ``` -------------------------------- ### Warm Start Training in liblinear-java Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/README.md Enabling warm start for training in liblinear-java by initializing the model with weights from a previous training run. This can speed up convergence for similar problems. ```java Model previousModel = Linear.loadModel(new File("path/to/previous_model.txt")); Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); param.setInitSol(previousModel.getSol()); Model newModel = Linear.train(problem, param); ``` -------------------------------- ### FeatureNode Instantiation Example Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-feature.md Demonstrates how to create FeatureNode objects with different indices and values. ```java Feature f1 = new FeatureNode(1, 0.5); // Feature 1, value 0.5 Feature f2 = new FeatureNode(3, -1.2); // Feature 3, value -1.2 Feature f3 = new FeatureNode(10, 0.0); // Feature 10, value 0.0 ``` -------------------------------- ### Example Liblinear File Format Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-problem.md Shows the format of a data file for liblinear, where each line represents an instance with its class label followed by sparse feature-value pairs. Feature indices start from 1. ```text 1 1:0.5 3:1.2 5:-0.8 2 1:-0.3 2:0.9 4:1.5 5:0.2 1 2:0.5 3:0.8 3 1:0.1 2:0.2 3:0.3 ``` -------------------------------- ### LIBSVM Data Format Example Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/quick-start.md Illustrates the LIBSVM file format for training data, which uses a sparse representation. Labels are integers for classification or real numbers for regression. Feature indices start from 1 and must be in ascending order. ```text 1 2:0.5 3:0.1 2 1:0.3 3:0.8 1 1:0.2 2:0.9 ``` -------------------------------- ### liblinear-java Project File Organization Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/DOCUMENTATION_MANIFEST.md This snippet shows the directory structure for the liblinear-java project's documentation files. It includes the main README, this manifest, quick start guides, and specific API reference files. ```text /workspace/home/output/ ├── README.md (Overview & navigation) ├── DOCUMENTATION_MANIFEST.md (This file) ├── quick-start.md (Essential patterns) ├── api-linear.md (Linear class) ├── api-parameter.md (Parameter class) ├── api-problem.md (Problem class) ├── api-model.md (Model class) ├── api-feature.md (Feature & FeatureNode) ├── types.md (Type definitions) ├── configuration.md (Tuning guide) └── errors.md (Exception handling) ``` -------------------------------- ### Complete Parameter Configuration Example Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Demonstrates how to create, configure, and use a Parameter object for training a model with Liblinear-Java. Includes setting basic options, handling unbalanced data, ensuring reproducibility, and initiating training. ```java // Create parameter object Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); // Configure basic options param.setMaxIters(2000); // Handle unbalanced data param.setWeights(new double[]{1.0, 2.5}, new int[]{1, 2}); // For reproducibility param.setRandom(new Random(12345)); // Use for training Model model = Linear.train(problem, param); ``` -------------------------------- ### Enable Warm Start for Faster Convergence Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/configuration.md For sequential training on similar datasets, set initial weights using `param.setInitSol()` to speed up convergence. Supported solvers include L2R_LR, L2R_L2LOSS_SVC, and L2R_L2LOSS_SVR. ```java // First training Model model1 = Linear.train(problem1, param); double[] initialWeights = model1.getFeatureWeights(); // Warm start for similar problem param.setInitSol(initialWeights); Model model2 = Linear.train(problem2, param); // Faster convergence ``` -------------------------------- ### Get Initial Solution Array Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Returns a copy of the initial solution array, if provided. ```java public double[] getInitSol() ``` -------------------------------- ### FeatureNode Usage Example Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/types.md Demonstrates how to create and manipulate a FeatureNode, which implements the Feature interface. This shows instantiation and modification of feature values. ```java Feature f = new FeatureNode(1, 0.5); int idx = f.getIndex(); double val = f.getValue(); f.setValue(0.8); ``` -------------------------------- ### Predict with Sparse Features Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-feature.md Demonstrates how to make a prediction using a trained model and a sparse feature vector for a test instance. Includes an example of retrieving probability estimates if the model supports it. ```java // Test instance with sparse features Feature[] testInstance = new Feature[]{ new FeatureNode(1, 0.3), new FeatureNode(4, 0.9), new FeatureNode(8, 0.5) }; // Predict double prediction = Linear.predict(model, testInstance); System.out.println("Predicted class: " + prediction); // Get probability estimates (if supported) if (model.isProbabilityModel()) { double[] probs = new double[model.getNrClass()]; Linear.predictProbability(model, testInstance, probs); System.out.println("Probabilities: " + Arrays.toString(probs)); } ``` -------------------------------- ### Parameter Selection with Custom Settings Source: https://github.com/bwaldvogel/liblinear-java/blob/main/README.md Perform parameter selection for C with custom settings. Users can specify alternative solvers, number of CV folds, and the starting C value for the search range. The -e option sets a stricter stopping tolerance. ```bash train -C -s 0 -v 3 -c 0.5 -e 0.0001 data_file ``` -------------------------------- ### Create Training Data Programmatically Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/quick-start.md Instantiate a Problem object and populate it with training instances and features. This is useful when data is not available in a file format. Ensure features are indexed starting from 1 and are in ascending order. ```java Problem problem = new Problem(); problem.l = 3; // 3 instances problem.n = 3; // 3 features problem.y = new double[]{1, 2, 1}; // Labels problem.x = new Feature[3][]; // Instance 1: label=1, features (1, 0.5), (2, 1.0) problem.x[0] = new Feature[]{ new FeatureNode(1, 0.5), new FeatureNode(2, 1.0) }; // Instance 2: label=2, features (1, -0.3), (3, 0.8) problem.x[1] = new Feature[]{ new FeatureNode(1, -0.3), new FeatureNode(3, 0.8) }; // Instance 3: label=1, feature (2, 0.9) problem.x[2] = new Feature[]{ new FeatureNode(2, 0.9) }; // Train Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); Model model = Linear.train(problem, param); ``` -------------------------------- ### Get Epsilon (Convergence Tolerance) Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Retrieves the epsilon value, which is the tolerance for convergence. ```java public double getEps() ``` -------------------------------- ### FeatureNode setValue() Method Example Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-feature.md Demonstrates how to update the value of an existing FeatureNode and verify the change using getValue(). ```java FeatureNode f = new FeatureNode(1, 0.5); f.setValue(1.5); System.out.println(f.getValue()); // 1.5 ``` -------------------------------- ### Set Initial Solution for Warm Start Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Sets an initial solution vector for warm-start training, which can speed up convergence for similar problems. Supported only by specific primal solvers. The array size must match the weight vector dimension. ```java public void setInitSol(double[] init_sol) ``` ```java // First training Model model1 = Linear.train(problem1, param); // Warm start for similar problem double[] initialWeights = model1.getFeatureWeights(); param.setInitSol(initialWeights); Model model2 = Linear.train(problem2, param); // Faster convergence ``` -------------------------------- ### Find Optimal Parameters via Cross-Validation Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-linear.md Automatically selects the best C and p parameters for supported solvers (L2R_LR, L2R_L2LOSS_SVC, L2R_L2LOSS_SVR) using cross-validation. Provides a starting point for hyperparameter tuning. ```java public static ParameterSearchResult findParameters(Problem prob, Parameter param, int nr_fold, double start_C, double start_p) ``` ```java Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); ParameterSearchResult result = Linear.findParameters(problem, param, 5, -1, -1); System.out.println("Best C: " + result.getBestC()); System.out.println("Best CV accuracy: " + (100.0 * result.getBestScore()) + "%"); // Retrain with best parameters param.setC(result.getBestC()); Model model = Linear.train(problem, param); ``` -------------------------------- ### Handle Unbalanced Data with Class Weights Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/quick-start.md Address datasets with imbalanced class distributions by assigning different weights to classes. This example penalizes the minority class more heavily during training. ```java Problem problem = Problem.readFromFile(Paths.get("train.txt"), 1.0); // Create parameter with class weights Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); // Penalize minority class (class 2) 3x more than class 1 param.setWeights( new double[]{1.0, 3.0}, // Weights new int[]{1, 2} // Class labels ); Model model = Linear.train(problem, param); ``` -------------------------------- ### Parameter Search Method Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/README.md Searches for optimal parameters using cross-validation. It requires the problem data, initial parameters, number of folds, and starting values for C and p. ```java ParameterSearchResult Linear.findParameters(Problem prob, Parameter param, int nr_fold, double start_C, double start_p) ``` -------------------------------- ### Manual Feature Normalization Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-problem.md Provides an example of manually normalizing features to the range [-1, 1]. This preprocessing step can improve model performance by scaling feature values. ```java // Manual feature normalization example Problem problem = Problem.readFromFile(Paths.get("data.txt"), 1.0); // Find min/max for each feature double[] min = new double[problem.n]; double[] max = new double[problem.n]; Arrays.fill(min, Double.POSITIVE_INFINITY); Arrays.fill(max, Double.NEGATIVE_INFINITY); for (int i = 0; i < problem.l; i++) { for (Feature f : problem.x[i]) { int idx = f.getIndex() - 1; min[idx] = Math.min(min[idx], f.getValue()); max[idx] = Math.max(max[idx], f.getValue()); } } // Scale features to [-1, 1] for (int i = 0; i < problem.l; i++) { for (Feature f : problem.x[i]) { int idx = f.getIndex() - 1; double range = max[idx] - min[idx]; if (range > 0) { double normalized = 2.0 * (f.getValue() - min[idx]) / range - 1.0; f.setValue(normalized); } } } ``` -------------------------------- ### Set Maximum Iterations for Training Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/configuration.md Configures the maximum number of optimization iterations for model training. This example increases the limit to 2000, which can be useful for large datasets or strict convergence criteria. ```java Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); param.setMaxIters(2000); // More iterations Model model = Linear.train(problem, param); ``` -------------------------------- ### Get Decision Function Coefficient Source: https://github.com/bwaldvogel/liblinear-java/blob/main/README.md Retrieves the coefficient for a specific feature and class. Feature indices start from 1, and label indices from 0. Returns zero for invalid indices. ```c double get_decfun_coef(const struct model *model_, int feat_idx, int label_idx); ``` -------------------------------- ### getInitSol() Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Returns a copy of the initial solution array. ```APIDOC ## getInitSol() ### Description Returns a copy of the initial solution array, which can be used to start the optimization process. ### Method GET ### Endpoint N/A (Java method) ### Returns - **double[]** - A copy of the initial solution array. ``` -------------------------------- ### Typical LIBLINEAR-Java Workflow Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/README.md This snippet outlines the standard steps for using the LIBLINEAR-Java library, from data loading to prediction. ```text 1. Load training data (Problem.readFromFile) 2. Configure parameters (new Parameter + setWeights/setP/etc) 3. [Optional] Tune with findParameters or crossValidation 4. Train model (Linear.train) 5. Save model (Linear.saveModel) 6. Load model for deployment (Linear.loadModel) 7. Predict on test data (Linear.predict/predictProbability) ``` -------------------------------- ### Get Solver Type Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Retrieves the type of solver used by the parameters. ```java public SolverType getSolverType() ``` -------------------------------- ### Get Number of Weights Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Returns the count of weight assignments present in the parameters. ```java public int getNumWeights() ``` -------------------------------- ### Build Training Data and Train Model Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-feature.md Illustrates the process of constructing a `Problem` object with labels and sparse feature instances, then training a model using `Linear.train`. Requires defining `Problem` parameters and `Parameter` settings. ```java Problem problem = new Problem(); problem.l = 4; problem.n = 10; problem.bias = -1; problem.y = new double[]{1, 2, 1, 2}; // Create sparse instances problem.x = new Feature[4][]; // Instance 1: features 2, 5, 7 problem.x[0] = new Feature[]{ new FeatureNode(2, 0.5), new FeatureNode(5, 1.2), new FeatureNode(7, -0.3) }; // Instance 2: features 1, 3, 6, 9 problem.x[1] = new Feature[]{ new FeatureNode(1, 0.1), new FeatureNode(3, 0.8), new FeatureNode(6, 1.5), new FeatureNode(9, 0.2) }; // ... more instances // Train model Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); Model model = Linear.train(problem, param); ``` -------------------------------- ### setInitSol Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Provides an initial solution vector for warm-start training. This can significantly speed up convergence when training on a problem similar to one previously solved. ```APIDOC ## setInitSol(double[] init_sol) ### Description Sets an initial solution vector, enabling warm-start training. This is beneficial for accelerating convergence when the current training problem is similar to a previously solved one. ### Method ```java public void setInitSol(double[] init_sol) ``` ### Parameters #### Path Parameters - **init_sol** (double[]) - Required - The initial solution vector. ### Supported Solvers L2R_LR, L2R_L2LOSS_SVC, L2R_L2LOSS_SVR ### Array Size Must match the weight vector dimension (nr_feature or nr_feature * nr_class). ### Use Case Speed up training when starting from a previous solution. ### Example ```java // First training Model model1 = Linear.train(problem1, param); // Warm start for similar problem double[] initialWeights = model1.getFeatureWeights(); param.setInitSol(initialWeights); Model model2 = Linear.train(problem2, param); // Faster convergence ``` ``` -------------------------------- ### Get Maximum Iterations Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Retrieves the maximum number of iterations allowed for the solver. ```java public int getMaxIters() ``` -------------------------------- ### Configure Training Parameters Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/quick-start.md Create a Parameter object to configure the training process, specifying the solver type, regularization parameter C, and stopping tolerance epsilon. ```java // Create parameter object Parameter param = new Parameter( SolverType.L2R_LR, // Logistic regression 1.0, // C (regularization) 0.01 // eps (stopping tolerance) ); ``` -------------------------------- ### Get Regularization Parameter C Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Retrieves the value of the C regularization parameter. ```java public double getC() ``` -------------------------------- ### Parameter Constructor with All Main Options Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md This constructor provides an option to set all the main parameters for the Parameter object, including solver type, regularization cost, stopping tolerance, maximum iterations, and epsilon for SVR. ```java public Parameter(SolverType solverType, double C, double eps, int max_iters, double p) ``` -------------------------------- ### LIBLINEAR Training Command-Line Usage Source: https://github.com/bwaldvogel/liblinear-java/blob/main/README.md This snippet shows the general command-line syntax for training a model using LIBLINEAR. It outlines the available options for setting the solver type, regularization parameters, and cross-validation modes. ```bash Usage: train [options] training_set_file [model_file] options: -s type : set type of solver (default 1) for multi-class classification 0 -- L2-regularized logistic regression (primal) 1 -- L2-regularized L2-loss support vector classification (dual) 2 -- L2-regularized L2-loss support vector classification (primal) 3 -- L2-regularized L1-loss support vector classification (dual) 4 -- support vector classification by Crammer and Singer 5 -- L1-regularized L2-loss support vector classification 6 -- L1-regularized logistic regression 7 -- L2-regularized logistic regression (dual) for regression 11 -- L2-regularized L2-loss support vector regression (primal) 12 -- L2-regularized L2-loss support vector regression (dual) 13 -- L2-regularized L1-loss support vector regression (dual) for outlier detection 21 -- one-class support vector machine (dual) -c cost : set the parameter C (default 1) -p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1) -n nu : set the parameter nu of one-class SVM (default 0.5) -e epsilon : set tolerance of termination criterion -s 0 and 2 |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2, where f is the primal function and pos/neg are # of positive/negative data (default 0.01) -s 11 |f'(w)|_2 <= eps*|f'(w0)|_2 (default 0.0001) -s 1, 3, 4, 7, and 21 Dual maximal violation <= eps; similar to libsvm (default 0.1 except 0.01 for -s 21) -s 5 and 6 |f'(w)|_1 <= eps*min(pos,neg)/l*|f'(w0)|_1, where f is the primal function (default 0.01) -s 12 and 13 |f'(alpha)|_1 <= eps |f'(alpha0)|, where f is the dual function (default 0.1) -B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default -1) -R : not regularize the bias; must with -B 1 to have the bias; DON'T use this unless you know what it is (for -s 0, 2, 5, 6, 11) -wi weight: weights adjust the parameter C of different classes (see README for details) -v n: n-fold cross validation mode -C : find parameters (C for -s 0, 2 and C, p for -s 11) -q : quiet mode (no outputs) ``` -------------------------------- ### Get Library Version Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-linear.md Retrieves the current version of the Liblinear-Java library as an integer. ```java public static int getVersion() ``` -------------------------------- ### Load Data from File and Train Model Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-problem.md Shows how to load training data from a file and train a classification model. Includes configuration of parameters and performing cross-validation. ```java // Load training data Problem problem = Problem.readFromFile( Paths.get("heart_scale"), 1.0 // Add bias term ); // Configure parameters Parameter param = new Parameter(SolverType.L2R_L2LOSS_SVC, 1.0, 0.01); // Train model Model model = Linear.train(problem, param); // Perform cross-validation double[] cvResults = new double[problem.l]; Linear.crossValidation(problem, param, 5, cvResults); // Calculate accuracy int correct = 0; for (int i = 0; i < problem.l; i++) { if (cvResults[i] == problem.y[i]) correct++; } System.out.printf("5-fold CV Accuracy: %.2f%%%n", 100.0 * correct / problem.l); ``` -------------------------------- ### Get Number of Classes Source: https://github.com/bwaldvogel/liblinear-java/blob/main/README.md Returns the number of classes in the model. For regression models, it returns 2. ```c int get_nr_class(const model *model_); ``` -------------------------------- ### Get Number of Features Source: https://github.com/bwaldvogel/liblinear-java/blob/main/README.md Returns the total number of features (attributes) used in the trained model. ```c int get_nr_feature(const model *model_); ``` -------------------------------- ### Get Weight Labels Array Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Returns a copy of the weight labels array, corresponding to the weights. ```java public int[] getWeightLabels() ``` -------------------------------- ### Get Weights Array Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Returns a copy of the weights array used for handling unbalanced data. ```java public double[] getWeights() ``` -------------------------------- ### Tune Parameters with Cross-Validation in liblinear-java Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/README.md Demonstrates automatic parameter search and manual grid search using cross-validation in liblinear-java. This helps in finding optimal hyperparameters for a model. ```java Problem problem = Problem.readFromFile(new File("path/to/data.txt")); Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); Parameter bestParam = Linear.findParameters(problem, param, 10, new double[]{0.1, 0.5, 1.0, 2.0, 5.0}, new double[]{0.001, 0.01, 0.1}); // Or use crossValidation for manual grid search: // double accuracy = Linear.crossValidation(problem, param, 10, Evaluation.ACCURACY); ``` -------------------------------- ### Get Labels Source: https://github.com/bwaldvogel/liblinear-java/blob/main/README.md Outputs the labels of the model into a provided array. For regression models, this array remains unchanged. ```c void get_labels(const model *model_, int* label); ``` -------------------------------- ### Create Binary Classification Problem Programmatically Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-problem.md Demonstrates how to manually construct a binary classification problem with instances, features, and labels. Use this when data is not available in a file format. ```java // Create a binary classification problem with 3 instances, 2 features Problem problem = new Problem(); problem.l = 3; // 3 instances problem.n = 2; // 2 features problem.bias = -1; // No bias problem.y = new double[]{1, 2, 1}; // Class labels // Create sparse features problem.x = new Feature[3][]; // Instance 1: label=1, features: (1, 0.5), (2, 1.0) problem.x[0] = new Feature[]{ new FeatureNode(1, 0.5), new FeatureNode(2, 1.0) }; // Instance 2: label=2, features: (1, -0.3), (2, 0.8) problem.x[1] = new Feature[]{ new FeatureNode(1, -0.3), new FeatureNode(2, 0.8) }; // Instance 3: label=1, features: (2, 0.9) problem.x[2] = new Feature[]{ new FeatureNode(2, 0.9) }; // Use for training Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); Model model = Linear.train(problem, param); ``` -------------------------------- ### Correct 1-Based Feature Indexing Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-feature.md Feature indices must start from 1. Ensure all FeatureNode indices are greater than 0. ```java // CORRECT: indices 1, 2, 3 Feature[] f1 = new Feature[]{ new FeatureNode(1, 0.5), new FeatureNode(2, 0.8), new FeatureNode(3, 0.3) }; // INCORRECT: indices 0, 1, 2 // Feature[] f2 = new Feature[]{ // new FeatureNode(0, 0.5), // Error: index must be > 0 // }; ``` -------------------------------- ### Get Nu Parameter Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Retrieves the nu parameter, typically used in specific solver types like One-Class SVM. ```java public double getNu() ``` -------------------------------- ### Parameter Constructor with Solver, C, Max Iterations, and Epsilon Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md This constructor allows specifying the maximum number of iterations along with solver type, regularization cost, and stopping tolerance. ```java public Parameter(SolverType solver, double C, int max_iters, double eps) ``` -------------------------------- ### Parameter Constructor with Solver, C, and Epsilon Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Use this constructor to create a Parameter object specifying the solver type, regularization cost (C), and stopping tolerance (eps). ```java public Parameter(SolverType solver, double C, double eps) ``` ```java Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); ``` -------------------------------- ### Get Solver ID Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/types.md Retrieve the numeric ID associated with a specific solver type. Useful for internal mapping or serialization. ```java public int getId() ``` ```java SolverType solver = SolverType.L2R_LR; System.out.println(solver.getId()); // 0 ``` -------------------------------- ### Get Model Hash Code Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-model.md Returns a hash code value for the model. This is useful for collections that use hash codes. ```java public int hashCode() ``` -------------------------------- ### Get Class Labels Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-model.md Retrieves a copy of the class labels array from a loaded model. Returns null for regression models. ```java Model model = Linear.loadModel(Paths.get("model.txt")); int[] labels = model.getLabels(); System.out.println("Classes: " + Arrays.toString(labels)); // e.g., [1, 2, 3] ``` -------------------------------- ### Import LIBLINEAR Classes Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/quick-start.md Import necessary classes from the LIBLINEAR library and Java's NIO Paths for file operations. ```java import de.bwaldvogel.liblinear.*; import java.nio.file.Paths; ``` -------------------------------- ### Train Model and Handle Errors Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/quick-start.md Demonstrates training a model from a file, saving it, and includes comprehensive error handling for common issues like invalid input data, file I/O problems, and invalid parameters. ```java try { Problem problem = Problem.readFromFile(Paths.get("train.txt"), 1.0); Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); Model model = Linear.train(problem, param); Linear.saveModel(Paths.get("model.txt"), model); } catch (InvalidInputDataException e) { System.err.printf("Data error at line %d: %s\n", e.getLine(), e.getMessage()); } catch (IOException e) { System.err.println("File I/O error: " + e.getMessage()); } catch (IllegalArgumentException e) { System.err.println("Invalid parameter: " + e.getMessage()); } ``` -------------------------------- ### Get SolverType by ID Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/types.md Obtain the SolverType enumeration constant corresponding to a given numeric ID. Throws an exception if the ID is not recognized. ```java public static SolverType getById(int id) ``` ```java SolverType solver = SolverType.getById(2); System.out.println(solver); // L2R_L2LOSS_SVC ``` -------------------------------- ### Get Class Bias Coefficients Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-model.md Retrieves the bias term coefficient for a specific class. Throws IllegalArgumentException for one-class SVM models. ```java for (int c = 0; c < model.getNrClass(); c++) { double bias = model.getDecfunBias(c); System.out.println("Class " + c + " bias: " + bias); } ``` -------------------------------- ### Predict Probability with liblinear-java Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/README.md How to predict probabilities for instances using specific solver types in liblinear-java. Requires a model trained with a suitable solver and the `predictProbability` method. ```java Model model = Linear.loadModel(new File("path/to/model.txt")); Feature[] x = new Feature[] { new FeatureNode(1, 1.0), new FeatureNode(2, 0.5) }; double[] probs = new double[model.getNrClass()]; Linear.predictProbability(model, x, probs); ``` -------------------------------- ### Feature Interface Definition Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/types.md Defines the contract for a sparse feature, including methods to get and set its index and value. This interface is implemented by FeatureNode. ```java public interface Feature { int getIndex(); double getValue(); void setValue(double value); } ``` -------------------------------- ### Feature Interface Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-feature.md The Feature interface defines the contract for feature representation, including methods to get and set the feature's index and value. ```APIDOC ## Feature Interface ### Description The `Feature` interface defines the contract for feature representation. ### Methods #### getIndex() - **Returns**: `int` - The feature index (1-based) #### getValue() - **Returns**: `double` - The feature value #### setValue(double value) - **Description**: Sets the feature value. - **Parameters**: - `value` (double) - The new feature value. ``` -------------------------------- ### Load Training Data from File Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/quick-start.md Load training data from a file in LIBSVM format. The format expects LABEL FEATURE_INDEX:VALUE pairs. ```java // Load from file in LIBSVM format // Format: LABEL FEATURE_INDEX:VALUE FEATURE_INDEX:VALUE ... Problem problem = Problem.readFromFile(Paths.get("train.txt"), 1.0); ``` -------------------------------- ### Sparse Feature Representation in Liblinear-Java Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-problem.md Illustrates the sparse representation of features used by liblinear, contrasting it with a dense representation. Feature indices start from 1. ```java // Dense representation (memory inefficient for sparse data): // [0, 0.1, 0.2, 0, 0, 0.3, ...] // Sparse representation (used by liblinear): // Feature[]{ // FeatureNode(2, 0.1), // index 2 // FeatureNode(3, 0.2), // index 3 // FeatureNode(6, 0.3) // index 6 // } ``` -------------------------------- ### Create Sparse Feature Array Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-feature.md Demonstrates how to create a sparse feature array for a single instance with a specified number of non-zero features. Ensure feature indices are in ascending order. ```java Feature[] instance = new Feature[3]; instance[0] = new FeatureNode(1, 0.5); // Feature 1 instance[1] = new FeatureNode(5, 1.2); // Feature 5 instance[2] = new FeatureNode(8, -0.3); // Feature 8 // Indices must be in ascending order // feature 1 < feature 5 < feature 8 ✓ ``` -------------------------------- ### Get Specific Feature Weight Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-model.md Retrieves the weight coefficient for a given feature index and class label index. Returns 0.0 if indices are out of range. ```java // Get weight for feature 5, class 2 (0-based index 1) double weight = model.getDecfunCoef(5, 1); // Get all weights for feature 1 across classes for (int c = 0; c < model.getNrClass(); c++) { double w = model.getDecfunCoef(1, c); System.out.println("Class " + c + ": " + w); } ``` -------------------------------- ### Get Decision Function Bias Source: https://github.com/bwaldvogel/liblinear-java/blob/main/README.md Retrieves the bias term for a specific class. This function cannot be used for one-class SVM models. For regression models, label_idx is ignored. ```c double get_decfun_bias(const struct model *model_, int label_idx); ``` -------------------------------- ### Parameter(SolverType solverType, double C, double eps, int max_iters, double p) Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Creates a Parameter with all main options including solver type, regularization cost, stopping tolerance, iteration limit, and epsilon for SVR. ```APIDOC ## Parameter(SolverType solverType, double C, double eps, int max_iters, double p) ### Description Creates a Parameter with all main options. ### Parameters #### Path Parameters - **solverType** (SolverType) - Required - Type of solver (see SolverType enum) - **C** (double) - Required - Regularization parameter (cost > 0). Default usually 1.0. - **eps** (double) - Required - Stopping tolerance (> 0). Default 0.01 for dual solvers, 0.0001 for primal. - **max_iters** (int) - Required - Maximum number of iterations. - **p** (double) - Required - Epsilon in epsilon-insensitive loss function (≥ 0). Used for SVR. ``` -------------------------------- ### Get Model String Representation Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-model.md Returns a string representation of the model's metadata, including bias, number of classes, number of features, and solver type. ```java public String toString() ``` -------------------------------- ### Configure eps for Quick Results Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/configuration.md Sets the stopping tolerance (eps) to 0.1 for faster training, suitable when quick results are prioritized over maximum accuracy. ```java Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.1); ``` -------------------------------- ### Parameter Selection for C Source: https://github.com/bwaldvogel/liblinear-java/blob/main/README.md Conduct cross-validation multiple times to find the optimal parameter C for L2-loss SVM. This command iteratively searches for the best C value. ```bash train -C data_file ``` -------------------------------- ### Make Predictions with liblinear-java Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/README.md Steps to make predictions using a trained liblinear-java model. This requires loading the model and creating feature vectors for prediction. ```java Model model = Linear.loadModel(new File("path/to/model.txt")); Feature[] x = new Feature[] { new FeatureNode(1, 1.0), new FeatureNode(2, 0.5) }; double prediction = Linear.predict(model, x); ``` -------------------------------- ### Get One-Class SVM Rho (Bias) Source: https://github.com/bwaldvogel/liblinear-java/blob/main/README.md Retrieves the bias term (rho) specifically used in one-class SVM models. This function is only applicable to one-class SVM models. ```c double get_decfun_rho(const struct model *model_); ``` -------------------------------- ### Train a Classifier in liblinear-java Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/README.md Steps to train a classifier using liblinear-java. This involves loading data, setting parameters, training the model, and saving the result. ```java Problem problem = Problem.readFromFile(new File("path/to/data.txt")); Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); Model model = Linear.train(problem, param); Linear.saveModel(new File("path/to/model.txt"), model); ``` -------------------------------- ### Get One-Class SVM Rho Bias Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-model.md Retrieves the rho bias term specifically used in one-class SVM models. Throws IllegalArgumentException for non-one-class SVM models. ```java if (model.getSolverType().isOneClass()) { double rho = model.getDecfunRho(); System.out.println("One-class SVM rho: " + rho); } ``` -------------------------------- ### Load Model from Path Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/README.md Loads a trained model from a specified file path. Ensure the path points to a valid Liblinear model file. ```java Model Linear.loadModel(Path path) ``` -------------------------------- ### Load Model from File Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-linear.md Loads a trained model from a file path. It correctly handles ISO-8859-1 charset and English number formatting for accurate loading. ```java public static Model loadModel(Path modelPath) throws IOException ``` ```java Model model = Linear.loadModel(Paths.get("model.txt")); double prediction = Linear.predict(model, testInstance); ``` -------------------------------- ### Load Model from File Path Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-model.md Loads a model from the specified file path. The file must contain a previously saved model. ```java public static Model load(Path modelPath) throws IOException ``` ```java Model model = Model.load(Paths.get("model.dat")); ``` -------------------------------- ### Parameter(SolverType solver, double C, double eps) Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Creates a Parameter with solver type, regularization cost, and stopping tolerance. Default values for max_iters, p, nu, and regularize_bias are applied. ```APIDOC ## Parameter(SolverType solver, double C, double eps) ### Description Creates a Parameter with solver type, regularization cost, and stopping tolerance. ### Parameters #### Path Parameters - **solver** (SolverType) - Required - Type of solver (see SolverType enum) - **C** (double) - Required - Regularization parameter (cost > 0). Default usually 1.0. - **eps** (double) - Required - Stopping tolerance (> 0). Default 0.01 for dual solvers, 0.0001 for primal. ### Request Example ```java Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); ``` ### Default Values - max_iters: 1000 - p: 0.1 - nu: 0.5 - regularize_bias: true ``` -------------------------------- ### Using ParameterSearchResult for Classification and Regression Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/types.md Demonstrates how to retrieve and display the best regularization parameter (C), cross-validation score, and epsilon (p) for regression solvers from a ParameterSearchResult object. ```java ParameterSearchResult result = Linear.findParameters(problem, param, 5, -1, -1); // For classification double bestC = result.getBestC(); double accuracy = result.getBestScore(); System.out.printf("Best C=%.2f, CV Accuracy=%.2f%%\n", bestC, 100*accuracy); // For regression double bestP = result.getBestP(); double mse = result.getBestScore(); System.out.printf("Best C=%.2f, p=%.2f, MSE=%.4f\n", bestC, bestP, mse); ``` -------------------------------- ### Predict Class Label or Regression Value Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-linear.md Predicts the class label or regression value for a single instance using a trained model. The input feature vector should be sparse with indices starting from 1. ```java public static double predict(Model model, Feature[] x) ``` ```java Feature[] testInstance = new Feature[]{ new FeatureNode(1, 0.5), new FeatureNode(3, 1.2), new FeatureNode(5, -0.8) }; double prediction = Linear.predict(model, testInstance); System.out.println("Predicted class: " + prediction); ``` -------------------------------- ### L2-regularized L2-loss SVR Dual Formulation Source: https://github.com/bwaldvogel/liblinear-java/blob/main/README.md This snippet presents the dual formulation for L2-regularized L2-loss Support Vector Regression. It includes an L1 norm penalty on the beta parameters. ```latex min_beta 0.5(beta^T (Q + lambda I/2/C) beta) - y^T beta + \sum |beta_i| ``` -------------------------------- ### I/O Operations Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/README.md Handles loading and saving of trained models. ```APIDOC ## loadModel ### Description Loads a trained model from a file path. ### Method `Model loadModel(Path path)` ### Parameters - **path** (Path) - The path to the model file. ``` ```APIDOC ## saveModel ### Description Saves a trained model to a file path. ### Method `void saveModel(Path path, Model model)` ### Parameters - **path** (Path) - The path where the model will be saved. - **model** (Model) - The model to save. ``` -------------------------------- ### Get Probability Estimates from Logistic Regression Models Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/quick-start.md Obtain probability estimates for class predictions, specifically for logistic regression solvers. This snippet checks if the model supports probability estimation and then predicts probabilities for a given instance. ```java Model model = Linear.loadModel(Paths.get("model.txt")); // Check if model supports probability if (model.isProbabilityModel()) { Feature[] testInstance = new Feature[]{ new FeatureNode(1, 0.5), new FeatureNode(3, 1.2) }; // Predict with probability estimates double[] probabilities = new double[model.getNrClass()]; double prediction = Linear.predictProbability(model, testInstance, probabilities); System.out.println("Predicted class: " + prediction); for (int i = 0; i < model.getNrClass(); i++) { System.out.printf("P(class %d) = %.4f\n", model.getLabels()[i], probabilities[i]); } } ``` -------------------------------- ### Set Stopping Tolerance (Epsilon) Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-parameter.md Sets the stopping tolerance (convergence criterion) for the solver. Smaller values lead to stricter convergence. Epsilon must be positive. ```java public void setEps(double eps) ``` ```java param.setEps(0.001); // Stricter convergence ``` -------------------------------- ### Train Multi-class Classification Model Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/api-problem.md Shows how to load data for multi-class classification and train a model using the one-vs-rest strategy. It also prints the number of classes and labels in the trained model. ```java // Load multi-class data Problem problem = Problem.readFromFile(Paths.get("iris.txt"), 0.0); System.out.println("Classes: " + problem.l + " instances, " + problem.n + " features"); // Train with one-vs-rest strategy Parameter param = new Parameter(SolverType.L2R_LR, 1.0, 0.01); Model model = Linear.train(problem, param); System.out.println("Model has " + model.getNrClass() + " classes"); System.out.println("Class labels: " + Arrays.toString(model.getLabels())); ``` -------------------------------- ### L2-regularized L2-loss SVC Dual Formulation Source: https://github.com/bwaldvogel/liblinear-java/blob/main/README.md This snippet presents the dual formulation for L2-regularized L2-loss Support Vector Classification. It involves optimizing the alpha parameters subject to non-negativity constraints. ```latex min_alpha 0.5(alpha^T (Q + I/2/C) alpha) - e^T alpha s.t. 0 <= alpha_i, ``` -------------------------------- ### Train Linear SVM Source: https://github.com/bwaldvogel/liblinear-java/blob/main/README.md Use this command to train a linear SVM model with the default L2-loss function. ```bash train data_file ``` -------------------------------- ### L2-regularized L1-loss SVR Dual Formulation Source: https://github.com/bwaldvogel/liblinear-java/blob/main/README.md This snippet details the dual formulation for L2-regularized L1-loss Support Vector Regression. It optimizes beta parameters with constraints between -C and C. ```latex min_beta 0.5(beta^T Q beta) - y^T beta + \sum |beta_i| s.t. -C <= beta_i <= C, ``` -------------------------------- ### Handle IOException when Loading a Model Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/errors.md Catch IOException when attempting to load a model from a file that does not exist or is inaccessible. This allows for recovery or user notification in case of file I/O errors. ```java try { Model model = Linear.loadModel(Paths.get("nonexistent.model")); } catch (IOException e) { System.err.println("Cannot load model: " + e.getMessage()); e.printStackTrace(); } ``` -------------------------------- ### Create Safe Parameter Configuration Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/errors.md Safely creates a Parameter object, catching invalid arguments or unknown solver IDs. Returns a default parameter on error. ```java public Parameter createSafeParameter(int solverId, double C, double eps) { try { SolverType solver = SolverType.getById(solverId); Parameter param = new Parameter(solver, C, eps); return param; } catch (IllegalArgumentException e) { System.err.println("Invalid parameter: " + e.getMessage()); // Return default parameter return new Parameter(SolverType.L2R_LR, 1.0, 0.01); } catch (RuntimeException e) { System.err.println("Unknown solver ID: " + solverId); return null; } } ``` -------------------------------- ### Safely Load Model Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/errors.md Loads a model from a file path, checking for file existence and handling potential I/O errors. Returns null if loading fails. ```java public Model safeLoadModel(Path path) { if (!Files.exists(path)) { System.err.println("Model file not found: " + path); return null; } try { return Linear.loadModel(path); } catch (IOException e) { System.err.println("Failed to load model: " + e.getMessage()); return null; } } ``` -------------------------------- ### Configure Regression with Epsilon Tube Width Source: https://github.com/bwaldvogel/liblinear-java/blob/main/_autodocs/configuration.md Configure a regression model using the L2R_L2LOSS_SVR solver. Set the epsilon parameter to define the tube width for the regression. ```java Parameter param = new Parameter(SolverType.L2R_L2LOSS_SVR, 1.0, 0.01); param.setP(0.1); // Epsilon tube width Model model = Linear.train(problem, param); ```