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

svm_two_class_csr_batch.py

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

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