Python* API Reference for Intel® Data Analytics Acceleration Library 2018 Update 3

lin_reg_norm_eq_dense_distr.py

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