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

svm_multi_class_csr_batch.py

1 # file: svm_multi_class_csr_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 
43 
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 DataSourceIface, FileDataSource
51 
52 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
53 if utils_folder not in sys.path:
54  sys.path.insert(0, utils_folder)
55 from utils import printNumericTables, createSparseTable
56 
57 # Input data set parameters
58 data_dir = os.path.join('..', 'data', 'batch')
59 trainDatasetFileName = os.path.join(data_dir, 'svm_multi_class_train_csr.csv')
60 trainLabelsFileName = os.path.join(data_dir, 'svm_multi_class_train_labels.csv')
61 testDatasetFileName = os.path.join(data_dir, 'svm_multi_class_test_csr.csv')
62 testLabelsFileName = os.path.join(data_dir, 'svm_multi_class_test_labels.csv')
63 
64 nClasses = 5
65 
66 trainingAlg = training.Batch()
67 predictionAlg = prediction.Batch()
68 
69 # Parameters for the SVM kernel function
70 kernel = kernel_function.linear.Batch(method=kernel_function.linear.fastCSR)
71 
72 trainingResult = None
73 predictionResult = None
74 testGroundTruth = None
75 
76 
77 def trainModel():
78  global trainingResult
79 
80  # Initialize FileDataSource to retrieve the input data from a .csv file
81  trainLabelsDataSource = FileDataSource(
82  trainLabelsFileName, DataSourceIface.doAllocateNumericTable,
83  DataSourceIface.doDictionaryFromContext
84  )
85 
86  # Create numeric table for training data
87  trainData = createSparseTable(trainDatasetFileName)
88 
89  # Retrieve the data from the input file
90  trainLabelsDataSource.loadDataBlock()
91 
92  # Create an algorithm object to train the multi-class SVM model
93  algorithm = multi_class_classifier.training.Batch(nClasses)
94 
95  algorithm.parameter.training = trainingAlg
96  algorithm.parameter.prediction = predictionAlg
97 
98  # Pass a training data set and dependent values to the algorithm
99  algorithm.input.set(classifier.training.data, trainData)
100  algorithm.input.set(classifier.training.labels, trainLabelsDataSource.getNumericTable())
101 
102  # Build the multi-class SVM model and retrieve the algorithm results
103  # (Result class from multi_class_classifier.training)
104  trainingResult = algorithm.compute()
105 
106 
107 def testModel():
108  global predictionResult
109 
110  # Create Numeric Tables for testing data
111  testData = createSparseTable(testDatasetFileName)
112 
113  # Create an algorithm object to predict multi-class SVM values
114  algorithm = multi_class_classifier.prediction.Batch(nClasses)
115 
116  algorithm.parameter.training = trainingAlg
117  algorithm.parameter.prediction = predictionAlg
118 
119  # Pass a testing data set and the trained model to the algorithm
120  algorithm.input.setTable(classifier.prediction.data, testData)
121  algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
122 
123  # Predict multi-class SVM values and retrieve the algorithm results
124  # (Result class from classifier.prediction)
125  predictionResult = algorithm.compute()
126 
127 
128 def printResults():
129 
130  # Initialize FileDataSource to retrieve the test data from a .csv file
131  testLabelsDataSource = FileDataSource(
132  testLabelsFileName, DataSourceIface.doAllocateNumericTable,
133  DataSourceIface.doDictionaryFromContext
134  )
135  # Retrieve the data from input file
136  testLabelsDataSource.loadDataBlock()
137  testGroundTruth = testLabelsDataSource.getNumericTable()
138 
139  printNumericTables(
140  testGroundTruth, predictionResult.get(classifier.prediction.prediction),
141  "Ground truth", "Classification results",
142  "Multi-class SVM classification sample program results (first 20 observations):",
143  20, flt64=False
144  )
145 
146 if __name__ == "__main__":
147  trainingAlg.parameter.cacheSize = 100000000
148  trainingAlg.parameter.kernel = kernel
149  predictionAlg.parameter.kernel = kernel
150 
151  trainModel()
152  testModel()
153  printResults()

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