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

svm_two_class_csr_batch.py

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

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