Python* API Reference for Intel® Data Analytics Acceleration Library 2018 Update 2

svm_multi_class_dense_batch.py

1 # file: svm_multi_class_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2018 Intel Corporation
4 # All Rights Reserved.
5 #
6 # If this software was obtained under the Intel Simplified Software License,
7 # the following terms apply:
8 #
9 # The source code, information and material ("Material") contained herein is
10 # owned by Intel Corporation or its suppliers or licensors, and title to such
11 # Material remains with Intel Corporation or its suppliers or licensors. The
12 # Material contains proprietary information of Intel or its suppliers and
13 # licensors. The Material is protected by worldwide copyright laws and treaty
14 # provisions. No part of the Material may be used, copied, reproduced,
15 # modified, published, uploaded, posted, transmitted, distributed or disclosed
16 # in any way without Intel's prior express written permission. No license under
17 # any patent, copyright or other intellectual property rights in the Material
18 # is granted to or conferred upon you, either expressly, by implication,
19 # inducement, estoppel or otherwise. Any license under such intellectual
20 # property rights must be express and approved by Intel in writing.
21 #
22 # Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 # notice or any other notice embedded in Materials by Intel or Intel's
24 # suppliers or licensors in any way.
25 #
26 #
27 # If this software was obtained under the Apache License, Version 2.0 (the
28 # "License"), the following terms apply:
29 #
30 # You may not use this file except in compliance with the License. You may
31 # obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 #
33 #
34 # Unless required by applicable law or agreed to in writing, software
35 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 #
38 # See the License for the specific language governing permissions and
39 # limitations under the License.
40 #===============================================================================
41 
42 
44 
45 import os
46 import sys
47 
48 from daal.algorithms.svm import training, prediction
49 from daal.algorithms import classifier, kernel_function, multi_class_classifier
50 from daal.data_management import (
51  FileDataSource, DataSourceIface, HomogenNumericTable, MergedNumericTable, NumericTableIface
52 )
53 
54 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
55 if utils_folder not in sys.path:
56  sys.path.insert(0, utils_folder)
57 from utils import printNumericTables
58 
59 DAAL_PREFIX = os.path.join('..', 'data')
60 
61 # Input data set parameters
62 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'svm_multi_class_train_dense.csv')
63 
64 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'svm_multi_class_test_dense.csv')
65 
66 nFeatures = 20
67 nClasses = 5
68 
69 trainingBatch = training.Batch()
70 predictionBatch = prediction.Batch()
71 
72 trainingResult = None
73 predictionResult = None
74 kernelBatch = kernel_function.linear.Batch()
75 testGroundTruth = None
76 
77 
78 def trainModel():
79  global trainingResult
80 
81  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
82  trainDataSource = FileDataSource(
83  trainDatasetFileName,
84  DataSourceIface.notAllocateNumericTable,
85  DataSourceIface.doDictionaryFromContext
86  )
87 
88  # Create Numeric Tables for training data and labels
89  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
90  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
91  mergedData = MergedNumericTable(trainData, trainGroundTruth)
92 
93  # Retrieve the data from the input file
94  trainDataSource.loadDataBlock(mergedData)
95 
96  # Create an algorithm object to train the multi-class SVM model
97  algorithm = multi_class_classifier.training.Batch(nClasses)
98 
99  algorithm.parameter.training = trainingBatch
100  algorithm.parameter.prediction = predictionBatch
101 
102  # Pass a training data set and dependent values to the algorithm
103  algorithm.input.set(classifier.training.data, trainData)
104  algorithm.input.set(classifier.training.labels, trainGroundTruth)
105 
106  # Build the multi-class SVM model
107  # and retrieve Result class from multi_class_classifier.training
108  trainingResult = algorithm.compute()
109 
110 
111 def testModel():
112  global predictionResult, testGroundTruth
113 
114  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
115  testDataSource = FileDataSource(
116  testDatasetFileName,
117  DataSourceIface.doAllocateNumericTable,
118  DataSourceIface.doDictionaryFromContext
119  )
120 
121  # Create Numeric Tables for testing data and labels
122  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
123  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
124  mergedData = MergedNumericTable(testData, testGroundTruth)
125 
126  # Retrieve the data from input file
127  testDataSource.loadDataBlock(mergedData)
128 
129  # Create an algorithm object to predict multi-class SVM values
130  algorithm = multi_class_classifier.prediction.Batch(nClasses)
131 
132  algorithm.parameter.training = trainingBatch
133  algorithm.parameter.prediction = predictionBatch
134 
135  # Pass a testing data set and the trained model to the algorithm
136  algorithm.input.setTable(classifier.prediction.data, testData)
137  algorithm.input.setModel(classifier.prediction.model,
138  trainingResult.get(classifier.training.model))
139 
140  # Predict multi-class SVM values
141  # and retrieve Result class from classifier.prediction
142  predictionResult = algorithm.compute() # Retrieve the algorithm results
143 
144 
145 def printResults():
146 
147  printNumericTables(
148  testGroundTruth,
149  predictionResult.get(classifier.prediction.prediction),
150  "Ground truth", "Classification results",
151  "Multi-class SVM classification sample program results (first 20 observations):", 20, flt64=False
152  )
153 
154 if __name__ == "__main__":
155 
156  trainingBatch.parameter.cacheSize = 100000000
157  trainingBatch.parameter.kernel = kernelBatch
158  predictionBatch.parameter.kernel = kernelBatch
159 
160  trainModel()
161  testModel()
162  printResults()

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