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

ridge_reg_norm_eq_dense_online.py

1 # file: ridge_reg_norm_eq_dense_online.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 online processing mode.
21 # !
22 # ! The program trains the ridge regression model on a training datasetFileName
23 # ! with the normal equations method and computes regression for the test data.
24 # !*****************************************************************************
25 
26 #
27 
28 
29 #
30 
31 import os
32 import sys
33 
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 # Input data set parameters
43 trainDatasetFileName = os.path.join("..", "data", "batch", "linear_regression_train.csv")
44 testDatasetFileName = os.path.join("..", "data", "batch", "linear_regression_test.csv")
45 
46 nTrainVectorsInBlock = 250
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  # Create an algorithm object to train the ridge regression model
63  algorithm = training.Online()
64 
65  while trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData) == nTrainVectorsInBlock:
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  # Update the ridge regression model
71  algorithm.compute()
72 
73 
74  # Finalize the ridge regression model and retrieve the algorithm results
75  trainingResult = algorithm.finalizeCompute()
76 
77  printNumericTable(trainingResult.get(training.model).getBeta(), "Ridge Regression coefficients:")
78  return trainingResult
79 
80 
81 def testModel(trainingResult):
82  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
83  testDataSource = FileDataSource(testDatasetFileName,
84  DataSource.doAllocateNumericTable,
85  DataSource.doDictionaryFromContext)
86 
87  # Create Numeric Tables for testing data and ground truth values
88  testData = HomogenNumericTable(nFeatures, 0, NumericTable.doNotAllocate)
89  testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTable.doNotAllocate)
90  mergedData = MergedNumericTable(testData, testGroundTruth)
91 
92  # Retrieve the data from the input file
93  testDataSource.loadDataBlock(mergedData)
94 
95  # Create an algorithm object to predict values of ridge regression
96  algorithm = prediction.Batch()
97 
98  # Pass a testing data set and the trained model to the algorithm
99  algorithm.input.setTable(prediction.data, testData)
100  algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
101 
102  # Predict values of ridge regression and retrieve the algorithm results
103  predictionResult = algorithm.compute()
104 
105  printNumericTable(predictionResult.get(prediction.prediction),
106  "Ridge Regression prediction results: (first 10 rows):", 10)
107  printNumericTable(testGroundTruth, "Ground truth (first 10 rows):", 10)
108 
109 
110 if __name__ == "__main__":
111  trainingResult = trainModel()
112  testModel(trainingResult)

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