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

ridge_reg_norm_eq_dense_batch.py

1 # file: ridge_reg_norm_eq_dense_batch.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 #
17 # ! Content:
18 # ! Python example of ridge regression in the batch processing mode.
19 # !
20 # ! The program trains the 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.algorithms.ridge_regression import training, prediction
34 from daal.data_management import DataSource, FileDataSource, NumericTable, HomogenNumericTable, MergedNumericTable
35 
36 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
37 if utils_folder not in sys.path:
38  sys.path.insert(0, utils_folder)
39 from utils import printNumericTable
40 
41 # Input data set parameters
42 trainDatasetFileName = os.path.join("..", "data", "batch", "linear_regression_train.csv")
43 testDatasetFileName = os.path.join("..", "data", "batch", "linear_regression_test.csv")
44 
45 nFeatures = 10 # Number of features in training and testing data sets
46 nDependentVariables = 2 # Number of dependent variables that correspond to each observation
47 
48 
49 def trainModel():
50  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
51  trainDataSource = FileDataSource(trainDatasetFileName,
52  DataSource.notAllocateNumericTable,
53  DataSource.doDictionaryFromContext)
54 
55  # Create Numeric Tables for training data and dependent variables
56  trainData = HomogenNumericTable(nFeatures, 0, NumericTable.doNotAllocate)
57  trainDependentVariables = HomogenNumericTable(nDependentVariables, 0, NumericTable.doNotAllocate)
58  mergedData = MergedNumericTable(trainData, trainDependentVariables)
59 
60  # Retrieve the data from input file
61  trainDataSource.loadDataBlock(mergedData)
62 
63  # Create an algorithm object to train the ridge regression model with the normal equations method
64  algorithm = training.Batch()
65 
66  # Pass a training data set and dependent values to the algorithm
67  algorithm.input.set(training.data, trainData)
68  algorithm.input.set(training.dependentVariables, trainDependentVariables)
69 
70  # Build the ridge regression model and etrieve the algorithm results
71  trainingResult = algorithm.compute()
72 
73  printNumericTable(trainingResult.get(training.model).getBeta(), "Ridge Regression coefficients:")
74  return trainingResult
75 
76 
77 def testModel(trainingResult):
78  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
79  testDataSource = FileDataSource(testDatasetFileName,
80  DataSource.doAllocateNumericTable,
81  DataSource.doDictionaryFromContext)
82 
83  # Create Numeric Tables for testing data and ground truth values
84  testData = HomogenNumericTable(nFeatures, 0, NumericTable.doNotAllocate)
85  testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTable.doNotAllocate)
86  mergedData = MergedNumericTable(testData, testGroundTruth)
87 
88  # Load the data from the data file
89  testDataSource.loadDataBlock(mergedData)
90 
91  # Create an algorithm object to predict values of ridge regression
92  algorithm = prediction.Batch()
93 
94  # Pass a testing data set and the trained model to the algorithm
95  algorithm.input.setTable(prediction.data, testData)
96  algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
97 
98  # Predict values of ridge regression and retrieve the algorithm results
99  predictionResult = algorithm.compute()
100 
101  printNumericTable(predictionResult.get(prediction.prediction),
102  "Ridge Regression prediction results: (first 10 rows):", 10)
103  printNumericTable(testGroundTruth, "Ground truth (first 10 rows):", 10)
104 
105 
106 if __name__ == "__main__":
107  trainingResult = trainModel()
108  testModel(trainingResult)

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