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

stump_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: stump_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-STUMP_BATCH"></a>
17 ## \example stump_dense_batch.py
18 
19 import os
20 import sys
21 
22 from daal.algorithms import classifier
23 from daal.algorithms.stump import training, prediction
24 from daal.data_management import (
25  FileDataSource, DataSourceIface, HomogenNumericTable, MergedNumericTable, NumericTableIface
26 )
27 
28 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
29 if utils_folder not in sys.path:
30  sys.path.insert(0, utils_folder)
31 from utils import printNumericTables
32 
33 DAAL_PREFIX = os.path.join('..', 'data')
34 
35 # Input data set parameters
36 nFeatures = 20
37 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'stump_train.csv')
38 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'stump_test.csv')
39 
40 trainingResult = None
41 predictionResult = None
42 testGroundTruth = None
43 
44 
45 def trainModel():
46  global trainingResult
47  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
48  trainDataSource = FileDataSource(
49  trainDatasetFileName,
50  DataSourceIface.notAllocateNumericTable,
51  DataSourceIface.doDictionaryFromContext
52  )
53 
54  # Create Numeric Tables for training data and labels
55  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
56  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
57  mergedData = MergedNumericTable(trainData, trainGroundTruth)
58 
59  # Retrieve the data from the input file
60  trainDataSource.loadDataBlock(mergedData)
61 
62  # Create an algorithm object to train the stump model
63  algorithm = training.Batch()
64 
65  # Pass a training data set and dependent values to the algorithm
66  algorithm.input.set(classifier.training.data, trainData)
67  algorithm.input.set(classifier.training.labels, trainGroundTruth)
68 
69  # Compute and retrieve the algorithm results
70  trainingResult = algorithm.compute()
71 
72 
73 def testModel():
74  global predictionResult, testGroundTruth
75 
76  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
77  testDataSource = FileDataSource(
78  testDatasetFileName,
79  DataSourceIface.notAllocateNumericTable,
80  DataSourceIface.doDictionaryFromContext
81  )
82 
83  # Create Numeric Tables for training data and labels
84  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
85  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
86  mergedData = MergedNumericTable(testData, testGroundTruth)
87 
88  # Retrieve the data from the input file
89  testDataSource.loadDataBlock(mergedData)
90 
91  # Create an algorithm object to train the stump model
92  algorithm = prediction.Batch()
93 
94  # Pass a training data set and dependent values to the algorithm
95  algorithm.input.setTable(classifier.prediction.data, testData)
96  algorithm.input.setModel(classifier.prediction.model,
97  trainingResult.get(classifier.training.model))
98 
99  # Compute and retrieve the algorithm Result class from classifier.prediction
100  predictionResult = algorithm.compute()
101 
102 
103 def printResults():
104  printNumericTables(
105  testGroundTruth,
106  predictionResult.get(classifier.prediction.prediction),
107  "Ground truth", "Classification results",
108  "Stump classification results (first 20 observations):", 20, flt64=False)
109 
110 if __name__ == "__main__":
111  trainModel()
112  testModel()
113  printResults()

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