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

lin_reg_norm_eq_dense_batch.py

1 # file: lin_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 
20 
21 import os
22 import sys
23 
24 from daal.algorithms.linear_regression import training, prediction
25 from daal.data_management import (
26  DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
27 )
28 
29 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
30 if utils_folder not in sys.path:
31  sys.path.insert(0, utils_folder)
32 from utils import printNumericTable
33 
34 DAAL_PREFIX = os.path.join('..', 'data')
35 
36 # Input data set parameters
37 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'linear_regression_train.csv')
38 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'linear_regression_test.csv')
39 
40 nFeatures = 10 # Number of features in training and testing data sets
41 nDependentVariables = 2 # Number of dependent variables that correspond to each observation
42 
43 trainingResult = None
44 predictionResult = None
45 
46 
47 def trainModel():
48  global trainingResult
49 
50  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
51  trainDataSource = FileDataSource(
52  trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
53  DataSourceIface.doDictionaryFromContext
54  )
55 
56  # Create Numeric Tables for training data and dependent variables
57  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
58  trainDependentVariables = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
59  mergedData = MergedNumericTable(trainData, trainDependentVariables)
60 
61  # Retrieve the data from input file
62  trainDataSource.loadDataBlock(mergedData)
63 
64  # Create an algorithm object to train the multiple linear regression model with the normal equations method
65  algorithm = training.Batch()
66 
67  # Pass a training data set and dependent values to the algorithm
68  algorithm.input.set(training.data, trainData)
69  algorithm.input.set(training.dependentVariables, trainDependentVariables)
70 
71  # Build the multiple linear regression model and retrieve the algorithm results
72  trainingResult = algorithm.compute()
73  printNumericTable(trainingResult.get(training.model).getBeta(), "Linear Regression coefficients:")
74 
75 
76 def testModel():
77  global trainingResult, predictionResult
78 
79  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
80  testDataSource = FileDataSource(
81  testDatasetFileName, DataSourceIface.doAllocateNumericTable,
82  DataSourceIface.doDictionaryFromContext
83  )
84 
85  # Create Numeric Tables for testing data and ground truth values
86  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
87  testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.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 multiple linear 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 multiple linear regression and retrieve the algorithm results
101  predictionResult = algorithm.compute()
102  printNumericTable(predictionResult.get(prediction.prediction), "Linear Regression prediction results: (first 10 rows):", 10)
103  printNumericTable(testGroundTruth, "Ground truth (first 10 rows):", 10)
104 
105 if __name__ == "__main__":
106 
107  trainModel()
108  testModel()

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