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

ridge_reg_norm_eq_dense_distr.py

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

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