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

log_reg_binary_dense_batch.py

Deprecation Notice: With the introduction of daal4py, a package that supersedes PyDAAL, Intel is deprecating PyDAAL and will discontinue support starting with Intel® DAAL 2021 and Intel® Distribution for Python 2021. Until then Intel will continue to provide compatible pyDAAL pip and conda packages for newer releases of Intel DAAL and make it available in open source. However, Intel will not add the new features of Intel DAAL to pyDAAL. Intel recommends developers switch to and use daal4py.

Note: To find daal4py examples, refer to daal4py documentation or browse github repository.

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

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