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

log_reg_binary_dense_batch.py

1 # file: log_reg_binary_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', 'binary_cls_train.csv')
41 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'binary_cls_test.csv')
42 
43 nFeatures = 20
44 nClasses = 2
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 
76  # Train the logistic regression model and retrieve the results of the training algorithm
77  trainingResult = algorithm.compute()
78  model = trainingResult.get(classifier.training.model)
79  printNumericTable(model.getBeta(), "Logistic Regression coefficients:")
80 
81 def testModel():
82  global testGroundTruth, predictionResult
83 
84  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
85  testDataSource = FileDataSource(
86  testDatasetFileName,
87  DataSourceIface.notAllocateNumericTable,
88  DataSourceIface.doDictionaryFromContext
89  )
90 
91  # Create Numeric Tables for testing data and labels
92  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
93  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
94  mergedData = MergedNumericTable(testData, testGroundTruth)
95 
96  # Retrieve the data from input file
97  testDataSource.loadDataBlock(mergedData)
98 
99  # Create algorithm objects for logistic regression prediction with the default method
100  algorithm = prediction.Batch(nClasses)
101 
102  # Pass the testing data set and trained model to the algorithm
103  algorithm.input.setTable(classifier.prediction.data, testData)
104  algorithm.input.setModel(classifier.prediction.model, model)
105 
106  # Compute prediction results and retrieve algorithm results
107  # (Result class from classifier.prediction)
108  predictionResult = algorithm.compute()
109 
110 
111 def printResults():
112 
113  printNumericTable(predictionResult.get(classifier.prediction.prediction),"Logistic regression prediction results (first 10 rows):",10)
114  printNumericTable(testGroundTruth,"Ground truth (first 10 rows):",10)
115 
116 if __name__ == "__main__":
117 
118  trainModel()
119  testModel()
120  printResults()

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