Python* API Reference for Intel® Data Analytics Acceleration Library 2019 Update 4

lin_reg_qr_dense_online.py

Deprecation Notice: With the introduction of daal4py, a package that supersedes PyDAAL, Intel is deprecating PyDAAL and will discontinue support starting with Intel® DAAL 2021 and Intel® Distribution for Python 2021. Until then Intel will continue to provide compatible pyDAAL pip and conda packages for newer releases of Intel DAAL and make it available in open source. However, Intel will not add the new features of Intel DAAL to pyDAAL. Intel recommends developers switch to and use daal4py.

Note: To find daal4py examples, refer to daal4py documentation or browse github repository.

1 # file: lin_reg_qr_dense_online.py
2 #===============================================================================
3 # Copyright 2014-2019 Intel Corporation.
4 #
5 # This software and the related documents are Intel copyrighted materials, and
6 # your use of them is governed by the express license under which they were
7 # provided to you (License). Unless the License provides otherwise, you may not
8 # use, modify, copy, publish, distribute, disclose or transmit this software or
9 # the related documents without Intel's prior written permission.
10 #
11 # This software and the related documents are provided as is, with no express
12 # or implied warranties, other than those that are expressly stated in the
13 # License.
14 #===============================================================================
15 
16 ## <a name="DAAL-EXAMPLE-PY-LINEAR_REGRESSION_QR_ONLINE"></a>
17 ## \example lin_reg_qr_dense_online.py
18 
19 import os
20 import sys
21 
22 from daal.algorithms.linear_regression import training, prediction
23 from daal.data_management import (
24  DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
25 )
26 
27 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
28 if utils_folder not in sys.path:
29  sys.path.insert(0, utils_folder)
30 from utils import printNumericTable
31 
32 DAAL_PREFIX = os.path.join('..', 'data')
33 
34 # Input data set parameters
35 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'online', 'linear_regression_train.csv')
36 testDatasetFileName = os.path.join(DAAL_PREFIX, 'online', 'linear_regression_test.csv')
37 
38 nTrainVectorsInBlock = 250
39 
40 nFeatures = 10 # Number of features in training and testing data sets
41 nDependentVariables = 2 # Number of dependent variables that correspond to each observation
42 
43 trainingResult = None
44 predictionResult = None
45 
46 
47 def trainModel():
48  global trainingResult
49 
50  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
51  trainDataSource = FileDataSource(
52  trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
53  DataSourceIface.doDictionaryFromContext
54  )
55 
56  # Create Numeric Tables for training data and dependent variables
57  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
58  trainDependentVariables = HomogenNumericTable(
59  nDependentVariables, 0, NumericTableIface.doNotAllocate
60  )
61  mergedData = MergedNumericTable(trainData, trainDependentVariables)
62 
63  # Create an algorithm object to train the multiple linear regression model
64  algorithm = training.Online(method=training.qrDense)
65 
66  while(trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData) == nTrainVectorsInBlock):
67  # Pass a training data set and dependent values to the algorithm
68  algorithm.input.set(training.data, trainData)
69  algorithm.input.set(training.dependentVariables, trainDependentVariables)
70 
71  # Update the multiple linear regression model
72  algorithm.compute()
73 
74  # Finalize the multiple linear regression model and retrieve the algorithm results
75  trainingResult = algorithm.finalizeCompute()
76  printNumericTable(trainingResult.get(training.model).getBeta(), "Linear Regression coefficients:")
77 
78 
79 def testModel():
80  global trainingResult, predictionResult
81 
82  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
83  testDataSource = FileDataSource(
84  testDatasetFileName, DataSourceIface.doAllocateNumericTable,
85  DataSourceIface.doDictionaryFromContext
86  )
87 
88  # Create Numeric Tables for testing data and ground truth values
89  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
90  testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
91  mergedData = MergedNumericTable(testData, testGroundTruth)
92 
93  # Retrieve the data from the input file
94  testDataSource.loadDataBlock(mergedData)
95 
96  # Create an algorithm object to predict values of multiple linear regression
97  algorithm = prediction.Batch()
98 
99  # Pass a testing data set and the trained model to the algorithm
100  algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
101  algorithm.input.setTable(prediction.data, testData)
102 
103  # Predict values of multiple linear regression and retrieve the algorithm results
104  predictionResult = algorithm.compute()
105  printNumericTable(predictionResult.get(prediction.prediction), "Linear Regression prediction results: (first 10 rows):", 10)
106  printNumericTable(testGroundTruth, "Ground truth (first 10 rows):", 10)
107 
108 if __name__ == "__main__":
109 
110  trainModel()
111  testModel()

For more complete information about compiler optimizations, see our Optimization Notice.