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

svm_multi_class_dense_batch.py

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

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