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

svm_multi_class_csr_batch.py

1 # file: svm_multi_class_csr_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 DataSourceIface, FileDataSource
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, createSparseTable
32 
33 # Input data set parameters
34 data_dir = os.path.join('..', 'data', 'batch')
35 trainDatasetFileName = os.path.join(data_dir, 'svm_multi_class_train_csr.csv')
36 trainLabelsFileName = os.path.join(data_dir, 'svm_multi_class_train_labels.csv')
37 testDatasetFileName = os.path.join(data_dir, 'svm_multi_class_test_csr.csv')
38 testLabelsFileName = os.path.join(data_dir, 'svm_multi_class_test_labels.csv')
39 
40 nClasses = 5
41 
42 trainingAlg = training.Batch()
43 predictionAlg = prediction.Batch()
44 
45 # Parameters for the SVM kernel function
46 kernel = kernel_function.linear.Batch(method=kernel_function.linear.fastCSR)
47 
48 trainingResult = None
49 predictionResult = None
50 testGroundTruth = None
51 
52 
53 def trainModel():
54  global trainingResult
55 
56  # Initialize FileDataSource to retrieve the input data from a .csv file
57  trainLabelsDataSource = FileDataSource(
58  trainLabelsFileName, DataSourceIface.doAllocateNumericTable,
59  DataSourceIface.doDictionaryFromContext
60  )
61 
62  # Create numeric table for training data
63  trainData = createSparseTable(trainDatasetFileName)
64 
65  # Retrieve the data from the input file
66  trainLabelsDataSource.loadDataBlock()
67 
68  # Create an algorithm object to train the multi-class SVM model
69  algorithm = multi_class_classifier.training.Batch(nClasses)
70 
71  algorithm.parameter.training = trainingAlg
72  algorithm.parameter.prediction = predictionAlg
73 
74  # Pass a training data set and dependent values to the algorithm
75  algorithm.input.set(classifier.training.data, trainData)
76  algorithm.input.set(classifier.training.labels, trainLabelsDataSource.getNumericTable())
77 
78  # Build the multi-class SVM model and retrieve the algorithm results
79  # (Result class from multi_class_classifier.training)
80  trainingResult = algorithm.compute()
81 
82 
83 def testModel():
84  global predictionResult
85 
86  # Create Numeric Tables for testing data
87  testData = createSparseTable(testDatasetFileName)
88 
89  # Create an algorithm object to predict multi-class SVM values
90  algorithm = multi_class_classifier.prediction.Batch(nClasses)
91 
92  algorithm.parameter.training = trainingAlg
93  algorithm.parameter.prediction = predictionAlg
94 
95  # Pass a testing data set and the trained model to the algorithm
96  algorithm.input.setTable(classifier.prediction.data, testData)
97  algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
98 
99  # Predict multi-class SVM values and retrieve the algorithm results
100  # (Result class from classifier.prediction)
101  predictionResult = algorithm.compute()
102 
103 
104 def printResults():
105 
106  # Initialize FileDataSource to retrieve the test data from a .csv file
107  testLabelsDataSource = FileDataSource(
108  testLabelsFileName, DataSourceIface.doAllocateNumericTable,
109  DataSourceIface.doDictionaryFromContext
110  )
111  # Retrieve the data from input file
112  testLabelsDataSource.loadDataBlock()
113  testGroundTruth = testLabelsDataSource.getNumericTable()
114 
115  printNumericTables(
116  testGroundTruth, predictionResult.get(classifier.prediction.prediction),
117  "Ground truth", "Classification results",
118  "Multi-class SVM classification sample program results (first 20 observations):",
119  20, flt64=False
120  )
121 
122 if __name__ == "__main__":
123  trainingAlg.parameter.cacheSize = 100000000
124  trainingAlg.parameter.kernel = kernel
125  predictionAlg.parameter.kernel = kernel
126 
127  trainModel()
128  testModel()
129  printResults()

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