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

log_reg_dense_batch.py

1 # file: log_reg_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 import logistic_regression
25 from daal.algorithms.logistic_regression import prediction, training
26 from daal.algorithms import classifier
27 from daal.data_management import (
28  FileDataSource, DataSourceIface, NumericTableIface, HomogenNumericTable,
29  MergedNumericTable, features
30 )
31 
32 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
33 if utils_folder not in sys.path:
34  sys.path.insert(0, utils_folder)
35 from utils import printNumericTable, printNumericTables
36 
37 DAAL_PREFIX = os.path.join('..', 'data')
38 
39 # Input data set parameters
40 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'logreg_train.csv')
41 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'logreg_test.csv')
42 
43 nFeatures = 6
44 nClasses = 5
45 
46 # Model object for the logistic regression algorithm
47 model = None
48 predictionResult = None
49 testGroundTruth = None
50 
51 def trainModel():
52  global model
53 
54  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
55  trainDataSource = FileDataSource(
56  trainDatasetFileName,
57  DataSourceIface.notAllocateNumericTable,
58  DataSourceIface.doDictionaryFromContext
59  )
60 
61  # Create Numeric Tables for training data and labels
62  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
63  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
64  mergedData = MergedNumericTable(trainData, trainGroundTruth)
65 
66  # Retrieve the data from the input file
67  trainDataSource.loadDataBlock(mergedData)
68 
69  # Create an algorithm object to train the logistic regression model
70  algorithm = training.Batch(nClasses)
71 
72  # Pass the training data set and dependent values to the algorithm
73  algorithm.input.set(classifier.training.data, trainData)
74  algorithm.input.set(classifier.training.labels, trainGroundTruth)
75  algorithm.parameter().penaltyL1=0.1;
76  algorithm.parameter().penaltyL2=0.1;
77 
78  # Train the logistic regression model and retrieve the results of the training algorithm
79  trainingResult = algorithm.compute()
80  model = trainingResult.get(classifier.training.model)
81  printNumericTable(model.getBeta(), "Logistic Regression coefficients:")
82 
83 def testModel():
84  global testGroundTruth, predictionResult
85 
86  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
87  testDataSource = FileDataSource(
88  testDatasetFileName,
89  DataSourceIface.notAllocateNumericTable,
90  DataSourceIface.doDictionaryFromContext
91  )
92 
93  # Create Numeric Tables for testing data and labels
94  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
95  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
96  mergedData = MergedNumericTable(testData, testGroundTruth)
97 
98  # Retrieve the data from input file
99  testDataSource.loadDataBlock(mergedData)
100 
101  # Create algorithm objects for logistic regression prediction with the default method
102  algorithm = prediction.Batch(nClasses)
103 
104  # Pass the testing data set and trained model to the algorithm
105  algorithm.input.setTable(classifier.prediction.data, testData)
106  algorithm.input.setModel(classifier.prediction.model, model)
107  algorithm.parameter().resultsToCompute |= logistic_regression.prediction.computeClassesProbabilities | logistic_regression.prediction.computeClassesLogProbabilities
108 
109  # Compute prediction results and retrieve algorithm results
110  # (Result class from classifier.prediction)
111  predictionResult = algorithm.compute()
112 
113 
114 def printResults():
115 
116  printNumericTable(predictionResult.get(classifier.prediction.prediction),"Logistic regression prediction results (first 10 rows):",10)
117  printNumericTable(testGroundTruth,"Ground truth (first 10 rows):",10)
118  printNumericTable(predictionResult.get(logistic_regression.prediction.probabilities),"Logistic regression prediction probabilities (first 10 rows):",10)
119  printNumericTable(predictionResult.get(logistic_regression.prediction.logProbabilities),"Logistic regression prediction log probabilities (first 10 rows):",10)
120 
121 if __name__ == "__main__":
122  trainModel()
123  testModel()
124  printResults()

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