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

lin_reg_qr_dense_batch.py

1 # file: lin_reg_qr_dense_batch.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, 'batch', 'linear_regression_train.csv')
38 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'linear_regression_test.csv')
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  # Retrieve the data from input file
64  trainDataSource.loadDataBlock(mergedData)
65 
66  # Create an algorithm object to train the multiple linear regression model with a QR decomposition-based method
67  algorithm = training.Batch(method=training.qrDense)
68 
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  # Build the multiple linear regression model and retrieve the algorithm results
74  trainingResult = algorithm.compute()
75  printNumericTable(trainingResult.get(training.model).getBeta(), "Linear Regression coefficients:")
76 
77 
78 def testModel():
79  global predictionResult
80 
81  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
82  testDataSource = FileDataSource(
83  testDatasetFileName, DataSourceIface.doAllocateNumericTable,
84  DataSourceIface.doDictionaryFromContext
85  )
86 
87  # Create Numeric Tables for testing data and ground truth values
88  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
89  testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
90  mergedData = MergedNumericTable(testData, testGroundTruth)
91 
92  testDataSource.loadDataBlock(mergedData)
93 
94  # Create an algorithm object to predict values of multiple linear regression
95  algorithm = prediction.Batch()
96 
97  # Pass a testing data set and the trained model to the algorithm
98  algorithm.input.setTable(prediction.data, testData)
99  algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
100 
101  # Predict values of multiple linear regression and retrieve the algorithm results
102  predictionResult = algorithm.compute()
103  printNumericTable(predictionResult.get(prediction.prediction), "Linear Regression prediction results: (first 10 rows):", 10)
104  printNumericTable(testGroundTruth, "Ground truth (first 10 rows):", 10)
105 
106 if __name__ == "__main__":
107 
108  trainModel()
109  testModel()

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