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

ridge_reg_norm_eq_dense_batch.py

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

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