Python* API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

lin_reg_qr_dense_online.py

1 # file: lin_reg_qr_dense_online.py
2 #===============================================================================
3 # Copyright 2014-2020 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #===============================================================================
17 
18 
19 
20 
21 import os
22 import sys
23 
24 from daal.algorithms.linear_regression import training, prediction
25 from daal.data_management import (
26  DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
27 )
28 
29 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
30 if utils_folder not in sys.path:
31  sys.path.insert(0, utils_folder)
32 from utils import printNumericTable
33 
34 DAAL_PREFIX = os.path.join('..', 'data')
35 
36 # Input data set parameters
37 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'online', 'linear_regression_train.csv')
38 testDatasetFileName = os.path.join(DAAL_PREFIX, 'online', 'linear_regression_test.csv')
39 
40 nTrainVectorsInBlock = 250
41 
42 nFeatures = 10 # Number of features in training and testing data sets
43 nDependentVariables = 2 # Number of dependent variables that correspond to each observation
44 
45 trainingResult = None
46 predictionResult = None
47 
48 
49 def trainModel():
50  global trainingResult
51 
52  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
53  trainDataSource = FileDataSource(
54  trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
55  DataSourceIface.doDictionaryFromContext
56  )
57 
58  # Create Numeric Tables for training data and dependent variables
59  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
60  trainDependentVariables = HomogenNumericTable(
61  nDependentVariables, 0, NumericTableIface.doNotAllocate
62  )
63  mergedData = MergedNumericTable(trainData, trainDependentVariables)
64 
65  # Create an algorithm object to train the multiple linear regression model
66  algorithm = training.Online(method=training.qrDense)
67 
68  while(trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData) == nTrainVectorsInBlock):
69  # Pass a training data set and dependent values to the algorithm
70  algorithm.input.set(training.data, trainData)
71  algorithm.input.set(training.dependentVariables, trainDependentVariables)
72 
73  # Update the multiple linear regression model
74  algorithm.compute()
75 
76  # Finalize the multiple linear regression model and retrieve the algorithm results
77  trainingResult = algorithm.finalizeCompute()
78  printNumericTable(trainingResult.get(training.model).getBeta(), "Linear Regression coefficients:")
79 
80 
81 def testModel():
82  global trainingResult, predictionResult
83 
84  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
85  testDataSource = FileDataSource(
86  testDatasetFileName, DataSourceIface.doAllocateNumericTable,
87  DataSourceIface.doDictionaryFromContext
88  )
89 
90  # Create Numeric Tables for testing data and ground truth values
91  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
92  testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
93  mergedData = MergedNumericTable(testData, testGroundTruth)
94 
95  # Retrieve the data from the input file
96  testDataSource.loadDataBlock(mergedData)
97 
98  # Create an algorithm object to predict values of multiple linear regression
99  algorithm = prediction.Batch()
100 
101  # Pass a testing data set and the trained model to the algorithm
102  algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
103  algorithm.input.setTable(prediction.data, testData)
104 
105  # Predict values of multiple linear regression and retrieve the algorithm results
106  predictionResult = algorithm.compute()
107  printNumericTable(predictionResult.get(prediction.prediction), "Linear Regression prediction results: (first 10 rows):", 10)
108  printNumericTable(testGroundTruth, "Ground truth (first 10 rows):", 10)
109 
110 if __name__ == "__main__":
111 
112  trainModel()
113  testModel()

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