Java* API Reference for Intel® Data Analytics Acceleration Library 2016 Update 4

LinearRegressionNormEqOnline.java

/* file: LinearRegressionNormEqOnline.java */
/*******************************************************************************
* Copyright 2014-2016 Intel Corporation All Rights Reserved.
*
* The source code, information and material ("Material") contained herein is
* owned by Intel Corporation or its suppliers or licensors, and title to such
* Material remains with Intel Corporation or its suppliers or licensors. The
* Material contains proprietary information of Intel or its suppliers and
* licensors. The Material is protected by worldwide copyright laws and treaty
* provisions. No part of the Material may be used, copied, reproduced,
* modified, published, uploaded, posted, transmitted, distributed or disclosed
* in any way without Intel's prior express written permission. No license under
* any patent, copyright or other intellectual property rights in the Material
* is granted to or conferred upon you, either expressly, by implication,
* inducement, estoppel or otherwise. Any license under such intellectual
* property rights must be express and approved by Intel in writing.
*
* Unless otherwise agreed by Intel in writing, you may not remove or alter this
* notice or any other notice embedded in Materials by Intel or Intel's
* suppliers or licensors in any way.
*******************************************************************************/
/*
// Content:
// Java example of multiple linear regression in the online processing mode.
//
// The program trains the multiple linear regression model on a training
// data set with the normal equations method and computes regression for
// the test data.
*/
package com.intel.daal.examples.linear_regression;
import com.intel.daal.algorithms.linear_regression.Model;
import com.intel.daal.algorithms.linear_regression.prediction.*;
import com.intel.daal.algorithms.linear_regression.training.*;
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 LinearRegressionNormEqOnline {
/* Input data set parameters */
private static final String trainDatasetFileName = "../data/online/linear_regression_train.csv";
private static final String testDatasetFileName = "../data/online/linear_regression_test.csv";
private static final int nFeatures = 10;
private static final int nDependentVariables = 2;
private static final int nTrainVectorsInBlock = 250;
static Model model;
static NumericTable results;
static NumericTable testDependentVariables;
private static DaalContext context = new DaalContext();
public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException {
trainModel();
testModel();
printResults();
context.dispose();
}
private static void trainModel() {
/* Initialize FileDataSource to retrieve the input data from a .csv file */
FileDataSource trainDataSource = new FileDataSource(context, trainDatasetFileName,
DataSource.DictionaryCreationFlag.DoDictionaryFromContext,
DataSource.NumericTableAllocationFlag.NotAllocateNumericTable);
/* Create Numeric Tables for training data and labels */
NumericTable trainData = new HomogenNumericTable(context, Double.class, nFeatures, 0, NumericTable.AllocationFlag.NotAllocate);
NumericTable trainDependentVariables = new HomogenNumericTable(context, Double.class, nDependentVariables, 0,
NumericTable.AllocationFlag.NotAllocate);
MergedNumericTable mergedData = new MergedNumericTable(context);
mergedData.addNumericTable(trainData);
mergedData.addNumericTable(trainDependentVariables);
TrainingOnline linearRegressionTraining = new TrainingOnline(context, Double.class, TrainingMethod.normEqDense);
linearRegressionTraining.input.set(TrainingInputId.data, trainData);
linearRegressionTraining.input.set(TrainingInputId.dependentVariable, trainDependentVariables);
while ((trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData)) == nTrainVectorsInBlock) {
linearRegressionTraining.compute();
}
TrainingResult trainingResult = linearRegressionTraining.finalizeCompute();
model = trainingResult.get(TrainingResultId.model);
}
private static void testModel() {
/* Initialize FileDataSource to retrieve the input data from a .csv file */
FileDataSource testDataSource = new FileDataSource(context, testDatasetFileName,
DataSource.DictionaryCreationFlag.DoDictionaryFromContext,
DataSource.NumericTableAllocationFlag.NotAllocateNumericTable);
/* Create Numeric Tables for testing data and labels */
NumericTable testData = new HomogenNumericTable(context, Double.class, nFeatures, 0, NumericTable.AllocationFlag.NotAllocate);
testDependentVariables = new HomogenNumericTable(context, Double.class, nDependentVariables, 0, NumericTable.AllocationFlag.NotAllocate);
MergedNumericTable mergedData = new MergedNumericTable(context);
mergedData.addNumericTable(testData);
mergedData.addNumericTable(testDependentVariables);
/* Retrieve the data from an input file */
testDataSource.loadDataBlock(mergedData);
/* Create algorithm objects to predict values of multiple linear regression with the default method */
PredictionBatch linearRegressionPredict = new PredictionBatch(context, Double.class,
PredictionMethod.defaultDense);
/* Provide the input data */
linearRegressionPredict.input.set(PredictionInputId.data, testData);
linearRegressionPredict.input.set(PredictionInputId.model, model);
/* Compute and retrieve the prediction results */
PredictionResult predictionResult = linearRegressionPredict.compute();
results = predictionResult.get(PredictionResultId.prediction);
}
private static void printResults() {
NumericTable beta = model.getBeta();
NumericTable expected = testDependentVariables;
Service.printNumericTable("Coefficients: ", beta);
Service.printNumericTable("First 10 rows of results (obtained): ", results, 10);
Service.printNumericTable("First 10 rows of results (expected): ", expected, 10);
}
}