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

ridge_reg_norm_eq_dense_distr.py

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

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