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

lin_reg_qr_dense_distr.py

1 # file: lin_reg_qr_dense_distr.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 import step1Local, step2Master
25 from daal.algorithms.linear_regression import training, prediction
26 from daal.data_management import (
27  DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
28 )
29 
30 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
31 if utils_folder not in sys.path:
32  sys.path.insert(0, utils_folder)
33 from utils import printNumericTable
34 
35 DAAL_PREFIX = os.path.join('..', 'data')
36 
37 # Input data set parameters
38 trainDatasetFileNames = [
39  os.path.join(DAAL_PREFIX, 'distributed', 'linear_regression_train_1.csv'),
40  os.path.join(DAAL_PREFIX, 'distributed', 'linear_regression_train_2.csv'),
41  os.path.join(DAAL_PREFIX, 'distributed', 'linear_regression_train_3.csv'),
42  os.path.join(DAAL_PREFIX, 'distributed', 'linear_regression_train_4.csv')
43 ]
44 
45 testDatasetFileName = os.path.join(DAAL_PREFIX, 'distributed', 'linear_regression_test.csv')
46 
47 nBlocks = 4
48 
49 nFeatures = 10 # Number of features in training and testing data sets
50 nDependentVariables = 2 # Number of dependent variables that correspond to each observation
51 
52 trainingResult = None
53 predictionResult = None
54 
55 
56 def trainModel():
57  global trainingResult
58 
59  # Create an algorithm object to build the final multiple linear regression model on the master node
60  masterAlgorithm = training.Distributed(step2Master, method=training.qrDense)
61 
62  for i in range(nBlocks):
63  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
64  trainDataSource = FileDataSource(
65  trainDatasetFileNames[i], DataSourceIface.notAllocateNumericTable,
66  DataSourceIface.doDictionaryFromContext
67  )
68 
69  # Create Numeric Tables for training data and dependent variables
70  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
71  trainDependentVariables = HomogenNumericTable(
72  nDependentVariables, 0, NumericTableIface.doNotAllocate
73  )
74  mergedData = MergedNumericTable(trainData, trainDependentVariables)
75 
76  # Retrieve the data from input file
77  trainDataSource.loadDataBlock(mergedData)
78 
79  # Create an algorithm object to train the multiple linear regression model based on the local-node data
80  localAlgorithm = training.Distributed(step1Local, method=training.qrDense)
81 
82  # Pass a training data set and dependent values to the algorithm
83  localAlgorithm.input.set(training.data, trainData)
84  localAlgorithm.input.set(training.dependentVariables, trainDependentVariables)
85 
86  # Train the multiple linear regression model on the local-node data
87  # Set the local multiple linear regression model as input for the master-node algorithm
88  masterAlgorithm.input.add(training.partialModels, localAlgorithm.compute())
89 
90  # Merge and finalize the multiple linear regression model on the master node
91  masterAlgorithm.compute()
92 
93  # Retrieve the algorithm results
94  trainingResult = masterAlgorithm.finalizeCompute()
95  printNumericTable(trainingResult.get(training.model).getBeta(), "Linear Regression coefficients:")
96 
97 
98 def testModel():
99 
100  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
101  testDataSource = FileDataSource(
102  testDatasetFileName, DataSourceIface.doAllocateNumericTable,
103  DataSourceIface.doDictionaryFromContext
104  )
105 
106  # Create Numeric Tables for testing data and ground truth values
107  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
108  testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
109  mergedData = MergedNumericTable(testData, testGroundTruth)
110 
111  # Retrieve the data from the input file
112  testDataSource.loadDataBlock(mergedData)
113 
114  # Create an algorithm object to predict values of multiple linear regression
115  algorithm = prediction.Batch()
116 
117  # Pass a testing data set and the trained model to the algorithm
118  algorithm.input.setTable(prediction.data, testData)
119  algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
120 
121  # Predict values of multiple linear regression and retrieve the algorithm results
122  predictionResult = algorithm.compute()
123  printNumericTable(predictionResult.get(prediction.prediction), "Linear Regression prediction results: (first 10 rows):", 10)
124  printNumericTable(testGroundTruth, "Ground truth (first 10 rows):", 10)
125 
126 if __name__ == "__main__":
127 
128  trainModel()
129  testModel()

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