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

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

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