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

svm_multi_class_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: svm_multi_class_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-SVM_MULTI_CLASS_DENSE_BATCH"></a>
17 ## \example svm_multi_class_dense_batch.py
18 
19 import os
20 import sys
21 
22 from daal.algorithms.svm import training, prediction
23 from daal.algorithms import classifier, kernel_function, multi_class_classifier
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 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'svm_multi_class_train_dense.csv')
37 
38 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'svm_multi_class_test_dense.csv')
39 
40 nFeatures = 20
41 nClasses = 5
42 
43 trainingBatch = training.Batch()
44 predictionBatch = prediction.Batch()
45 
46 trainingResult = None
47 predictionResult = None
48 kernelBatch = kernel_function.linear.Batch()
49 testGroundTruth = None
50 
51 
52 def trainModel():
53  global trainingResult
54 
55  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
56  trainDataSource = FileDataSource(
57  trainDatasetFileName,
58  DataSourceIface.notAllocateNumericTable,
59  DataSourceIface.doDictionaryFromContext
60  )
61 
62  # Create Numeric Tables for training data and labels
63  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
64  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
65  mergedData = MergedNumericTable(trainData, trainGroundTruth)
66 
67  # Retrieve the data from the input file
68  trainDataSource.loadDataBlock(mergedData)
69 
70  # Create an algorithm object to train the multi-class SVM model
71  algorithm = multi_class_classifier.training.Batch(nClasses)
72 
73  algorithm.parameter.training = trainingBatch
74  algorithm.parameter.prediction = predictionBatch
75 
76  # Pass a training data set and dependent values to the algorithm
77  algorithm.input.set(classifier.training.data, trainData)
78  algorithm.input.set(classifier.training.labels, trainGroundTruth)
79 
80  # Build the multi-class SVM model
81  # and retrieve Result class from multi_class_classifier.training
82  trainingResult = algorithm.compute()
83 
84 
85 def testModel():
86  global predictionResult, testGroundTruth
87 
88  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
89  testDataSource = FileDataSource(
90  testDatasetFileName,
91  DataSourceIface.doAllocateNumericTable,
92  DataSourceIface.doDictionaryFromContext
93  )
94 
95  # Create Numeric Tables for testing data and labels
96  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
97  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
98  mergedData = MergedNumericTable(testData, testGroundTruth)
99 
100  # Retrieve the data from input file
101  testDataSource.loadDataBlock(mergedData)
102 
103  # Create an algorithm object to predict multi-class SVM values
104  algorithm = multi_class_classifier.prediction.Batch(nClasses)
105 
106  algorithm.parameter.training = trainingBatch
107  algorithm.parameter.prediction = predictionBatch
108 
109  # Pass a testing data set and the trained model to the algorithm
110  algorithm.input.setTable(classifier.prediction.data, testData)
111  algorithm.input.setModel(classifier.prediction.model,
112  trainingResult.get(classifier.training.model))
113 
114  # Predict multi-class SVM values
115  # and retrieve Result class from classifier.prediction
116  predictionResult = algorithm.compute() # Retrieve the algorithm results
117 
118 
119 def printResults():
120 
121  printNumericTables(
122  testGroundTruth,
123  predictionResult.get(classifier.prediction.prediction),
124  "Ground truth", "Classification results",
125  "Multi-class SVM classification sample program results (first 20 observations):", 20, flt64=False
126  )
127 
128 if __name__ == "__main__":
129 
130  trainingBatch.parameter.cacheSize = 100000000
131  trainingBatch.parameter.kernel = kernelBatch
132  predictionBatch.parameter.kernel = kernelBatch
133 
134  trainModel()
135  testModel()
136  printResults()

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