Python* API Reference for Intel® Data Analytics Acceleration Library 2019

log_reg_dense_batch.py

1 # file: log_reg_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2018 Intel Corporation.
4 #
5 # This software and the related documents are Intel copyrighted materials, and
6 # your use of them is governed by the express license under which they were
7 # provided to you (License). Unless the License provides otherwise, you may not
8 # use, modify, copy, publish, distribute, disclose or transmit this software or
9 # the related documents without Intel's prior written permission.
10 #
11 # This software and the related documents are provided as is, with no express
12 # or implied warranties, other than those that are expressly stated in the
13 # License.
14 #===============================================================================
15 
16 ## <a name="DAAL-EXAMPLE-PY-LOG_REG_DENSE_BATCH"></a>
17 ## \example log_reg_dense_batch.py
18 
19 import os
20 import sys
21 
22 from daal.algorithms import logistic_regression
23 from daal.algorithms.logistic_regression import prediction, training
24 from daal.algorithms import classifier
25 from daal.data_management import (
26  FileDataSource, DataSourceIface, NumericTableIface, HomogenNumericTable,
27  MergedNumericTable, features
28 )
29 
30 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
31 if utils_folder not in sys.path:
32  sys.path.insert(0, utils_folder)
33 from utils import printNumericTable, printNumericTables
34 
35 DAAL_PREFIX = os.path.join('..', 'data')
36 
37 # Input data set parameters
38 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'logreg_train.csv')
39 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'logreg_test.csv')
40 
41 nFeatures = 6
42 nClasses = 5
43 
44 # Model object for the logistic regression algorithm
45 model = None
46 predictionResult = None
47 testGroundTruth = None
48 
49 def trainModel():
50  global model
51 
52  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
53  trainDataSource = FileDataSource(
54  trainDatasetFileName,
55  DataSourceIface.notAllocateNumericTable,
56  DataSourceIface.doDictionaryFromContext
57  )
58 
59  # Create Numeric Tables for training data and labels
60  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
61  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
62  mergedData = MergedNumericTable(trainData, trainGroundTruth)
63 
64  # Retrieve the data from the input file
65  trainDataSource.loadDataBlock(mergedData)
66 
67  # Create an algorithm object to train the logistic regression model
68  algorithm = training.Batch(nClasses)
69 
70  # Pass the training data set and dependent values to the algorithm
71  algorithm.input.set(classifier.training.data, trainData)
72  algorithm.input.set(classifier.training.labels, trainGroundTruth)
73  algorithm.parameter().penaltyL1=0.1;
74  algorithm.parameter().penaltyL2=0.1;
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  algorithm.parameter().resultsToCompute |= logistic_regression.prediction.computeClassesProbabilities | logistic_regression.prediction.computeClassesLogProbabilities
106 
107  # Compute prediction results and retrieve algorithm results
108  # (Result class from classifier.prediction)
109  predictionResult = algorithm.compute()
110 
111 
112 def printResults():
113 
114  printNumericTable(predictionResult.get(classifier.prediction.prediction),"Logistic regression prediction results (first 10 rows):",10)
115  printNumericTable(testGroundTruth,"Ground truth (first 10 rows):",10)
116  printNumericTable(predictionResult.get(logistic_regression.prediction.probabilities),"Logistic regression prediction probabilities (first 10 rows):",10)
117  printNumericTable(predictionResult.get(logistic_regression.prediction.logProbabilities),"Logistic regression prediction log probabilities (first 10 rows):",10)
118 
119 if __name__ == "__main__":
120  trainModel()
121  testModel()
122  printResults()

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