/* file: LogRegModelBuilder.java */
* Copyright 2014-2019 Intel Corporation.
* This software and the related documents are Intel copyrighted  materials,  and
* your use of  them is  governed by the  express license  under which  they were
* provided to you (License).  Unless the License provides otherwise, you may not
* use, modify, copy, publish, distribute,  disclose or transmit this software or
* the related documents without Intel's prior written permission.
* This software and the related documents  are provided as  is,  with no express
* or implied  warranties,  other  than those  that are  expressly stated  in the
* License.

 //  Content:
 //     Java example of logistic regression model builder.
 //     The program trains the logistic regression model on a training data set with
 //     the normal equations method and computes regression for the test data.

package com.intel.daal.examples.logistic_regression;

import java.nio.FloatBuffer;
import com.intel.daal.algorithms.logistic_regression.Model;
import com.intel.daal.algorithms.logistic_regression.ModelBuilder;
import com.intel.daal.algorithms.logistic_regression.prediction.*;
import com.intel.daal.algorithms.classifier.prediction.ModelInputId;
import com.intel.daal.algorithms.classifier.prediction.NumericTableInputId;
import com.intel.daal.algorithms.classifier.prediction.PredictionResultId;
import com.intel.daal.data_management.data.NumericTable;
import com.intel.daal.data_management.data.HomogenNumericTable;
import com.intel.daal.data_management.data.MergedNumericTable;
import com.intel.daal.data_management.data_source.DataSource;
import com.intel.daal.data_management.data_source.FileDataSource;
import com.intel.daal.examples.utils.Service;
import com.intel.daal.services.DaalContext;

class LogRegModelBuilder {
    /* Input data set parameters */
    private static final String trainDatasetFileName = "../data/batch/logreg_trained_model.csv";
    private static final String testDatasetFileName  = "../data/batch/logreg_test.csv";

    private static final int nFeatures            = 6;  /* Number of features in training and testing data sets */
    private static final int nClasses             = 5;  /* Number of classes */

    static Model        model;
    static NumericTable results;
    static NumericTable testGroundTruth;

    private static DaalContext context = new DaalContext();

    public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException {


    public static void buildModel() {
        /* Initialize FileDataSource to retrieve the beta data from a .csv file */
        FileDataSource trainDataSource = new FileDataSource(context, trainDatasetFileName,

        /* Create Numeric Table for beta coefficients */
        NumericTable betaData = new HomogenNumericTable(context, Float.class, nFeatures + 1, 0, NumericTable.AllocationFlag.DoNotAllocate);

        /* Define the size of beta */
        long nBeta = betaData.getNumberOfRows()*betaData.getNumberOfColumns();

        /* Initialize beta array */
        FloatBuffer bufferBeta = FloatBuffer.allocate(0);
        bufferBeta = betaData.getBlockOfRows(0, nClasses, bufferBeta);

        /* Create model builder */
        ModelBuilder modelBuilder = new ModelBuilder(context, Float.class, nFeatures, nClasses);

        /* Set beta */
        modelBuilder.setBeta(bufferBeta, nBeta);

        model = modelBuilder.getModel();

    private static void testModel() {
        /* Initialize FileDataSource to retrieve the test data from a .csv file */
        FileDataSource testDataSource = new FileDataSource(context, testDatasetFileName,

        /* Create Numeric Tables for testing data and ground truth labels */
        NumericTable testData = new HomogenNumericTable(context, Float.class, nFeatures, 0, NumericTable.AllocationFlag.DoNotAllocate);
        testGroundTruth = new HomogenNumericTable(context, Float.class, 1, 0, NumericTable.AllocationFlag.DoNotAllocate);
        MergedNumericTable mergedData = new MergedNumericTable(context);

        /* Load the data from the input file */

        /* Create algorithm objects to predict values of multiple logistic regression with the default method */
        PredictionBatch logisticRegressionPredict = new PredictionBatch(context, Float.class,
                PredictionMethod.defaultDense, nClasses);

        logisticRegressionPredict.input.set(NumericTableInputId.data, testData);
        logisticRegressionPredict.input.set(ModelInputId.model, model);


        /* Compute prediction results */
        PredictionResult predictionResult = logisticRegressionPredict.compute();

        results = predictionResult.get(PredictionResultId.prediction);

    private static void printResults() {
        Service.printNumericTable("Logistic Regression coefficients of built model:", model.getBeta());
        Service.printNumericTable("Logistic Regression prediction results: (first 10 rows):", results, 10);
        Service.printNumericTable("Ground truth (first 10 rows):", testGroundTruth, 10);

Optimization Notice: 
For more complete information about compiler optimizations, see our Optimization Notice.
Select sticky button color: 
Orange (only for download buttons)