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

lin_reg_norm_eq_dense_distr.py

1 # file: lin_reg_norm_eq_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 trainDatasetFileNames = [
38  os.path.join(DAAL_PREFIX, 'distributed', 'linear_regression_train_1.csv'),
39  os.path.join(DAAL_PREFIX, 'distributed', 'linear_regression_train_2.csv'),
40  os.path.join(DAAL_PREFIX, 'distributed', 'linear_regression_train_3.csv'),
41  os.path.join(DAAL_PREFIX, 'distributed', 'linear_regression_train_4.csv')
42 ]
43 
44 testDatasetFileName = os.path.join(DAAL_PREFIX, 'distributed', 'linear_regression_test.csv')
45 
46 nBlocks = 4
47 
48 nFeatures = 10 # Number of features in training and testing data sets
49 nDependentVariables = 2 # Number of dependent variables that correspond to each observation
50 
51 trainingResult = None
52 predictionResult = None
53 
54 
55 def trainModel():
56  global trainingResult
57 
58  # Create an algorithm object to build the final multiple linear regression model on the master node
59  masterAlgorithm = training.Distributed(step2Master)
60 
61  for i in range(nBlocks):
62  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
63  trainDataSource = FileDataSource(
64  trainDatasetFileNames[i], DataSourceIface.notAllocateNumericTable,
65  DataSourceIface.doDictionaryFromContext
66  )
67 
68  # Create Numeric Tables for training data and variables
69  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
70  trainDependentVariables = HomogenNumericTable(
71  nDependentVariables, 0, NumericTableIface.doNotAllocate
72  )
73  mergedData = MergedNumericTable(trainData, trainDependentVariables)
74 
75  # Retrieve the data from input file
76  trainDataSource.loadDataBlock(mergedData)
77 
78  # Create an algorithm object to train the multiple linear regression model based on the local-node data
79  localAlgorithm = training.Distributed(step1Local)
80 
81  # Pass a training data set and dependent values to the algorithm
82  localAlgorithm.input.set(training.data, trainData)
83  localAlgorithm.input.set(training.dependentVariables, trainDependentVariables)
84 
85  # Train the multiple linear regression model on the local-node data
86  # Set the local multiple linear regression model as input for the master-node algorithm
87  masterAlgorithm.input.add(training.partialModels, localAlgorithm.compute())
88 
89  # Merge and finalize the multiple linear regression model on the master node
90  masterAlgorithm.compute()
91 
92  # Retrieve the algorithm results
93  trainingResult = masterAlgorithm.finalizeCompute()
94  printNumericTable(trainingResult.get(training.model).getBeta(), "Linear Regression coefficients:")
95 
96 
97 def testModel():
98  global trainingResult, predictionResult
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.