Python* API Reference for Intel® Data Analytics Acceleration Library 2019 Update 4

svm_two_class_csr_batch.py

Deprecation Notice: With the introduction of daal4py, a package that supersedes PyDAAL, Intel is deprecating PyDAAL and will discontinue support starting with Intel® DAAL 2021 and Intel® Distribution for Python 2021. Until then Intel will continue to provide compatible pyDAAL pip and conda packages for newer releases of Intel DAAL and make it available in open source. However, Intel will not add the new features of Intel DAAL to pyDAAL. Intel recommends developers switch to and use daal4py.

Note: To find daal4py examples, refer to daal4py documentation or browse github repository.

1 # file: svm_two_class_csr_batch.py
2 #===============================================================================
3 # Copyright 2014-2019 Intel Corporation.
4 #
5 # This software and the related documents are Intel copyrighted materials, and
6 # your use of them is governed by the express license under which they were
7 # provided to you (License). Unless the License provides otherwise, you may not
8 # use, modify, copy, publish, distribute, disclose or transmit this software or
9 # the related documents without Intel's prior written permission.
10 #
11 # This software and the related documents are provided as is, with no express
12 # or implied warranties, other than those that are expressly stated in the
13 # License.
14 #===============================================================================
15 
16 ## <a name="DAAL-EXAMPLE-PY-SVM_TWO_CLASS_CSR_BATCH"></a>
17 ## \example svm_two_class_csr_batch.py
18 
19 import os
20 import sys
21 
22 from daal.algorithms.svm import training, prediction
23 from daal.algorithms import kernel_function, classifier
24 from daal.data_management import DataSourceIface, FileDataSource
25 
26 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
27 if utils_folder not in sys.path:
28  sys.path.insert(0, utils_folder)
29 from utils import printNumericTables, createSparseTable
30 
31 # Input data set parameters
32 DATA_PREFIX = os.path.join('..', 'data', 'batch')
33 
34 trainDatasetFileName = os.path.join(DATA_PREFIX, 'svm_two_class_train_csr.csv')
35 trainLabelsFileName = os.path.join(DATA_PREFIX, 'svm_two_class_train_labels.csv')
36 testDatasetFileName = os.path.join(DATA_PREFIX, 'svm_two_class_test_csr.csv')
37 testLabelsFileName = os.path.join(DATA_PREFIX, 'svm_two_class_test_labels.csv')
38 
39 # Parameters for the SVM kernel function
40 kernel = kernel_function.linear.Batch(method=kernel_function.linear.fastCSR)
41 
42 # Model object for the SVM algorithm
43 trainingResult = None
44 predictionResult = None
45 
46 
47 def trainModel():
48  global trainingResult
49 
50  # Initialize FileDataSource to retrieve the input data from a .csv file
51  trainLabelsDataSource = FileDataSource(
52  trainLabelsFileName, DataSourceIface.doAllocateNumericTable,
53  DataSourceIface.doDictionaryFromContext
54  )
55 
56  # Create numeric table for training data
57  trainData = createSparseTable(trainDatasetFileName)
58 
59  # Retrieve the data from the input file
60  trainLabelsDataSource.loadDataBlock()
61 
62  # Create an algorithm object to train the SVM model
63  algorithm = training.Batch()
64 
65  algorithm.parameter.kernel = kernel
66  algorithm.parameter.cacheSize = 40000000
67 
68  # Pass a training data set and dependent values to the algorithm
69  algorithm.input.set(classifier.training.data, trainData)
70  algorithm.input.set(classifier.training.labels, trainLabelsDataSource.getNumericTable())
71 
72  # Build the SVM model
73  trainingResult = algorithm.compute()
74 
75 
76 def testModel():
77  global predictionResult
78 
79  # Create Numeric Tables for testing data
80  testData = createSparseTable(testDatasetFileName)
81 
82  # Create an algorithm object to predict SVM values
83  algorithm = prediction.Batch()
84 
85  algorithm.parameter.kernel = kernel
86 
87  # Pass a testing data set and the trained model to the algorithm
88  algorithm.input.setTable(classifier.prediction.data, testData)
89 
90  algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
91 
92  # Predict SVM values
93  algorithm.compute()
94 
95  # Retrieve the algorithm results
96  predictionResult = algorithm.getResult()
97 
98 
99 def printResults():
100 
101  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
102  testLabelsDataSource = FileDataSource(
103  testLabelsFileName, DataSourceIface.doAllocateNumericTable,
104  DataSourceIface.doDictionaryFromContext
105  )
106  # Retrieve the data from input file
107  testLabelsDataSource.loadDataBlock()
108  testGroundTruth = testLabelsDataSource.getNumericTable()
109 
110  printNumericTables(
111  testGroundTruth, predictionResult.get(classifier.prediction.prediction),
112  "Ground truth\t", "Classification results",
113  "SVM classification results (first 20 observations):", 20, flt64=False
114  )
115 
116 if __name__ == "__main__":
117 
118  trainModel()
119  testModel()
120  printResults()

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